Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Added python implementation for breadth first search #41

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

Merged
merged 1 commit into from
Oct 4, 2018
Merged
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
40 changes: 40 additions & 0 deletions 40 graphs/breadth_first_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Breadth First Search

from collections import defaultdict

class Graph:
def __init__(self):
self.graph = defaultdict(list)

def add_edges(self,_from,_to):
for t in _to:
self.graph[_from].append(t)

def display(self):
print self.graph

def bfs(self,graph,start):
queue = [start]
visited = []

while queue:
a = queue.pop(0)
if a not in visited:
visited.append(a)
for neighbor in graph[a]:
queue.append(neighbor)
print visited

def main():

G = Graph()
G.add_edges(1,[2,7,8])
G.add_edges(2,[3,6])
G.add_edges(3,[4,5])
G.add_edges(8,[9,12])
G.add_edges(9,[10,11])
G.display()
G.bfs(G.graph,1)

if __name__ == '__main__':
main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.