SHARE
TWEET

asdf

a guest Mar 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def bfs_tree(adj, start):
  2.     n = len(adj)
  3.  
  4.     state = [("U") for i in range(n)]
  5.    
  6.  
  7.     parent = [(None) for i in range(n)]
  8.     Q = deque([start])
  9.     state[start] = "D"
  10.     Q.append(start)
  11.     return bfs_loop(adj, Q, state, parent)
  12.  
  13.    
  14.  
  15. def bfs_loop(adj, Q, state, parent):
  16.     while len(Q) > 0:
  17.         u = Q.popleft()
  18.         for vertex, _ in adj[u]:
  19.             if state[vertex] == "U":
  20.                 state[vertex] = "D"
  21.                 parent[vertex] = u
  22.                 Q.append(vertex)
  23.         state[u] = "P"
  24.     return parent
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