SHARE
TWEET

Quick Find

a guest Nov 14th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import graphviz
  2. def drawUF(a):
  3.     dot = graphviz.Digraph(comment='Nada')
  4.     n = len(a)
  5.     for i in range(n):
  6.         dot.node(str(i), str(i))
  7.     for i in range(n):
  8.         if i != a[i]:
  9.             dot.edge(str(i), str(a[i]))
  10.     dot.graph_attr['rankdir'] = 'BT'
  11.     return dot
  12.  
  13. dot = drawUF([0, 0, 9, 9, 9, 6, 6, 7, 8, 9])
  14. #print(dot)
  15. graphviz.Source(dot)
  16.  
  17. def quickfind(s, a):
  18.     return s[a]
  19.    
  20. def qfunion(s, a, b):
  21.     pa = quickfind(s, a)
  22.     pb = quickfind(s, b)
  23.     for i in range(len(s)):
  24.         if s[i] == pa:
  25.             s[i] = pb
  26.  
  27. s = [i for i in range(10)]
  28. dot = drawUF(s)
  29. graphviz.Source(dot)
  30.  
  31. qfunion(s, 3, 4)
  32. dot = drawUF(s)
  33. graphviz.Source(dot)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top