mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-22 09:12:08 +00:00
update graphs/breadth_first_search.py (#3908)
* update graphs/breadth_first_search.py - update naming style to snake_case - add type hints * add doctests
This commit is contained in:
parent
c359768e25
commit
c39be1d8b8
|
@ -2,24 +2,52 @@
|
||||||
|
|
||||||
""" Author: OMKAR PATHAK """
|
""" Author: OMKAR PATHAK """
|
||||||
|
|
||||||
|
from typing import Set
|
||||||
|
|
||||||
|
|
||||||
class Graph:
|
class Graph:
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.vertices = {}
|
self.vertices = {}
|
||||||
|
|
||||||
def printGraph(self):
|
def print_graph(self) -> None:
|
||||||
"""prints adjacency list representation of graaph"""
|
"""
|
||||||
for i in self.vertices.keys():
|
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:
|
||||||
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
|
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
|
||||||
|
|
||||||
def addEdge(self, fromVertex, toVertex):
|
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
|
||||||
"""adding the edge between two vertices"""
|
"""
|
||||||
if fromVertex in self.vertices.keys():
|
adding the edge between two vertices
|
||||||
self.vertices[fromVertex].append(toVertex)
|
>>> 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)
|
||||||
else:
|
else:
|
||||||
self.vertices[fromVertex] = [toVertex]
|
self.vertices[from_vertex] = [to_vertex]
|
||||||
|
|
||||||
def BFS(self, startVertex):
|
def bfs(self, start_vertex: int) -> Set[int]:
|
||||||
|
"""
|
||||||
|
>>> 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]
|
||||||
|
"""
|
||||||
# initialize set for storing already visited vertices
|
# initialize set for storing already visited vertices
|
||||||
visited = set()
|
visited = set()
|
||||||
|
|
||||||
|
@ -27,8 +55,8 @@ class Graph:
|
||||||
queue = []
|
queue = []
|
||||||
|
|
||||||
# mark the source node as visited and enqueue it
|
# mark the source node as visited and enqueue it
|
||||||
visited.add(startVertex)
|
visited.add(start_vertex)
|
||||||
queue.append(startVertex)
|
queue.append(start_vertex)
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
vertex = queue.pop(0)
|
vertex = queue.pop(0)
|
||||||
|
@ -42,18 +70,22 @@ class Graph:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
g = Graph()
|
from doctest import testmod
|
||||||
g.addEdge(0, 1)
|
|
||||||
g.addEdge(0, 2)
|
|
||||||
g.addEdge(1, 2)
|
|
||||||
g.addEdge(2, 0)
|
|
||||||
g.addEdge(2, 3)
|
|
||||||
g.addEdge(3, 3)
|
|
||||||
|
|
||||||
g.printGraph()
|
testmod(verbose=True)
|
||||||
|
|
||||||
|
g = Graph()
|
||||||
|
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()
|
||||||
# 0 : 1 -> 2
|
# 0 : 1 -> 2
|
||||||
# 1 : 2
|
# 1 : 2
|
||||||
# 2 : 0 -> 3
|
# 2 : 0 -> 3
|
||||||
# 3 : 3
|
# 3 : 3
|
||||||
|
|
||||||
assert sorted(g.BFS(2)) == [0, 1, 2, 3]
|
assert sorted(g.bfs(2)) == [0, 1, 2, 3]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user