DFSとBFSの実装例
BFS(幅優先探索)
と
DFS(深さ優先探索)
の実装例
code:python
def dfs(G, v):
seen =
False
* len(G)
q = deque(
v
)
seen
v
= True
while q:
v = q.pop()
for u in G
v
:
if not seen
u
:
seen
u
= True
q.append(u)
def bfs(G, v):
seen =
False
* len(G)
q = deque(
v
)
seen
v
= True
while q:
v = q.popleft()
for u in G
v
:
if not seen
u
:
seen
u
= True
q.append(u)