Minimum number of edges to be removed from given Graph such that no path exists between given pairs of vertices
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++
#include <bits/stdc++.h>
using namespace std;
bool comp(pair< int , int > a, pair< int , int > b)
{
if (a.second < b.second) {
return true ;
}
return false ;
}
int findMinEdges(vector<pair< int , int > > arr,
int N)
{
int minEdges = 0;
int reachable = 0;
sort(arr.begin(), arr.end(), comp);
for ( int i = 0; i < arr.size(); i++) {
if (reachable > arr[i].first)
continue ;
else {
reachable = arr[i].second;
minEdges++;
}
}
return minEdges;
}
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
import java.util.Arrays;
class GFG
{
public static int findMinEdges( int [][] arr, int N)
{
int minEdges = 0 ;
int reachable = 0 ;
Arrays.sort(arr, (a, b) -> (a[ 1 ] - b[ 1 ]));
for ( int i = 0 ; i < arr.length; i++) {
if (reachable > arr[i][ 0 ])
continue ;
else {
reachable = arr[i][ 1 ];
minEdges++;
}
}
return minEdges;
}
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));
}
}
|
Python3
def findMinEdges(arr, N):
minEdges = 0
reachable = 0
arr.sort()
for i in range ( len (arr)):
if (reachable > arr[i][ 0 ]):
continue
else :
reachable = arr[i][ 1 ]
minEdges + = 1
return minEdges + 1
if __name__ = = '__main__' :
arr = [[ 1 , 8 ],[ 2 , 7 ],[ 3 , 5 ],[ 4 , 6 ],[ 7 , 9 ]]
N = len (arr)
print (findMinEdges(arr, N))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG {
public static int findMinEdges(List<List< int >> arr, int N) {
int minEdges = 0;
int reachable = 0;
arr.Sort((a, b) => a[1] - b[1]);
for ( int i = 0; i < arr.Count; i++) {
if (reachable > arr[i][0])
continue ;
else
{
reachable = arr[i][1];
minEdges++;
}
}
return minEdges;
}
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));
}
}
|
Javascript
<script>
function findMinEdges(arr,
N)
{
let minEdges = 0;
let reachable = 0;
arr.sort( function (a, b) { return a.second - b.second })
for (let i = 0; i < arr.length; i++) {
if (reachable > arr[i].first)
continue ;
else {
reachable = arr[i].second;
minEdges++;
}
}
return minEdges;
}
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));
</script>
|
Time Complexity: O(M*log M)
Auxiliary Space: O(1)
Last Updated :
16 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...