-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs_new.py
58 lines (41 loc) · 1.38 KB
/
dfs_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# A function to conduct dfs.
def dfs(graph, visited, node):
print(node)
if node not in visited:
visited.append(node)
# Note to self: you made the mistake of putting the for loop in the same position as the if statement
for i in graph[node]:
#print(node)
#print(i, node)
dfs(graph, visited, i)
return visited
'''
Note to self:
return is executed on the CURRENT FUNCTION CALL
It terminates only the CURRENT FUNCTION CALL and goes back up to the previous
function call.
Because the for loop calls keys we had the key error. So need to
specify if a key does not have any children.
What if you encounter something that was already visited? Then it returns the list
Is that what a DFS is? You should ask this.
'''
# a directed graph
graph = {'A': ['B','C'], 'B': ['D', 'F'], 'D': ['E','F'], 'E': ['D'],'F': [], 'C': []}
graph1 = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
def test(): # Problem: I am not entirely sure if this is correct. -> ask someone
if dfs(graph, [], 'A') == ['A', 'B', 'D', 'E', 'F', 'C']:
return True
else:
return False
#print(dfs(graph, [], 'A'))
print(dfs(graph, [], 'A'))