We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 10815b3 commit 5b7c47eCopy full SHA for 5b7c47e
dfs_bfs.py
@@ -1,4 +1,4 @@
1
-# Given a graph, there are two methods to
+# Given a graph, there are two methods to
2
# perform traversal on it.
3
# 1. Depth First Search (DFS)
4
# 2. Breadth First Search (BFS)
@@ -17,9 +17,9 @@ def dfs_2(graph, start, visited=None):
17
visited = set()
18
visited.add(start)
19
for next in graph[start] - visited:
20
- dfs(graph, next, visited)
+ dfs_2(graph, next, visited)
21
return visited
22
-
+
23
def bfs(graph, start):
24
visited, queue = set(), [start]
25
while queue:
@@ -30,7 +30,7 @@ def bfs(graph, start):
30
31
32
# bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}
33
34
def dfs_paths(graph, start, goal):
35
stack = [(start, [start])]
36
while stack:
@@ -47,6 +47,6 @@ def dfs_paths(graph, start, goal):
47
'D': set(['B']),
48
'E': set(['B', 'F']),
49
'F': set(['C', 'E'])}
50
51
result = list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
52
print(result)
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments