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 <bits/stdc++.h> 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; } |
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.
Recommended Posts:
- Convert undirected connected graph to strongly connected directed graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Path with minimum XOR sum of edges in a directed graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Minimum edges required to add to make Euler Circuit
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Check if a directed graph is connected or not
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- All vertex pairs connected with exactly k edges in a graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path with exactly k edges in a directed and weighted graph
- Assign directions to edges so that the directed graph remains acyclic
- Maximum number of edges among all connected components of an undirected graph
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Strongly Connected Components
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of edges between two vertices of a Graph
- Sum of the minimum elements in all connected components of an undirected graph
- Minimum Cost Path in a directed graph via given set of intermediate nodes
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.

