Given an undirected graph consisting of N valued over the range [1, N] such that vertices (i, i + 1) are connected and an array arr[] consisting of M pair of integers, the task is to find the minimum number of edges that should be removed from the graph such that there doesn’t exist any path for every pair (u, v) in the array arr[].
Examples:
Input: arr[] = {{1, 4}, {2, 5}
Output: 1
Explanation:
For N = 5, the given graph can be represented as:1 <-> 2 <-> 3 <-> 4 <-> 5
After removing the edge between vertices 2 and 3 or the edge between vertices 3 and 4, the graph modifies to:
1 <-> 2 3 <-> 4 <-> 5
Now, there doesn’t exist any path between every pair of nodes in the array. Therefore, the minimum number of edges that should be removed is 1.
Input: arr[] = {{1, 8}, {2, 7}, {3, 5}, {4, 6}, {7, 9}}
Output: 2
Approach: The given problem can be solved using a Greedy Approach. The idea is to sort the given array of pairs arr[] in increasing order of ending pairs and for each pair, say (u, v) remove the nearest edges connected to the vertex v so that all the other vertices on the connected components containing vertex v is unreachable. Follow the steps below to solve the given problem:
- Create a variable, say minEdges as 0, that stores the count of removed edges.
- Create a variable reachable as 0, that keeps track of the smallest vertex that is reachable from the last vertex i.e., N.
- Sort the given array of pairs arr[] in increasing order of the second value of the pairs.
- Traverse the given array arr[] and for every pair (u, v) in arr[], if reachable > u, it implies that there exists no path between u and v, otherwise removing the last edge between (v – 1) and v is the most optimal choice. Therefore, increment the value of minEdges by 1 and the value of reachable will be equal to v.
- After completing the above steps, print the value of minEdges as result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Comparator function to sort the // given array of pairs in increasing // order of second value of the pairs bool comp(pair< int , int > a, pair< int , int > b) { if (a.second < b.second) { return true ; } return false ; } // Function to find minimum number of edges // to be removed such that there exist no // path between each pair in the array arr[] int findMinEdges(vector<pair< int , int > > arr, int N) { // Stores the count of edges to be deleted int minEdges = 0; // Stores the smallest vertex reachable // from the current vertex int reachable = 0; // Sort the array arr[] in increasing // order of the second value of the pair sort(arr.begin(), arr.end(), comp); // Loop to iterate through array arr[] for ( int i = 0; i < arr.size(); i++) { // If reachable > arr[i].first, there // exist no path between arr[i].first // and arr[i].second, hence continue if (reachable > arr[i].first) continue ; else { // Update the reachable vertex reachable = arr[i].second; // Increment minEdges by 1 minEdges++; } } // Return answer return minEdges; } // Driver Code int main() { vector<pair< int , int > > arr = { { 1, 8 }, { 2, 7 }, { 3, 5 }, { 4, 6 }, { 7, 9 } }; int N = arr.size(); cout << findMinEdges(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.Arrays; class GFG { // Comparator function to sort the // given array of pairs in increasing // order of second value of the pairs // Function to find minimum number of edges // to be removed such that there exist no // path between each pair in the array arr[] public static int findMinEdges( int [][] arr, int N) { // Stores the count of edges to be deleted int minEdges = 0 ; // Stores the smallest vertex reachable // from the current vertex int reachable = 0 ; // Sort the array arr[] in increasing // order of the second value of the pair Arrays.sort(arr, (a, b) -> (a[ 1 ] - b[ 1 ])); // Loop to iterate through array arr[] for ( int i = 0 ; i < arr.length; i++) { // If reachable > arr[i][0], there // exist no path between arr[i][0] // and arr[i][1], hence continue if (reachable > arr[i][ 0 ]) continue ; else { // Update the reachable vertex reachable = arr[i][ 1 ]; // Increment minEdges by 1 minEdges++; } } // Return answer return minEdges; } // Driver Code public static void main(String args[]) { int [][] arr = { { 1 , 8 }, { 2 , 7 }, { 3 , 5 }, { 4 , 6 }, { 7 , 9 } }; int N = arr.length; System.out.println(findMinEdges(arr, N)); } } // This code is contributed by _saurabh_jaiswal. |
Python3
# Python 3 program for the above approach # Comparator function to sort the # given array of pairs in increasing # order of second value of the pairs # Function to find minimum number of edges # to be removed such that there exist no # path between each pair in the array arr[] def findMinEdges(arr, N): # Stores the count of edges to be deleted minEdges = 0 # Stores the smallest vertex reachable # from the current vertex reachable = 0 # Sort the array arr[] in increasing # order of the second value of the pair arr.sort() # Loop to iterate through array arr[] for i in range ( len (arr)): # If reachable > arr[i].first, there # exist no path between arr[i].first # and arr[i].second, hence continue if (reachable > arr[i][ 0 ]): continue else : # Update the reachable vertex reachable = arr[i][ 1 ] # Increment minEdges by 1 minEdges + = 1 # Return answer return minEdges + 1 # Driver Code if __name__ = = '__main__' : arr = [[ 1 , 8 ],[ 2 , 7 ],[ 3 , 5 ],[ 4 , 6 ],[ 7 , 9 ]] N = len (arr) print (findMinEdges(arr, N)) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; using System.Linq; using System.Collections.Generic; public class GFG { // Comparator function to sort the // given array of pairs in increasing // order of second value of the pairs // Function to find minimum number of edges // to be removed such that there exist no // path between each pair in the array []arr public static int findMinEdges(List<List< int >> arr, int N) { // Stores the count of edges to be deleted int minEdges = 0; // Stores the smallest vertex reachable // from the current vertex int reachable = 0; // Sort the array []arr in increasing // order of the second value of the pair arr.Sort((a, b) => a[1] - b[1]); // Loop to iterate through array []arr for ( int i = 0; i < arr.Count; i++) { // If reachable > arr[i,0], there // exist no path between arr[i,0] // and arr[i,1], hence continue if (reachable > arr[i][0]) continue ; else { // Update the reachable vertex reachable = arr[i][1]; // Increment minEdges by 1 minEdges++; } } // Return answer return minEdges; } // Driver Code public static void Main(String []args) { int [,] arr = { { 1, 8 }, { 2, 7 }, { 3, 5 }, { 4, 6 }, { 7, 9 } }; List<List< int >> arr1 = new List<List< int >>(); for ( int i = 0;i<arr.GetLength(0);i++){ List< int > arr2 = new List< int >(); arr2.Add(arr[i,0]); arr2.Add(arr[i,1]); arr1.Add(arr2); } int N = arr.GetLength(0); Console.WriteLine(findMinEdges(arr1, N)); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to find minimum number of edges // to be removed such that there exist no // path between each pair in the array arr[] function findMinEdges(arr, N) { // Stores the count of edges to be deleted let minEdges = 0; // Stores the smallest vertex reachable // from the current vertex let reachable = 0; // Sort the array arr[] in increasing // order of the second value of the pair arr.sort( function (a, b) { return a.second - b.second }) // Loop to iterate through array arr[] for (let i = 0; i < arr.length; i++) { // If reachable > arr[i].first, there // exist no path between arr[i].first // and arr[i].second, hence continue if (reachable > arr[i].first) continue ; else { // Update the reachable vertex reachable = arr[i].second; // Increment minEdges by 1 minEdges++; } } // Return answer return minEdges; } // Driver Code let arr = [{first: 1, second: 8}, { first: 2, second: 7 }, { first: 3, second: 5 }, { first: 4, second: 6 }, { first: 7, second: 9 }]; let N = arr.length; document.write(findMinEdges(arr, N)); // This code is contributed by Potta Lokesh </script> |
2
Time Complexity: O(M*log M)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!