Guest User

Untitled

a guest
Jul 23rd, 2022
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1.  
  2. def is_increasing(a):
  3.     return all(a[i]<=a[i+1] for i in range(len(a)-1))
  4.  
  5. def do_swaps(a,b,swaps):
  6.     return (tuple(b[i] if swaps[i] else a[i] for i in range(len(a))),
  7.             tuple(a[i] if swaps[i] else b[i] for i in range(len(a))))
  8.  
  9.  
  10. def is_solvable(a,b):
  11.     n = len(a)
  12.     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)])
  13.  
  14.  
  15. def f(n,k):
  16.     X = cartesian_product([range(k)]*n)
  17.     return sum(1 for a,b in cartesian_product([X, X]) if is_solvable(a,b))
  18.  
  19.  
  20. def get_graph(n,k):
  21.     X = cartesian_product([range(k)]*n)
  22.     Y = [x for x in X if is_increasing(x)]
  23.     g = Graph()
  24.     g.allow_loops(True)
  25.     for a,b in cartesian_product([X, X]):
  26.         for swaps in cartesian_product([[0,1]]*n):
  27.             g.add_edge((a,b), do_swaps(a,b,swaps))
  28.     return g, [p for p in g.vertices() if all(x in Y for x in p)]
  29.  
  30.  
  31. #print (is_solvable((1,0), (0,1)))
  32. #print ([f(n,4) for n in range(1, 4)])
  33. g, Y = get_graph(3, 3)
  34. show(g.plot(figsize=100, vertex_colors={'green': Y}))
Advertisement
Add Comment
Please, Sign In to add comment