Skip to content

Commit 6dad899

Browse files
committed
added python dfs and bfs
1 parent 3c4604f commit 6dad899

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed
File renamed without changes.

src/python/WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "algorithms_python")

src/python/sequential/graph/bfs/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
py_binary(
2+
name = "bfs",
3+
main = "breadth_first_traversal.py",
4+
srcs = glob(["*.py"]),
5+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import collections
2+
3+
4+
def bfs(graph, root):
5+
"""
6+
Performs breadth-first traversal on the :param graph: starting from the :param root: node.
7+
"""
8+
explored = set()
9+
traversed = []
10+
search_queue = collections.deque()
11+
12+
search_queue.append(root)
13+
while search_queue:
14+
node = search_queue.popleft()
15+
if node not in explored:
16+
explored.add(node)
17+
traversed.append(node)
18+
search_queue += graph.get(node, [])
19+
20+
return traversed
21+
22+
23+
def main():
24+
graph = {
25+
"you": ["alice", "bob", "clair"],
26+
"alice": ["peggy"],
27+
"clair": ["thom", "jonny"],
28+
"bob": ["anuj", "peggy"],
29+
}
30+
31+
print(
32+
bfs(graph, "you")
33+
)
34+
35+
36+
if __name__ == "__main__":
37+
main()

src/python/sequential/graph/dfs/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
py_binary(
2+
name = "dfs_iterative",
3+
main = "iterative_depth_first_traversal.py",
4+
srcs = ["iterative_depth_first_traversal.py"],
5+
)
6+
7+
py_binary(
8+
name = "dfs_recursive",
9+
main = "recursive_depth_first_traversal.py",
10+
srcs = ["recursive_depth_first_traversal.py"],
11+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import collections
2+
3+
4+
def dfs(graph, root):
5+
"""
6+
Performs iterative depth-first traversal on the :param graph: starting from the :param root: node.
7+
"""
8+
explored = set()
9+
traversed = []
10+
search_stack = collections.deque()
11+
12+
search_stack.append(root)
13+
while search_stack:
14+
node = search_stack.pop()
15+
if node not in explored:
16+
explored.add(node)
17+
traversed.append(node)
18+
search_stack += graph.get(node, [])
19+
20+
return traversed
21+
22+
23+
def main():
24+
graph = {
25+
"you": ["alice", "bob", "clair"],
26+
"alice": ["peggy"],
27+
"clair": ["thom", "jonny"],
28+
"bob": ["anuj", "peggy"],
29+
}
30+
31+
print(
32+
dfs(graph, "you")
33+
)
34+
35+
36+
if __name__ == "__main__":
37+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def dfs(graph, root):
2+
"""
3+
Performs recursive depth-first traversal on the :param graph: starting from the :param root: node.
4+
"""
5+
explored = set()
6+
traversed = []
7+
8+
def explore(node):
9+
explored.add(node)
10+
traversed.append(node)
11+
12+
successors = graph.get(node, [])
13+
for succ in successors:
14+
if succ not in explored:
15+
explore(succ)
16+
17+
explore(root)
18+
19+
return traversed
20+
21+
22+
def main():
23+
graph = {
24+
"you": ["alice", "bob", "clair"],
25+
"alice": ["peggy"],
26+
"clair": ["thom", "jonny"],
27+
"bob": ["anuj", "peggy"],
28+
}
29+
30+
print(
31+
dfs(graph, "you")
32+
)
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)