📌  相关文章
📜  要从给定图形中删除的最小边数,使得给定顶点对之间不存在路径

📅  最后修改于: 2022-05-13 01:56:06.769000             🧑  作者: Mango

要从给定图中删除的最小边数,使得给定顶点对之间不存在路径

给定一个由N组成的无向图,其值在[1, N]范围内,使得顶点(i, i + 1)相连,并且一个数组arr[]M对整数组成,任务是找到应该从图中删除的边,这样数组arr[]中的每一对(u, v)都不存在任何路径。

例子:

方法:给定的问题可以使用贪心方法来解决。这个想法是按照结束对的递增顺序对给定的数组对arr[]进行排序,并且对于每一对,比如(u, v)删除连接到顶点v的最近边,以便连接组件上的所有其他顶点包含顶点v不可达。请按照以下步骤解决给定的问题:

  • 创建一个变量,例如minEdges0 ,用于存储已移除边的计数。
  • 创建一个可达的变量0 ,它跟踪从最后一个顶点(即N )可达的最小顶点。
  • 按对的第二个值的升序对给定的对数组arr[]进行排序。
  • 遍历给定数组arr[]并且对于arr [] 中的每一对(u, v) ,如果可达 > u ,则意味着uv之间不存在路径,否则删除(v – 1)v是最优选择。因此,将minEdges的值加1reachable的值将等于v
  • 完成上述步骤后,打印minEdges的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Comparator function to sort the
// given array of pairs in increasing
// order of second value of the pairs
bool comp(pair a, pair 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 > 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 > 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> 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> arr1 = new List>();
        for(int i = 0;i arr2 = new List();
            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


输出:
2

时间复杂度: O(M*log M)
辅助空间: O(1)