Minimize the number of weakly connected nodes
Given an undirected graph, task is to find the minimum number of weakly connected nodes after converting this graph into directed one.
Weakly Connected Nodes : Nodes which are having 0 indegree(number of incoming edges).
Prerequisite : BFS traversal
Input : 4 4 0 1 1 2 2 3 3 0 Output : 0 disconnected components Input : 6 5 1 2 2 3 4 5 4 6 5 6 Output : 1 disconnected components
Approach : We find a node which helps in traversing maximum nodes in a single walk. To cover all possible paths, DFS graph traversal technique is used for this.
Do the above steps to traverse the graph. Now, iterate through graph again and check which nodes are having 0 indegree.
2 weakly connected nodes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.