mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
Update breadth_first_search.py (#1869)
This commit is contained in:
parent
7aaf79cc23
commit
c92a520956
|
@ -5,42 +5,40 @@
|
||||||
|
|
||||||
class Graph:
|
class Graph:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.vertex = {}
|
self.vertices = {}
|
||||||
|
|
||||||
# for printing the Graph vertices
|
|
||||||
def printGraph(self):
|
def printGraph(self):
|
||||||
for i in self.vertex.keys():
|
"""prints adjacency list representation of graaph"""
|
||||||
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
for i in self.vertices.keys():
|
||||||
|
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
|
||||||
|
|
||||||
# for adding the edge between two vertices
|
|
||||||
def addEdge(self, fromVertex, toVertex):
|
def addEdge(self, fromVertex, toVertex):
|
||||||
# check if vertex is already present,
|
"""adding the edge between two vertices"""
|
||||||
if fromVertex in self.vertex.keys():
|
if fromVertex in self.vertices.keys():
|
||||||
self.vertex[fromVertex].append(toVertex)
|
self.vertices[fromVertex].append(toVertex)
|
||||||
else:
|
else:
|
||||||
# else make a new vertex
|
self.vertices[fromVertex] = [toVertex]
|
||||||
self.vertex[fromVertex] = [toVertex]
|
|
||||||
|
|
||||||
def BFS(self, startVertex):
|
def BFS(self, startVertex):
|
||||||
# Take a list for stoting already visited vertices
|
# initialize set for storing already visited vertices
|
||||||
visited = [False] * len(self.vertex)
|
visited = set()
|
||||||
|
|
||||||
# create a list to store all the vertices for BFS
|
# create a first in first out queue to store all the vertices for BFS
|
||||||
queue = []
|
queue = []
|
||||||
|
|
||||||
# mark the source node as visited and enqueue it
|
# mark the source node as visited and enqueue it
|
||||||
visited[startVertex] = True
|
visited.add(startVertex)
|
||||||
queue.append(startVertex)
|
queue.append(startVertex)
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
startVertex = queue.pop(0)
|
vertex = queue.pop(0)
|
||||||
print(startVertex, end=" ")
|
|
||||||
|
|
||||||
# mark all adjacent nodes as visited and print them
|
# loop through all adjacent vertex and enqueue it if not yet visited
|
||||||
for i in self.vertex[startVertex]:
|
for adjacent_vertex in self.vertices[vertex]:
|
||||||
if visited[i] == False:
|
if adjacent_vertex not in visited:
|
||||||
queue.append(i)
|
queue.append(adjacent_vertex)
|
||||||
visited[i] = True
|
visited.add(adjacent_vertex)
|
||||||
|
return visited
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -53,13 +51,9 @@ if __name__ == "__main__":
|
||||||
g.addEdge(3, 3)
|
g.addEdge(3, 3)
|
||||||
|
|
||||||
g.printGraph()
|
g.printGraph()
|
||||||
print("BFS:")
|
# 0 : 1 -> 2
|
||||||
g.BFS(2)
|
# 1 : 2
|
||||||
|
# 2 : 0 -> 3
|
||||||
|
# 3 : 3
|
||||||
|
|
||||||
# OUTPUT:
|
assert sorted(g.BFS(2)) == [0, 1, 2, 3]
|
||||||
# 0 -> 1 -> 2
|
|
||||||
# 1 -> 2
|
|
||||||
# 2 -> 0 -> 3
|
|
||||||
# 3 -> 3
|
|
||||||
# BFS:
|
|
||||||
# 2 0 3 1
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user