Home DFS 알고리즘(기본 예제)
Post
Cancel

DFS 알고리즘(기본 예제)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
graph = {
    1: [2,3,4],
    2: [5],
    3: [5],
    4: [],
    5: [6,7],
    6: [],
    7: [3],
}

def dfs(v, discovered=[]):
    discovered.append(v)
    for w in graph[v]:
        if not w in discovered:
            discovered = dfs(w, discovered)
    return discovered

print(dfs(1))
This post is licensed under CC BY 4.0 by the author.