📌  相关文章
📜  Java程序,用于查找要切割的最小边数以使图断开连接

📅  最后修改于: 2021-10-25 05:02:53             🧑  作者: Mango

给定一个连通图,任务是找到要切割/移除以使给定图断开连接的最小边数。

如果一个图至少存在两个不通过路径连接的顶点,则该图是断开连接的。

例子:

方法:

解决这个问题的方法是在图中从起始顶点到结束顶点的边不相交路径集中找到路径的数量。边不相交路径集是任何两条路径之间没有公共边的路径集。

  1. 选择一个节点作为起始节点,另一个节点作为结束节点。
  2. 从起始节点启动 BFS 并检查是否存在从起始节点到结束节点的路径。
  3. 如果是,则从该路径中删除所有边并再次运行 BFS。
  4. 重复步骤 2 和 3,直到不存在从开始节点到结束节点的路径。
  5. 返回路径被删除的次数。

举例说明:

代码:

Java
// Java Program to Find
// Minimum Number of Edges
// to Cut to make the
// Graph Disconnected
 
import java.util.*;
 
public class GFG {
 
    // Function to find the min number of edges
    public static int minEdgesRemoval(int[][] edges, int n)
    {
        // Initialize adjacency list for Graph
        Map > graph
            = new HashMap >();
 
        // Initializing starting and ending vertex
        int start = edges[0][0];
        int end = edges[0][1];
 
        // Create adjacency list of the graph
        for (int i = 0; i < n; i++) {
            int n1 = edges[i][0];
            int n2 = edges[i][1];
            List li;
 
            // Add edges node 1
            if (graph.containsKey(n1)) {
                li = graph.get(n1);
            }
            else {
                li = new ArrayList();
            }
           
            li.add(n2);
            graph.put(n1, li);
 
            // Add edges node 2
            if (graph.containsKey(n2)) {
                li = graph.get(n2);
            }
            else {
                li = new ArrayList();
            }
           
            li.add(n1);
            graph.put(n2, li);
        }
 
        // Variable to count the number of paths getting
        // deleted
        int deleteEdgeCount = 0;
       
        while (true) {
 
            // bfsTraversalPath is the BFS path from start
            // to end node It is a map of parent vertex and
            // child vertex
            Map bfsTraversalPath = bfs(graph, start);
 
            // If end is present on the path from start node
            // then delete that path and increment
            // deleteEdgeCount
            if (bfsTraversalPath.containsKey(end)) {
               
                deleteEdgeCount++;
                int parent = bfsTraversalPath.get(end);
                int currEnd = end;
               
                // Delete all the edges in the current path
                while (parent != -1)
                {
                    deleteEdge(graph, parent, currEnd);
                    deleteEdge(graph, currEnd, parent);
                    currEnd = parent;
                    parent = bfsTraversalPath.get(currEnd);
                }
            }
 
            // If end is not present in the path
            // then we have a disconnected graph.
            else {
                break;
            }
        }
 
        return deleteEdgeCount;
    }
 
    // Function to delete/remove an edge
    private static void deleteEdge(Map > graph,
               Integer start, Integer end)
    {
 
        List list = graph.get(start);
        list.remove(end);
    }
 
    // Function for BFS Path
    private static Map
    bfs(Map > graph, int start)
    {
 
        // Map for BFS Path
        Map bfsTraversalPath
            = new HashMap();
 
        // Array for marking visited vertex
        List visited = new ArrayList();
 
        // Array for BFS
        List queue = new ArrayList();
       
        int qStartIndex = 0;
       
        bfsTraversalPath.put(start, -1);
        queue.add(start);
       
        while (qStartIndex < queue.size())
        {
            int curr = queue.get(qStartIndex++);
            visited.add(curr);
           
            for (int k : graph.get(curr))
            {
                if (!visited.contains(k))
                {
                    queue.add(k);
                    if (!bfsTraversalPath.containsKey(k))
                    {
                        bfsTraversalPath.put(k, curr);
                    }
                }
            }
        }
       
        return bfsTraversalPath;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Number of edges
        int n = 7;
 
        // Edge List
        int[][] edges
            = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 },
                { 4, 0 }, { 4, 1 }, { 1, 3 } };
 
        // Run the function
        System.out.println("Minimum Number of Edges to Remove = "
            + minEdgesRemoval(edges, n));
    }
}


输出
Minimum Number of Edges to Remove = 2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程