Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. def DFS(G):
  2. # G to lista list z informacją o istnieniu krawędzi
  3. # G[i] to lista numerów wierzchołków, które są połączone
  4. #      krawędzią z wierzchołkiem i
  5. # tu proszę umieścić swoją implementację
  6.     visited = [False for i in range (len(G))]
  7.     parent = [None for i in range (len(G))]
  8.  
  9.     def DFSvisit(u):
  10.         #visited[u] = True
  11.         for s in G[u]:
  12.             if not visited[s]:
  13.                 visited[s] = True
  14.                 parent[s] = u
  15.                 DFSvisit(s)
  16.  
  17.     for v in range(len(G)):
  18.         if not visited[v]:
  19.             DFSvisit(v)
  20.     return parent
  21.  
  22.  
  23. # elementarny test. Może wypisać np.
  24. # [None, 0, 1, 2]
  25. G = [[1,2],[0,2,3],[3,1,0],[]]
  26. print( DFS(G) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement