graphs.depth_first_search_2

Author: OMKAR PATHAK

Attributes

g

Classes

Graph

Module Contents

class graphs.depth_first_search_2.Graph
add_edge(from_vertex: int, to_vertex: int) None

Add an edge between two vertices.

Parameters:
  • from_vertex – The source vertex.

  • 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

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

dfs_recursive(start_vertex: int, visited: list) None

Perform a recursive depth-first search (DFS) traversal on the graph.

Parameters:
  • start_vertex – The starting vertex for the traversal.

  • 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

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

vertex
graphs.depth_first_search_2.g