Question: Python; Thank you!! Please help with my homework for DFS in Python : note we should not use classes but use functions and global variables
Python; Thank you!!
Please help with my homework for DFS in Python: note we should not use classes but use functions and global variables in this homework.
here is the input for it: the first number is the source and the second number dictates the destination
this input should be on a list but tbh I am not sure how to make it since there are duplicate destinations. I am assuming they have to be added to the visited. I tried a couple of ways of solving this problem but I am really stuck.
Any help would really be appreaciated.
[2,1] [3,1] [4,1] [5,1] [6,1] [7,1] [8,1] [9,1] [11,1] [12,1] [13,1] [14,1] [18,1] [20,1] [22,1] [32,1] [1,2] [3,2] [4,2] [8,2] [14,2] [18,2] [20,2] [22,2] [31,2] [1,3] [2,3] [4,3] [8,3] [9,3] [10,3] [14,3] [28,3] [29,3] [33,3] [1,4] [2,4] [3,4] [8,4] [13,4] [14,4] [1,5] [7,5] [11,5] [1,6] [7,6] [11,6] [17,6] [1,7] [5,7] [6,7] [17,7] [1,8] [2,8] [3,8] [4,8] [1,9] [3,9] [31,9] [33,9] [34,9] [3,10] [34,10] [1,11] [5,11] [6,11] [1,12] [1,13] [4,13] [1,14] [2,14] [3,14] [4,14] [34,14] [33,15] [34,15] [33,16] [34,16] [6,17] [7,17] [1,18] [2,18] [33,19] [34,19] [1,20] [2,20] [34,20] [33,21] [34,21] [1,22] [2,22] [33,23] [34,23] [26,24] [28,24] [30,24] [33,24] [34,24] [26,25] [28,25] [32,25] [24,26] [25,26] [32,26] [30,27] [34,27] [3,28] [24,28] [25,28] [34,28] [3,29] [32,29] [34,29] [24,30] [27,30] [33,30] [34,30] [2,31] [9,31] [33,31] [34,31] [1,32] [25,32] [26,32] [29,32] [33,32] [34,32] [3,33] [9,33] [15,33] [16,33] [19,33] [21,33] [23,33] [24,33] [30,33] [31,33] [32,33] [34,33] [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,34]
here is my current code.
============================================================================
graph = {#help is this a correct way to make edge cases in dfs; note we are not able to 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')
graph = {#help is this a correct way to make edge cases in dfs; note we are not able to 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') # there is an error
=========================================================

Thank you I appreciate your hard work :)
Name 1. Use the DFS method to explore for the following graph. (start from node 1) Zachary's karate club A social network of a karate club was studied by Wayne W. Zachary for a period of three years from 1970 to 1972. The network captures 34 members of a karate club, documenting links between pairs of members who interacted outside the club. During the study a conflict arose between the administrator "John A" and instructor "Mr. Hi" (pseudonyms), which led to the split of the club into two. Half of the members formed a new club around Mr. Hi; members from the other part found a new instructor or gave up karate. 25 17 32 30 29 27 19 33 23 21 31 16 10 15
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
