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

Examples :

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

**Explanation :**

**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.

`// CPP code to minimize the number ` `// of weakly connected nodes ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Set of nodes which are traversed ` `// in each launch of the DFS ` `set<` `int` `> node; ` `vector<` `int` `> Graph[10001]; ` ` ` `// Function traversing the graph using DFS ` `// approach and updating the set of nodes ` `void` `dfs(` `bool` `visit[], ` `int` `src) ` `{ ` ` ` `visit[src] = ` `true` `; ` ` ` `node.insert(src); ` ` ` `int` `len = Graph[src].size(); ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `if` `(!visit[Graph[src][i]]) ` ` ` `dfs(visit, Graph[src][i]); ` `} ` ` ` `// building a undirected graph ` `void` `buildGraph(` `int` `x[], ` `int` `y[], ` `int` `len){ ` ` ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` `int` `p = x[i]; ` ` ` `int` `q = y[i]; ` ` ` `Graph[p].push_back(q); ` ` ` `Graph[q].push_back(p); ` ` ` `} ` `} ` ` ` `// computes the minimum number of disconnected ` `// components when a bi-directed graph is ` `// converted to a undirected graph ` `int` `compute(` `int` `n) ` `{ ` ` ` `// Declaring and initializing ` ` ` `// a visited array ` ` ` `bool` `visit[n + 5]; ` ` ` `memset` `(visit, ` `false` `, ` `sizeof` `(visit)); ` ` ` `int` `number_of_nodes = 0; ` ` ` ` ` `// We check if each node is ` ` ` `// visited once or not ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// We only launch DFS from a ` ` ` `// node iff it is unvisited. ` ` ` `if` `(!visit[i]) { ` ` ` ` ` `// Clearing the set of nodes ` ` ` `// on every relaunch of DFS ` ` ` `node.clear(); ` ` ` ` ` `// relaunching DFS from an ` ` ` `// unvisited node. ` ` ` `dfs(visit, i); ` ` ` ` ` `// iterating over the node set to count the ` ` ` `// number of nodes visited after making the ` ` ` `// graph directed and storing it in the ` ` ` `// variable count. If count / 2 == number ` ` ` `// of nodes - 1, then increment count by 1. ` ` ` `int` `count = 0; ` ` ` `for` `(` `auto` `it = node.begin(); it != node.end(); ++it) ` ` ` `count += Graph[(*it)].size(); ` ` ` ` ` `count /= 2; ` ` ` `if` `(count == node.size() - 1) ` ` ` `number_of_nodes++; ` ` ` `} ` ` ` `} ` ` ` `return` `number_of_nodes; ` `} ` ` ` `//Driver function ` `int` `main() ` `{ ` ` ` `int` `n = 6,m = 4; ` ` ` `int` `x[m + 5] = {1, 1, 4, 4}; ` ` ` `int` `y[m+5] = {2, 3, 5, 6}; ` ` ` ` ` `/*For given x and y above, graph is as below : ` ` ` `1-----2 4------5 ` ` ` `| | ` ` ` `| | ` ` ` `| | ` ` ` `3 6 ` ` ` ` ` `// Note : This code will work for ` ` ` `// connected graph also as : ` ` ` `1-----2 ` ` ` `| | \ ` ` ` `| | \ ` ` ` `| | \ ` ` ` `3-----4----5 ` ` ` `*/` ` ` ` ` `// Building graph in the form of a adjacency list ` ` ` `buildGraph(x, y, n); ` ` ` `cout << compute(n) << ` `" weakly connected nodes"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

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.

## Recommended Posts:

- Check if a graph is Strongly, Unilaterally or Weakly connected
- Convert undirected connected graph to strongly connected directed graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Print levels with odd number of nodes and even number of nodes
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Maximum number of edges among all connected components of an undirected graph
- Number of connected components in a 2-D matrix of strings
- Number of ways to select a node from each connected component
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Program to count Number of connected components in an undirected graph
- Check if the length of all connected components is a Fibonacci number
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Strongly Connected Components
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Tarjan's Algorithm to find Strongly Connected Components
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Cycles of length n in an undirected and connected graph
- Largest connected component on a grid
- Sum of the minimum elements in all connected components of an undirected graph

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.