MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / BFS

Method BFS

Graphs/breadth_first_search.py:27–46  ·  view source on GitHub ↗
(self, startVertex)

Source from the content-addressed store, hash-verified

25 self.vertex[fromVertex] = [toVertex]
26
27 def BFS(self, startVertex):
28 # Take a list for stoting already visited vertexes
29 visited = [False] * len(self.vertex)
30
31 # create a list to store all the vertexes for BFS
32 queue = []
33
34 # mark the source node as visited and enqueue it
35 visited[startVertex] = True
36 queue.append(startVertex)
37
38 while queue:
39 startVertex = queue.pop(0)
40 print(startVertex, end = ' ')
41
42 # mark all adjacent nodes as visited and print them
43 for i in self.vertex[startVertex]:
44 if visited[i] == False:
45 queue.append(i)
46 visited[i] = True
47
48if __name__ == '__main__':
49 g = Graph()

Callers 1

Calls 1

popMethod · 0.45

Tested by

no test coverage detected