Advertisement
here2share

# dictionary_adjacency_list.py

Jun 12th, 2022
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. # dictionary_adjacency_list.py
  2.  
  3. graph = {
  4.   '7' : ['19','21', '14'],
  5.   '19': ['1', '12', '31'],
  6.   '21': [],
  7.   '14': ['23', '6'],
  8.   '1' : [],
  9.   '12': [],
  10.   '31': [],
  11.   '23': [],
  12.   '6' : []
  13. }
  14.  
  15. visited = [] # List of visited nodes of graph.
  16. def dfs(visited, graph, node):
  17.    
  18.     for neighbor in graph[node]:
  19.         dfs(visited, graph, neighbor)
  20.     print(node)
  21.  
  22. print("Following is the Depth-First Search")
  23. dfs(visited, graph, '7')
  24. print("visited=",visited)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement