# Minimum edges required to make a Directed Graph Strongly Connected

Given a Directed graph of N vertices and M edges, the task is to find the minimum number of edges required to make the given graph Strongly Connected.

Examples:

Input: N = 3, M = 3, source[] = {1, 2, 1}, destination[] = {2, 3, 3}
Output: 1
Explanation:
Adding a directed edge joining the pair of vertices {3, 1} makes the graph strongly connected.
Hence, the minimum number of edges required is 1.
Below is the illustration of the above example: Input: N = 6, M = 5, source[] = {1, 3, 1, 3, 5}, destination[] = {2, 2, 3, 5, 6}
Output: 3
Explanation:
Adding 3 directed edges to join the following pair of vertices makes the graph strongly connected:

• {2, 1}
• {4, 5}
• {6, 4}

Hence, the minimum number of edges required is 3.

Approach:
For a Strongly Connected Graph, each vertex must have an in-degree and an out-degree of at least 1. Therefore, in order to make a graph strongly connected, each vertex must have an incoming edge and an outgoing edge. The maximum of the number of incoming edges and the outgoing edges required to make the graph strongly connected is the minimum edges required to make it strongly connected.
Follow the steps below to solve the problem:

• Find the count of in-degrees and out-degrees of each vertex of the graph, using DFS.
• If the in-degree or out-degree of a vertex is greater than 1, then consider it as only 1.
• Count the total in-degree and out-degree of the given graph.
• The minimum number of edges required to make the graph strongly connected is then given by max(N-totalIndegree, N-totalOutdegree).
• Print the count of minimum edges as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement ` `// the above appraoch ` `#include ` `using` `namespace` `std; ` ` `  `// Perform DFS to count the in-degree ` `// and out-degree of the graph ` `void` `dfs(``int` `u, vector<``int``> adj[], ` `         ``int``* vis, ``int``* inDeg, ``int``* outDeg) ` `{ ` `    ``// Mark the source as visited ` `    ``vis[u] = 1; ` ` `  `    ``// Mark in-degree as 1 ` `    ``inDeg[u] = 1; ` ` `  `    ``// Traversing adjacent nodes ` `    ``for` `(``auto` `v : adj[u]) { ` ` `  `        ``// If not visited ` `        ``if` `(vis[v] == 0) { ` ` `  `            ``// Mark out-degree as 1 ` `            ``outDeg[u] = 1; ` ` `  `            ``// DFS Traversal on ` `            ``// adjacent vertex ` `            ``dfs(v, adj, vis, ` `                ``inDeg, outDeg); ` `        ``} ` `    ``} ` `} ` ` `  `// Function to return minimum number ` `// of edges required to make the graph ` `// strongly connected ` `int` `findMinimumEdges(``int` `source[], ` `                     ``int` `N, ``int` `M, ` `                     ``int` `dest[]) ` `{ ` `    ``// For Adjacency List ` `    ``vector<``int``> adj[N + 1]; ` ` `  `    ``// Create the Adjacency List ` `    ``for` `(``int` `i = 0; i < M; i++) { ` ` `  `        ``adj].push_back(dest[i]); ` `    ``} ` ` `  `    ``// Initialize the in-degree array ` `    ``int` `inDeg[N + 1] = { 0 }; ` ` `  `    ``// Initialize the out-degree array ` `    ``int` `outDeg[N + 1] = { 0 }; ` ` `  `    ``// Initialize the visited array ` `    ``int` `vis[N + 1] = { 0 }; ` ` `  `    ``// Perform DFS to count in-degrees ` `    ``// and out-degreess ` `    ``dfs(1, adj, vis, inDeg, outDeg); ` ` `  `    ``// To store the result ` `    ``int` `minEdges = 0; ` ` `  `    ``// To store total count of in-degree ` `    ``// and out-degree ` `    ``int` `totalIndegree = 0; ` `    ``int` `totalOutdegree = 0; ` ` `  `    ``// Find total in-degree ` `    ``// and out-degree ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` ` `  `        ``if` `(inDeg[i] == 1) ` `            ``totalIndegree++; ` `        ``if` `(outDeg[i] == 1) ` `            ``totalOutdegree++; ` `    ``} ` ` `  `    ``// Calculate the minimum ` `    ``// edges required ` `    ``minEdges = max(N - totalIndegree, ` `                   ``N - totalOutdegree); ` ` `  `    ``// Return the minimum edges ` `    ``return` `minEdges; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 6, M = 5; ` ` `  `    ``int` `source[] = { 1, 3, 1, 3, 5 }; ` `    ``int` `destination[] = { 2, 2, 3, 5, 6 }; ` ` `  `    ``cout << findMinimumEdges(source, N, M, ` `                             ``destination); ` `    ``return` `0; ` `} `

Output:

```3
```

Time Complexity: O(N + M)
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.