Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Breadth First Traversal [Python] #617

Merged
merged 7 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()