mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-20 00:02:04 +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 """
|
||||
|
||||
from typing import Set
|
||||
|
||||
|
||||
class Graph:
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.vertices = {}
|
||||
|
||||
def printGraph(self):
|
||||
"""prints adjacency list representation of graaph"""
|
||||
for i in self.vertices.keys():
|
||||
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:
|
||||
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
|
||||
|
||||
def addEdge(self, fromVertex, toVertex):
|
||||
"""adding the edge between two vertices"""
|
||||
if fromVertex in self.vertices.keys():
|
||||
self.vertices[fromVertex].append(toVertex)
|
||||
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)
|
||||
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
|
||||
visited = set()
|
||||
|
||||
|
@ -27,8 +55,8 @@ class Graph:
|
|||
queue = []
|
||||
|
||||
# mark the source node as visited and enqueue it
|
||||
visited.add(startVertex)
|
||||
queue.append(startVertex)
|
||||
visited.add(start_vertex)
|
||||
queue.append(start_vertex)
|
||||
|
||||
while queue:
|
||||
vertex = queue.pop(0)
|
||||
|
@ -42,18 +70,22 @@ class Graph:
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
g = Graph()
|
||||
g.addEdge(0, 1)
|
||||
g.addEdge(0, 2)
|
||||
g.addEdge(1, 2)
|
||||
g.addEdge(2, 0)
|
||||
g.addEdge(2, 3)
|
||||
g.addEdge(3, 3)
|
||||
from doctest import testmod
|
||||
|
||||
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
|
||||
# 1 : 2
|
||||
# 2 : 0 -> 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