Skip to content

Instantly share code, notes, and snippets.

@GEEGABYTE1
Created August 2, 2021 17:18
Show Gist options
  • Save GEEGABYTE1/fac42ec52d89784fbc6d8d0cfc63c288 to your computer and use it in GitHub Desktop.
Save GEEGABYTE1/fac42ec52d89784fbc6d8d0cfc63c288 to your computer and use it in GitHub Desktop.
Depth-First and BFS traversal
## Our graph has been converted into a dictionary in the form of {vertex: [edge1, edge2, edge3, ..., edge n]} ##
### BFS Traversal ###
first_element = list(test_graph.graph_dict.keys())[0]
last_element = list(test_graph.graph_dict.keys())[-1]
lst = list(test_graph.graph_dict[first_element].edges.keys())
for i in range(len(lst)):
first_edge = lst[0]
if i == 0:
pass
else:
dictionary[first_element].pop(0)
lst.pop(0)
output = bfs(dictionary, first_element, last_element)
#########################
### DFS Traversal ###
first_element = list(test_graph.graph_dict.keys())[0]
lst = list(test_graph.graph_dict[first_element].edges.keys())
for i in range(len(lst)):
first_edge = lst[0]
if i == 0:
pass
else:
dictionary[first_element].pop(0)
lst.pop(0)
output = dfs(dictionary, first_element)
print()
#########################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment