mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Update graphs/graph_list.py (#3813)
- update naming style to snake_case - add type hints
This commit is contained in:
parent
686d837d3e
commit
83a24cb06d
|
@ -7,34 +7,34 @@
|
||||||
|
|
||||||
class AdjacencyList:
|
class AdjacencyList:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.List = {}
|
self.adj_list = {}
|
||||||
|
|
||||||
def addEdge(self, fromVertex, toVertex):
|
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
|
||||||
# check if vertex is already present
|
# check if vertex is already present
|
||||||
if fromVertex in self.List.keys():
|
if from_vertex in self.adj_list:
|
||||||
self.List[fromVertex].append(toVertex)
|
self.adj_list[from_vertex].append(to_vertex)
|
||||||
else:
|
else:
|
||||||
self.List[fromVertex] = [toVertex]
|
self.adj_list[from_vertex] = [to_vertex]
|
||||||
|
|
||||||
def printList(self):
|
def print_list(self) -> None:
|
||||||
for i in self.List:
|
for i in self.adj_list:
|
||||||
print((i, "->", " -> ".join([str(j) for j in self.List[i]])))
|
print((i, "->", " -> ".join([str(j) for j in self.adj_list[i]])))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
al = AdjacencyList()
|
al = AdjacencyList()
|
||||||
al.addEdge(0, 1)
|
al.add_edge(0, 1)
|
||||||
al.addEdge(0, 4)
|
al.add_edge(0, 4)
|
||||||
al.addEdge(4, 1)
|
al.add_edge(4, 1)
|
||||||
al.addEdge(4, 3)
|
al.add_edge(4, 3)
|
||||||
al.addEdge(1, 0)
|
al.add_edge(1, 0)
|
||||||
al.addEdge(1, 4)
|
al.add_edge(1, 4)
|
||||||
al.addEdge(1, 3)
|
al.add_edge(1, 3)
|
||||||
al.addEdge(1, 2)
|
al.add_edge(1, 2)
|
||||||
al.addEdge(2, 3)
|
al.add_edge(2, 3)
|
||||||
al.addEdge(3, 4)
|
al.add_edge(3, 4)
|
||||||
|
|
||||||
al.printList()
|
al.print_list()
|
||||||
|
|
||||||
# OUTPUT:
|
# OUTPUT:
|
||||||
# 0 -> 1 -> 4
|
# 0 -> 1 -> 4
|
||||||
|
|
Loading…
Reference in New Issue
Block a user