graphs.depth_first_search_2 =========================== .. py:module:: graphs.depth_first_search_2 .. autoapi-nested-parse:: Author: OMKAR PATHAK Attributes ---------- .. autoapisummary:: graphs.depth_first_search_2.g Classes ------- .. autoapisummary:: graphs.depth_first_search_2.Graph Module Contents --------------- .. py:class:: Graph .. py:method:: add_edge(from_vertex: int, to_vertex: int) -> None Add an edge between two vertices. :param from_vertex: The source vertex. :param to_vertex: The destination vertex. Example: >>> g = Graph() >>> g.add_edge(0, 1) >>> g.add_edge(0, 2) >>> g.print_graph() {0: [1, 2]} 0 -> 1 -> 2 .. py:method:: dfs() -> None Perform depth-first search (DFS) traversal on the graph and print the visited vertices. Example: >>> 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.dfs() 0 1 2 3 .. py:method:: dfs_recursive(start_vertex: int, visited: list) -> None 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, 2) >>> g.add_edge(1, 2) >>> g.add_edge(2, 0) >>> g.add_edge(2, 3) >>> g.add_edge(3, 3) >>> visited = [False] * len(g.vertex) >>> g.dfs_recursive(0, visited) 0 1 2 3 .. py:method:: print_graph() -> None Print the graph vertices. Example: >>> 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]} 0 -> 1 -> 2 1 -> 2 2 -> 0 -> 3 3 -> 3 .. py:attribute:: vertex .. py:data:: g