Skip to content

Commit 9fa59b9

Browse files
SteadBytessinghpratyush
authored andcommitted
Fix #27: Add Breadth-First Search [Python] (#539)
* add python breadth first search * update README with python bfs * Implement breadth first **search** instead of just traversal
1 parent 07ea507 commit 9fa59b9

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ 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) | | | | |
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) | | | |
2424
| [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) | |
2525
| [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) |
2626
| [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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Breadth-first search (BFS) is an algorithm for searching tree or
3+
graph data structures. It produces a set of actions to be follwed to reach a
4+
target state from the start state. Starting at the initial state (often the
5+
root node for a tree) it explores all neighbor nodes at each level
6+
before moving to the next level.
7+
"""
8+
9+
from collections import deque, namedtuple
10+
11+
12+
def breadth_first_search(graph, start, target):
13+
""" Performs a breadth-first search on a graph
14+
15+
Args:
16+
graph (list of list of int): Adjacency matrix representation of graph
17+
source (int): Index of source vertex to begin search from
18+
19+
Returns:
20+
tuple (path, distance): path traversed from start to target, total
21+
distance of path
22+
None,None if target not found
23+
"""
24+
vertex_info = {}
25+
VistitedVertex = namedtuple("VisitedVertex", "parent distance")
26+
vertex_info[start] = VistitedVertex(None, 0)
27+
28+
search_queue = deque()
29+
visited = set()
30+
search_queue.append(start)
31+
32+
while search_queue:
33+
u = search_queue.popleft()
34+
if u == target:
35+
return construct_path(u, vertex_info)
36+
for v in graph[u]:
37+
if v not in visited:
38+
if v not in search_queue:
39+
vertex_info[v] = VistitedVertex(
40+
u, vertex_info[u].distance + 1)
41+
search_queue.append(v)
42+
visited.add(u)
43+
return None, None
44+
45+
46+
def construct_path(vertex, vertex_info):
47+
path = []
48+
distance = vertex_info[vertex].distance
49+
while True:
50+
path.append(vertex)
51+
if vertex_info[vertex].parent is not None:
52+
vertex = vertex_info[vertex].parent
53+
else:
54+
break
55+
return path[::-1], distance
56+
57+
58+
def main():
59+
graph_adj_list = [
60+
[1],
61+
[0, 4, 5],
62+
[3, 4, 5],
63+
[2, 6],
64+
[1, 2],
65+
[1, 2, 6],
66+
[3, 5],
67+
[]
68+
]
69+
70+
start = 0
71+
target = 10
72+
path, distance = breadth_first_search(graph_adj_list, start, target)
73+
print('Path from vertex {} to vertex {}: {}'.format(start, target, path))
74+
print('Path distance: {}'.format(distance))
75+
76+
77+
if __name__ == '__main__':
78+
main()

0 commit comments

Comments
 (0)