Skip to content

Commit

Permalink
Add Breadth First Traversal [Python] (iiitv#617)
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

* add python breadth_first_traversal

* udpate README with python breadth first traversal

* add python breadth_first_search link
  • Loading branch information
SteadBytes authored and singhpratyush committed Jan 7, 2018
1 parent 9fa59b9 commit 2ad3eeb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ 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) | [:white_check_mark:](breadth_first_search/breadth_first_search.py) | | | |
| [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) | | | |
| [Breadth First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) | | | |[:white_check_mark:](breadth_first_traversal/breadth_first_traversal.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
92 changes: 92 additions & 0 deletions breadth_first_traversal/breadth_first_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Breadth-first-traversal is an algorithm for traversing a tree or
graph data structure. Starting at the tree root (or some arbitrary node of a
graph, sometimes referred to as a 'search key'[1]) and explores the neighbor
nodes at that level first, before moving to the next level.
"""

from collections import deque


def breadth_first_traversal(graph, source):
""" Performs a breadth-first traversal 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:
list of dicts describing each vertex in the searched graph
-> [{distance: _, predecessor: _ }]
"""
vertex_info = []
for i in range(len(graph)):
vertex_info.append({"distance": None, "predecessor": None})
vertex_info[source]["distance"] = 0

search_queue = deque()
search_queue.append(source)

while search_queue:
u = search_queue.popleft()
for v in graph[u]:
if vertex_info[v]["distance"] is None:
vertex_info[v]["distance"] = vertex_info[u]["distance"] + 1
vertex_info[v]["predecessor"] = u
search_queue.append(v)
return vertex_info


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

for i in range(len(graph_adj_list)):
print("vertex %s : distance = %s, predecessor = %s" %
(i, vertex_info[i]["distance"], vertex_info[i]["predecessor"]))

assert(vertex_info[0] == {
"distance": 4,
"predecessor": 1
})
assert(vertex_info[1] == {
"distance": 3,
"predecessor": 4
})
assert(vertex_info[2] == {
"distance": 1,
"predecessor": 3
})
assert(vertex_info[3] == {
"distance": 0,
"predecessor": None
})
assert(vertex_info[4] == {
"distance": 2,
"predecessor": 2
})
assert(vertex_info[5] == {
"distance": 2,
"predecessor": 2
})
assert(vertex_info[6] == {
"distance": 1,
"predecessor": 3
})
assert(vertex_info[7] == {
"distance": None,
"predecessor": None
})


if __name__ == '__main__':
main()

0 comments on commit 2ad3eeb

Please sign in to comment.