Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Added Breadth first search algorithm in graphs #27

Merged
merged 1 commit into from
Oct 2, 2019
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
51 changes: 51 additions & 0 deletions Python/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class SimpleGraph:
def __init__(self):
self.edges = {}

def neighbors(self, id):
return self.edges[id]


import collections

class Queue:
def __init__(self):
self.elements = collections.deque()

def empty(self):
return len(self.elements) == 0

def put(self, x):
self.elements.append(x)

def get(self):
return self.elements.popleft()


def breadth_first_search(graph, start):
# print out what we find
open_list = Queue()
open_list.put(start)
visited = {}
visited[start] = True

while not open_list.empty():
current = open_list.get()
print("Visiting %r" % current)
for next in graph.neighbors(current):
if next not in visited:
open_list.put(next)
visited[next] = True



if __name__ == '__main__':
example_graph = SimpleGraph()
example_graph.edges = {
'A': ['B'],
'B': ['A', 'C', 'D'],
'C': ['A'],
'D': ['E', 'A'],
'E': ['B']
}
breadth_first_search(example_graph, 'A')