26 lines
523 B
Python
26 lines
523 B
Python
# Adjacency list
|
|
first_graph = {
|
|
1: [3, 20],
|
|
2: [20],
|
|
3: [1, 20],
|
|
20: [2, 3]
|
|
}
|
|
|
|
# First exercise: go from 1, to 20, to 2
|
|
first_graph[first_graph[1][1]][0]
|
|
|
|
# pass visited by reference
|
|
def depth_first_search(graph: dict, start, visited=set()):
|
|
print(start)
|
|
if len(visited)==len(graph):
|
|
return visited
|
|
|
|
for x in graph[start]:
|
|
if x not in visited:
|
|
visited.add(x)
|
|
return depth_first_search(graph, x, visited)
|
|
pass
|
|
|
|
|
|
depth_first_search(first_graph, 1)
|