Question: In Python Please help with my HW, I am trying to make a dfs, but I am receiving an error. I believe the error is
In Python
Please help with my HW, I am trying to make a dfs, but I am receiving an error.
I believe the error is within the graph list but I am not sure. ( on the list)The outside number represents the name of the vertex while the inside list represents the vertexes that are connected to it.
I added comments just in case you need help guiding through the code
thank you;I appreciate your hard work!!
=============================================================================================
here is the error
PS C:\Users\Admin\py> python -u "c:\Use 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]rs\Admin\py\hey.py" 2 51, in
===========================================================================================================================
here is the code
graph = { # the inside list are the vertexes that are connected to the vertex
# 1: [2, 3, 4, 5, 6, 7, 8, 9, 11,12,13,14,18,20,22,32],
2: [1],
3: [1,2],
4: [1,2,3],
5: [1],
6: [1],
7: [1,5,6],
8: [1,2,3,4],
9: [1,3],
10: [3],
11: [1,5,6],
12: [1],
13: [1,4],
14: [1,2,3,4],
17: [6,7],
18: [1,2],
20: [1,2],
22: [1,2],
26: [24,25],
28: [3,24,25],
29: [3],
30: [24,27],
31: [2,9],
32: [1,25,26,29],
33: [3,9,15,16,19,21,23,24,30,31,32],
34: [9,14,15,16,19,20,21,23,24,27,28,29,30,31,32,33]
}
visited = set() # SET OF VISITED NODES
len_order = len(graph)
counter = 0
order = [0 * i for i in range(len_order)]
def dfs(visited, graph, node):
global counter
# PRINT THE NODE, ADD THE NODE TO VISITED SET, SET ITS ORDER TO 1, INCREMENT THE COUNTER, AND CALL FUNCTION RECURSIVELY FOR ITS NEIGHBOURING NODES IF IT IS NOT ALREADY VISITED
if node not in visited:
print(node)
visited.add(node)
order[counter] = 1
print(order)
counter = counter + 1
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
dfs(visited, graph,2)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
