Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_increasing(a):
- return all(a[i]<=a[i+1] for i in range(len(a)-1))
- def do_swaps(a,b,swaps):
- return (tuple(b[i] if swaps[i] else a[i] for i in range(len(a))),
- tuple(a[i] if swaps[i] else b[i] for i in range(len(a))))
- def is_solvable(a,b):
- n = len(a)
- return any(all(is_increasing(x) for x in pair) for pair in [do_swaps(a,b,swaps) for swaps in cartesian_product([[0,1]]*n)])
- def f(n,k):
- X = cartesian_product([range(k)]*n)
- return sum(1 for a,b in cartesian_product([X, X]) if is_solvable(a,b))
- def get_graph(n,k):
- X = cartesian_product([range(k)]*n)
- Y = [x for x in X if is_increasing(x)]
- g = Graph()
- g.allow_loops(True)
- for a,b in cartesian_product([X, X]):
- for swaps in cartesian_product([[0,1]]*n):
- g.add_edge((a,b), do_swaps(a,b,swaps))
- return g, [p for p in g.vertices() if all(x in Y for x in p)]
- #print (is_solvable((1,0), (0,1)))
- #print ([f(n,4) for n in range(1, 4)])
- g, Y = get_graph(3, 3)
- show(g.plot(figsize=100, vertex_colors={'green': Y}))
Advertisement
Add Comment
Please, Sign In to add comment