MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / dfs_recursive

Method dfs_recursive

graphs/depth_first_search_2.py:81–109  ·  view source on GitHub ↗

Perform a recursive depth-first search (DFS) traversal on the graph. :param start_vertex: The starting vertex for the traversal. :param visited: A list to track visited vertices. Example: >>> g = Graph() >>> g.add_edge(0, 1) >>> g.add_edge(0

(self, start_vertex: int, visited: list)

Source from the content-addressed store, hash-verified

79 self.dfs_recursive(i, visited)
80
81 def dfs_recursive(self, start_vertex: int, visited: list) -> None:
82 """
83 Perform a recursive depth-first search (DFS) traversal on the graph.
84
85 :param start_vertex: The starting vertex for the traversal.
86 :param visited: A list to track visited vertices.
87
88 Example:
89 >>> g = Graph()
90 >>> g.add_edge(0, 1)
91 >>> g.add_edge(0, 2)
92 >>> g.add_edge(1, 2)
93 >>> g.add_edge(2, 0)
94 >>> g.add_edge(2, 3)
95 >>> g.add_edge(3, 3)
96 >>> visited = [False] * len(g.vertex)
97 >>> g.dfs_recursive(0, visited)
98 0 1 2 3
99 """
100 # mark start vertex as visited
101 visited[start_vertex] = True
102
103 print(start_vertex, end="")
104
105 # Recur for all the vertices that are adjacent to this node
106 for i in self.vertex:
107 if not visited[i]:
108 print(" ", end="")
109 self.dfs_recursive(i, visited)
110
111
112if __name__ == "__main__":

Callers 1

dfsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected