Skip to content

Commit 2ad3eeb

Browse files
SteadBytessinghpratyush
authored andcommitted
Add Breadth First Traversal [Python] (#617)
* add python breadth first search * update README with python bfs * Implement breadth first **search** instead of just traversal * add python breadth_first_traversal * udpate README with python breadth first traversal * add python breadth_first_search link
1 parent 9fa59b9 commit 2ad3eeb

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Community (college) maintained list of Algorithms and Data Structures implementa
2020
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:| :-----------------:|
2121
| [Bin Sort](http://www.cdn.geeksforgeeks.org/bucket-sort-2/)| [:white_check_mark:](bin_sort/bin_sort.c) | |[:white_check_mark:](bin_sort/BinSort.java) | [:white_check_mark:](bin_sort/bin_sort.py) | [:white_check_mark:](bin_sort/bin_sort.go) | | |
2222
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:white_check_mark:](binary_search/binary_search.c) | | [:white_check_mark:](binary_search/BinarySearch.java) | [:white_check_mark:](binary_search/binary_search.py) | [:white_check_mark:](binary_search/binary_search.go) | [:white_check_mark:](binary_search/binarySearch.js) | [:white_check_mark:](binary_search/BinarySearch.cs) |
23-
| [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [:white_check_mark:](breadth_first_search/BreadthFirstSearch.java) | [:white_check_mark:](breadth_first_search/breadth_first_search.py) | | | |
23+
| [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [:white_check_mark:](breadth_first_search/BreadthFirstSearch.java) |[:white_check_mark:](breadth_first_search/breadth_first_search.py) | | | |
24+
| [Breadth First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) | | | |[:white_check_mark:](breadth_first_traversal/breadth_first_traversal.py) | | | |
2425
| [Coin Change Problem](http://www.algorithmist.com/index.php/Coin_Change) | [:white_check_mark:](coin_change_problem/coin_change_problem.c) | | [:white_check_mark:](coin_change_problem/CoinChangeProblem.java) | [:white_check_mark:](coin_change_problem/coin_change_problem.py) | [:white_check_mark:](coin_change_problem/coin_change_problem.go) | [:white_check_mark:](coin_change_problem/coinChangeProblem.js) | |
2526
| [Counting Sort](http://www.geeksforgeeks.org/counting-sort/)| [:white_check_mark:](counting_sort/counting_sort.c) | | [:white_check_mark:](counting_sort/CountingSort.java) | [:white_check_mark:](counting_sort/counting_sort.py) | [:white_check_mark:](counting_sort/counting_sort.go) | [:white_check_mark:](counting_sort/countingSort.js) | [:white_check_mark:](counting_sort/CountingSort.cs) |
2627
| [Depth First Traversal](http://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/) | | | [:white_check_mark:](depth_first_traversal/DepthFirstTraversal.java) | [:white_check_mark:](depth_first_traversal/DepthFirstTraversal.py) | | | |
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Breadth-first-traversal is an algorithm for traversing a tree or
3+
graph data structure. Starting at the tree root (or some arbitrary node of a
4+
graph, sometimes referred to as a 'search key'[1]) and explores the neighbor
5+
nodes at that level first, before moving to the next level.
6+
"""
7+
8+
from collections import deque
9+
10+
11+
def breadth_first_traversal(graph, source):
12+
""" Performs a breadth-first traversal on a graph
13+
14+
Args:
15+
graph (list of list of int): Adjacency matrix representation of graph
16+
source (int): Index of source vertex to begin search from
17+
18+
Returns:
19+
list of dicts describing each vertex in the searched graph
20+
-> [{distance: _, predecessor: _ }]
21+
"""
22+
vertex_info = []
23+
for i in range(len(graph)):
24+
vertex_info.append({"distance": None, "predecessor": None})
25+
vertex_info[source]["distance"] = 0
26+
27+
search_queue = deque()
28+
search_queue.append(source)
29+
30+
while search_queue:
31+
u = search_queue.popleft()
32+
for v in graph[u]:
33+
if vertex_info[v]["distance"] is None:
34+
vertex_info[v]["distance"] = vertex_info[u]["distance"] + 1
35+
vertex_info[v]["predecessor"] = u
36+
search_queue.append(v)
37+
return vertex_info
38+
39+
40+
def main():
41+
graph_adj_list = [
42+
[1],
43+
[0, 4, 5],
44+
[3, 4, 5],
45+
[2, 6],
46+
[1, 2],
47+
[1, 2, 6],
48+
[3, 5],
49+
[]
50+
]
51+
vertex_info = breadth_first_traversal(graph_adj_list, 3)
52+
53+
for i in range(len(graph_adj_list)):
54+
print("vertex %s : distance = %s, predecessor = %s" %
55+
(i, vertex_info[i]["distance"], vertex_info[i]["predecessor"]))
56+
57+
assert(vertex_info[0] == {
58+
"distance": 4,
59+
"predecessor": 1
60+
})
61+
assert(vertex_info[1] == {
62+
"distance": 3,
63+
"predecessor": 4
64+
})
65+
assert(vertex_info[2] == {
66+
"distance": 1,
67+
"predecessor": 3
68+
})
69+
assert(vertex_info[3] == {
70+
"distance": 0,
71+
"predecessor": None
72+
})
73+
assert(vertex_info[4] == {
74+
"distance": 2,
75+
"predecessor": 2
76+
})
77+
assert(vertex_info[5] == {
78+
"distance": 2,
79+
"predecessor": 2
80+
})
81+
assert(vertex_info[6] == {
82+
"distance": 1,
83+
"predecessor": 3
84+
})
85+
assert(vertex_info[7] == {
86+
"distance": None,
87+
"predecessor": None
88+
})
89+
90+
91+
if __name__ == '__main__':
92+
main()

0 commit comments

Comments
 (0)