mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
Update graphs/depth_first_search_2.py (#3799)
- update naming style to snake_case - add type hints
This commit is contained in:
parent
aebf9bdaaf
commit
4fa8c9d4b4
|
@ -8,58 +8,58 @@ class Graph:
|
||||||
self.vertex = {}
|
self.vertex = {}
|
||||||
|
|
||||||
# for printing the Graph vertices
|
# for printing the Graph vertices
|
||||||
def printGraph(self):
|
def print_graph(self) -> None:
|
||||||
print(self.vertex)
|
print(self.vertex)
|
||||||
for i in self.vertex.keys():
|
for i in self.vertex:
|
||||||
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
|
||||||
|
|
||||||
# for adding the edge between two vertices
|
# for adding the edge between two vertices
|
||||||
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.vertex.keys():
|
if from_vertex in self.vertex:
|
||||||
self.vertex[fromVertex].append(toVertex)
|
self.vertex[from_vertex].append(to_vertex)
|
||||||
else:
|
else:
|
||||||
# else make a new vertex
|
# else make a new vertex
|
||||||
self.vertex[fromVertex] = [toVertex]
|
self.vertex[from_vertex] = [to_vertex]
|
||||||
|
|
||||||
def DFS(self):
|
def dfs(self) -> None:
|
||||||
# visited array for storing already visited nodes
|
# visited array for storing already visited nodes
|
||||||
visited = [False] * len(self.vertex)
|
visited = [False] * len(self.vertex)
|
||||||
|
|
||||||
# call the recursive helper function
|
# call the recursive helper function
|
||||||
for i in range(len(self.vertex)):
|
for i in range(len(self.vertex)):
|
||||||
if visited[i] is False:
|
if not visited[i]:
|
||||||
self.DFSRec(i, visited)
|
self.dfs_recursive(i, visited)
|
||||||
|
|
||||||
def DFSRec(self, startVertex, visited):
|
def dfs_recursive(self, start_vertex: int, visited: list) -> None:
|
||||||
# mark start vertex as visited
|
# mark start vertex as visited
|
||||||
visited[startVertex] = True
|
visited[start_vertex] = True
|
||||||
|
|
||||||
print(startVertex, end=" ")
|
print(start_vertex, end=" ")
|
||||||
|
|
||||||
# Recur for all the vertices that are adjacent to this node
|
# Recur for all the vertices that are adjacent to this node
|
||||||
for i in self.vertex.keys():
|
for i in self.vertex:
|
||||||
if visited[i] is False:
|
if not visited[i]:
|
||||||
self.DFSRec(i, visited)
|
self.dfs_recursive(i, visited)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
g = Graph()
|
g = Graph()
|
||||||
g.addEdge(0, 1)
|
g.add_edge(0, 1)
|
||||||
g.addEdge(0, 2)
|
g.add_edge(0, 2)
|
||||||
g.addEdge(1, 2)
|
g.add_edge(1, 2)
|
||||||
g.addEdge(2, 0)
|
g.add_edge(2, 0)
|
||||||
g.addEdge(2, 3)
|
g.add_edge(2, 3)
|
||||||
g.addEdge(3, 3)
|
g.add_edge(3, 3)
|
||||||
|
|
||||||
g.printGraph()
|
g.print_graph()
|
||||||
print("DFS:")
|
print("DFS:")
|
||||||
g.DFS()
|
g.dfs()
|
||||||
|
|
||||||
# OUTPUT:
|
# OUTPUT:
|
||||||
# 0 -> 1 -> 2
|
# 0 -> 1 -> 2
|
||||||
# 1 -> 2
|
# 1 -> 2
|
||||||
# 2 -> 0 -> 3
|
# 2 -> 0 -> 3
|
||||||
# 3 -> 3
|
# 3 -> 3
|
||||||
# DFS:
|
# DFS:
|
||||||
# 0 1 2 3
|
# 0 1 2 3
|
||||||
|
|
Loading…
Reference in New Issue
Block a user