Skip to content

Commit

Permalink
Fix iiitv#27: Add Breadth-First Search [Python] (iiitv#539)
Browse files Browse the repository at this point in the history
* add python breadth first search

* update README with python bfs

* Implement breadth first **search** instead of just traversal
  • Loading branch information
SteadBytes authored and singhpratyush committed Jan 6, 2018
1 parent 07ea507 commit 9fa59b9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:| :-----------------:|
| [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) | | |
| [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) |
| [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [:white_check_mark:](breadth_first_search/BreadthFirstSearch.java) | | | | |
| [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) | | | |
| [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) | |
| [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) |
| [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) | | | |
Expand Down
78 changes: 78 additions & 0 deletions breadth_first_search/breadth_first_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Breadth-first search (BFS) is an algorithm for searching tree or
graph data structures. It produces a set of actions to be follwed to reach a
target state from the start state. Starting at the initial state (often the
root node for a tree) it explores all neighbor nodes at each level
before moving to the next level.
"""

from collections import deque, namedtuple


def breadth_first_search(graph, start, target):
""" Performs a breadth-first search on a graph
Args:
graph (list of list of int): Adjacency matrix representation of graph
source (int): Index of source vertex to begin search from
Returns:
tuple (path, distance): path traversed from start to target, total
distance of path
None,None if target not found
"""
vertex_info = {}
VistitedVertex = namedtuple("VisitedVertex", "parent distance")
vertex_info[start] = VistitedVertex(None, 0)

search_queue = deque()
visited = set()
search_queue.append(start)

while search_queue:
u = search_queue.popleft()
if u == target:
return construct_path(u, vertex_info)
for v in graph[u]:
if v not in visited:
if v not in search_queue:
vertex_info[v] = VistitedVertex(
u, vertex_info[u].distance + 1)
search_queue.append(v)
visited.add(u)
return None, None


def construct_path(vertex, vertex_info):
path = []
distance = vertex_info[vertex].distance
while True:
path.append(vertex)
if vertex_info[vertex].parent is not None:
vertex = vertex_info[vertex].parent
else:
break
return path[::-1], distance


def main():
graph_adj_list = [
[1],
[0, 4, 5],
[3, 4, 5],
[2, 6],
[1, 2],
[1, 2, 6],
[3, 5],
[]
]

start = 0
target = 10
path, distance = breadth_first_search(graph_adj_list, start, target)
print('Path from vertex {} to vertex {}: {}'.format(start, target, path))
print('Path distance: {}'.format(distance))


if __name__ == '__main__':
main()

0 comments on commit 9fa59b9

Please sign in to comment.