Graph
~2 mins read
1
2
3
4
5
dfs(node):
if node is not visited:
visit node
for n in neighbours:
dfs(n)
1
2
3
4
5
6
7
bfs(start_node):
add start_node to q
while q:
get a node from q
if node is not visited:
add neighbors to q
visit node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
toposort(graph):
tfs(node):
for n in neighbours:
if n is not seen:
tfs(n)
if node is not seen:
mark as seen
add to stack
for node in graph:
tfs(node)
return reversed stack