1. DFS #

Created Monday 30 March 2020

Algorithm: We implement the graph as an adjacency matrix:

  1. We ask the user number of vertices. We store this as n. Now we assume that the vertices are named from 0 to n-1.
  2. We ask for how many edges. The user inputs the edge pairs. We mark (a,b) and (b,a) as true for all inputs ‘a’ and ‘b’.
  3. We go through all vertices, calling DFS from it if they are absent from visited, at the same time checking if visited.size()<V. This ensures that disconnected components are also taken into account.

Insights/Takeaways:


Efficiency T.C = O(V+E)** if we use hashmap as visited array.** S.C = O(V)


Implementation tips

DFS code


Applications