Question: Please help with my homework in python I am trying to do a dfs without using classes but I am having an error with my
Please help with my homework in python
I am trying to do a dfs without using classes but I am having an error with my list called graph.
my code should move to the next key after checking the first key
here is my code: I made some comments to help you out.
Thank you!!
graph = {#help is this a correct way to make edge cases in dfs?; note we should not implement classes.
'2': [2, 1],
'3': [[3, 1], [3, 2]],
'4': [[4, 1], [4, 2], [4, 3]],
'5': [5, 1],
'6': [6, 1],
'7': [[7, 1], [7, 5], [7, 6]],
'8': [[8, 1], [8, 2], [8, 3], [8, 4]],
'9': [[9, 1], [9, 3]],
'10': [10, 3],
'11': [[11, 1], [11, 5], [11, 6]],
'12': [12, 1],
'13': [[13, 1], [13, 4]],
'14': [[14, 1], [14, 2], [14, 3], [14, 4]],
'17': [[17, 6], [17, 7]],
'18': [[18, 1], [18, 2]],
'20': [[20, 1], [20, 2]],
'22': [[22, 1], [22, 2]],
'26': [[26, 24], [26, 25]],
'28': [[28, 3], [28, 24], [28, 25]],
'29': [29, 3],
'30': [[30, 24], [30, 27]],
'31': [[31, 2], [31, 9]],
'32': [[32, 1], [32, 25], [32, 26], [32, 29]],
'33': [[33, 3], [33, 9], [33, 15], [33, 16], [33, 19], [33, 21], [33, 23], [33, 24], [33, 30], [33, 31], [33, 32]],
'34': [[34, 9], [34, 10], [34, 14], [34, 15], [34, 16], [34, 19], [34, 20], [34, 21], [34, 23], [34, 24], [34, 27], [34, 28], [34, 29], [34, 30], [34, 31], [34, 32], [34, 33]]
}
visited = set()
len_order = len(graph)
counter = 0
order = [0 * i for i in range(len_order)]
def dfs(visited, graph, node):
global counter
if node not in visited:
print(node)
visited.add(node)
order[counter] = 1
print(order)
counter = counter + 1
for neighbour in graph[node]: #error is here it's not going to the key 3 in the list called graph
dfs(visited, graph, neighbour)
dfs(visited, graph, '2')
========================================
here is the error
PS C:\Users\Admin\py> python -u "c:\Users\Admin\py\hey.py" 2 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Traceback (most recent call last): File "c:\Users\Admin\py\hey.py", line 85, in
Thank you!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
