2018-10-19 12:48:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
""" Author: OMKAR PATHAK """
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2020-12-09 09:21:46 +00:00
|
|
|
|
2021-10-30 11:06:25 +00:00
|
|
|
from queue import Queue
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
class Graph:
|
2020-12-09 09:21:46 +00:00
|
|
|
def __init__(self) -> None:
|
2021-09-07 11:37:03 +00:00
|
|
|
self.vertices: dict[int, list[int]] = {}
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-12-09 09:21:46 +00:00
|
|
|
def print_graph(self) -> None:
|
|
|
|
"""
|
|
|
|
prints adjacency list representation of graaph
|
|
|
|
>>> g = Graph()
|
|
|
|
>>> g.print_graph()
|
|
|
|
>>> g.add_edge(0, 1)
|
|
|
|
>>> g.print_graph()
|
|
|
|
0 : 1
|
|
|
|
"""
|
|
|
|
for i in self.vertices:
|
2020-04-19 13:56:52 +00:00
|
|
|
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-12-09 09:21:46 +00:00
|
|
|
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
|
|
|
|
"""
|
|
|
|
adding the edge between two vertices
|
|
|
|
>>> g = Graph()
|
|
|
|
>>> g.print_graph()
|
|
|
|
>>> g.add_edge(0, 1)
|
|
|
|
>>> g.print_graph()
|
|
|
|
0 : 1
|
|
|
|
"""
|
|
|
|
if from_vertex in self.vertices:
|
|
|
|
self.vertices[from_vertex].append(to_vertex)
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-12-09 09:21:46 +00:00
|
|
|
self.vertices[from_vertex] = [to_vertex]
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def bfs(self, start_vertex: int) -> set[int]:
|
2020-12-09 09:21:46 +00:00
|
|
|
"""
|
|
|
|
>>> g = Graph()
|
|
|
|
>>> g.add_edge(0, 1)
|
|
|
|
>>> g.add_edge(0, 1)
|
|
|
|
>>> g.add_edge(0, 2)
|
|
|
|
>>> g.add_edge(1, 2)
|
|
|
|
>>> g.add_edge(2, 0)
|
|
|
|
>>> g.add_edge(2, 3)
|
|
|
|
>>> g.add_edge(3, 3)
|
|
|
|
>>> sorted(g.bfs(2))
|
|
|
|
[0, 1, 2, 3]
|
|
|
|
"""
|
2020-04-19 13:56:52 +00:00
|
|
|
# initialize set for storing already visited vertices
|
|
|
|
visited = set()
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-04-19 13:56:52 +00:00
|
|
|
# create a first in first out queue to store all the vertices for BFS
|
2021-11-08 18:18:30 +00:00
|
|
|
queue: Queue = Queue()
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
# mark the source node as visited and enqueue it
|
2020-12-09 09:21:46 +00:00
|
|
|
visited.add(start_vertex)
|
2021-10-30 11:06:25 +00:00
|
|
|
queue.put(start_vertex)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-10-30 11:06:25 +00:00
|
|
|
while not queue.empty():
|
|
|
|
vertex = queue.get()
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-04-19 13:56:52 +00:00
|
|
|
# loop through all adjacent vertex and enqueue it if not yet visited
|
|
|
|
for adjacent_vertex in self.vertices[vertex]:
|
|
|
|
if adjacent_vertex not in visited:
|
2021-10-30 11:06:25 +00:00
|
|
|
queue.put(adjacent_vertex)
|
2020-04-19 13:56:52 +00:00
|
|
|
visited.add(adjacent_vertex)
|
|
|
|
return visited
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-12-09 09:21:46 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod(verbose=True)
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
g = Graph()
|
2020-12-09 09:21:46 +00:00
|
|
|
g.add_edge(0, 1)
|
|
|
|
g.add_edge(0, 2)
|
|
|
|
g.add_edge(1, 2)
|
|
|
|
g.add_edge(2, 0)
|
|
|
|
g.add_edge(2, 3)
|
|
|
|
g.add_edge(3, 3)
|
|
|
|
|
|
|
|
g.print_graph()
|
2020-04-19 13:56:52 +00:00
|
|
|
# 0 : 1 -> 2
|
|
|
|
# 1 : 2
|
|
|
|
# 2 : 0 -> 3
|
|
|
|
# 3 : 3
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-12-09 09:21:46 +00:00
|
|
|
assert sorted(g.bfs(2)) == [0, 1, 2, 3]
|