technojam / Ultimate_Algorithms_Repository

This is a collection of Data Structures and Algorithms
83 stars 166 forks source link

Depth First Search will not work if the graph is disconnected #198

Closed nprakhar0411 closed 4 years ago

nprakhar0411 commented 4 years ago

The algorithm provided in DepthFirstSearch.cpp assumes the the graph is connected and we traverse all vertices on a single call to vertex numbered 1. Correct way should be: for(int i = 1; i <= nodes; ++i){ if(!visited[i]){ dfs(i); } }