Graph Theory37 sections · 1633 units
Open in Course

How to Call It

Starting the engine

In your main function, first build the graph by reading edges and populating the adjacency list. Initialize the visited array to all false. Pick a starting vertex (usually vertex 11 or vertex 00 depending on the problem). Call dfs on that starting vertex. If the graph is disconnected, you might need to call dfs multiple times to visit all components.

The DFS will explore everything reachable from the starting point. For a fully connected graph, one call is enough. For disconnected graphs, loop through all vertices and start DFS from any unvisited vertex.