Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

4/8/23, 5:56 PM 3EA19_M04_HASNA AFIFAH S_17222009.

ipynb - Colaboratory

graph = {
'A' : ['Z','S', 'T'],
'Z' : ['O'],
'O' : ['S1'],
'S1' : ['F','R'],
'F' : ['B'],
'B' : [],
'R' : [],
'S' : ['O1', 'F1', 'R1'],
'O1' : [],
'F1' : ['B'],
'B1' : [],
'R1' : ['C', 'P'],
'C' : [],
'P' : [],
'T' : ['L'],
'L' : ['M'],
'M' : ['D'],
'D' : []
}

visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): #function for dfs


if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

print("Following is the Depth-First Search")

Following is the Depth-First Search

dfs(visited, graph, 'A')

A
Z
O
S1
F
B
R
S
O1
F1
R1
C
P
T
L
M
D

https://colab.research.google.com/drive/1g6zk7mRqoDLnQZSDqma0Y8EMB4BfGbdY?hl=id#scrollTo=1FUehJ74WA9P&printMode=true 1/2
4/8/23, 5:56 PM 3EA19_M04_HASNA AFIFAH S_17222009.ipynb - Colaboratory

check 0 d selesai pada 17.56

https://colab.research.google.com/drive/1g6zk7mRqoDLnQZSDqma0Y8EMB4BfGbdY?hl=id#scrollTo=1FUehJ74WA9P&printMode=true 2/2

You might also like