当前位置: 首页 > 知识库问答 >
问题:

修改Java福特福尔克森的实现,以打印在最大流量解决方案的每一个边缘使用的最大流量?

祁飞翰
2023-03-14

我想修改Ford-Fulkerson算法的这个实现(也发布在下面),这样我就可以绘制节点图并分析数据。我不仅要输出最大流,而且要输出每个边的最大流,例如,如果最大流是50,并且它使用了从节点1到3的值为10的流,我想以1-3-10或类似的格式打印它。我试过打印u和v,然后打印剩余的[u][v],但看起来不对。有什么想法吗?

// Java program for implementation of Ford Fulkerson algorithm 
import java.util.*; 
import java.lang.*; 
import java.io.*; 
import java.util.LinkedList; 

class MaxFlow 
{ 
    static final int V = 6; //Number of vertices in graph 

    /* Returns true if there is a path from source 's' to sink 
    't' in residual graph. Also fills parent[] to store the 
    path */
    boolean bfs(int rGraph[][], int s, int t, int parent[]) 
    { 
        // Create a visited array and mark all vertices as not 
        // visited 
        boolean visited[] = new boolean[V]; 
        for(int i=0; i<V; ++i) 
            visited[i]=false; 

        // Create a queue, enqueue source vertex and mark 
        // source vertex as visited 
        LinkedList<Integer> queue = new LinkedList<Integer>(); 
        queue.add(s); 
        visited[s] = true; 
        parent[s]=-1; 

        // Standard BFS Loop 
        while (queue.size()!=0) 
        { 
            int u = queue.poll(); 

            for (int v=0; v<V; v++) 
            { 
                if (visited[v]==false && rGraph[u][v] > 0) 
                { 
                    queue.add(v); 
                    parent[v] = u; 
                    visited[v] = true; 
                } 
            } 
        } 

        // If we reached sink in BFS starting from source, then 
        // return true, else false 
        return (visited[t] == true); 
    } 

    // Returns tne maximum flow from s to t in the given graph 
    int fordFulkerson(int graph[][], int s, int t) 
    { 
        int u, v; 

        // Create a residual graph and fill the residual graph 
        // with given capacities in the original graph as 
        // residual capacities in residual graph 

        // Residual graph where rGraph[i][j] indicates 
        // residual capacity of edge from i to j (if there 
        // is an edge. If rGraph[i][j] is 0, then there is 
        // not) 
        int rGraph[][] = new int[V][V]; 

        for (u = 0; u < V; u++) 
            for (v = 0; v < V; v++) 
                rGraph[u][v] = graph[u][v]; 

        // This array is filled by BFS and to store path 
        int parent[] = new int[V]; 

        int max_flow = 0; // There is no flow initially 

        // Augment the flow while tere is path from source 
        // to sink 
        while (bfs(rGraph, s, t, parent)) 
        { 
            // Find minimum residual capacity of the edhes 
            // along the path filled by BFS. Or we can say 
            // find the maximum flow through the path found. 
            int path_flow = Integer.MAX_VALUE; 
            for (v=t; v!=s; v=parent[v]) 
            { 
                u = parent[v]; 
                path_flow = Math.min(path_flow, rGraph[u][v]); 
            } 

            // update residual capacities of the edges and 
            // reverse edges along the path 
            for (v=t; v != s; v=parent[v]) 
            { 
                u = parent[v]; 
                rGraph[u][v] -= path_flow; 
                rGraph[v][u] += path_flow; 
            } 

            // Add path flow to overall flow 
            max_flow += path_flow; 
        } 

        // Return the overall flow 
        return max_flow; 
    } 

    // Driver program to test above functions 
    public static void main (String[] args) throws java.lang.Exception 
    { 
        // Let us create a graph shown in the above example 
        int graph[][] =new int[][] { {0, 16, 13, 0, 0, 0}, 
                                    {0, 0, 10, 12, 0, 0}, 
                                    {0, 4, 0, 0, 14, 0}, 
                                    {0, 0, 9, 0, 0, 20}, 
                                    {0, 0, 0, 7, 0, 4}, 
                                    {0, 0, 0, 0, 0, 0} 
                                }; 
        MaxFlow m = new MaxFlow(); 

        System.out.println("The maximum possible flow is " + 
                        m.fordFulkerson(graph, 0, 5)); 

    } 
} 


共有1个答案

凤经武
2023-03-14

每当您沿着增大路径流动时,您就会用流动值增加rgraph中的每个边(并用相同的值减小其反向)。由于rgraph被初始化为graph的值,这意味着您可以通过取graph[u][v]-rgraph[u][v]中的正条目来获得您想要的东西。对于您的示例,结果是

0 12 11 0 0 0
-12 0 0 12 0 0
-11 0 0 0 11 0
0 -12 0 0 -7 19
0 0 -11 7 0 4
0 0 0 -19 -4 0

这与CLRS图中的结果相匹配。第26.6页。727,在此示例似乎有其起源。

 类似资料:
  • 我试着解决一个关于最大流量问题的问题。我有一个源和两个汇。我需要在这个网络中找到一个最大流量。这部分是一般最大流量。然而,在这种特殊的最大流问题中,两个目标必须得到相同的流量。 有没有人能帮助我,我该怎么做呢?

  • 如何确定流中对象的不同属性的最小值和最大值?我已经看到了关于如何得到同一变量的最小值和最大值的答案。我还看到了关于如何使用特定对象属性(例如)获取最小值或最大值的答案。但是我如何获得流中所有“x”属性的最小值和所有“y”属性的最大值呢? 假设我有一个Java

  • 用餐问题: 几家人一起出去吃饭。为了增加他们的社会交往,他们愿意坐在桌子上,这样同一家庭的两个成员就不会在同一张桌子上。假设晚餐特遣队有家庭,而家庭有成员。另外,假设有桌可用,并且第1桌的座位容量为。 问题是:我们可以坐在桌子上的最多人数是多少? 编辑:创建一个图并运行最大流算法可以解决这个问题。但如果我们用Dinic算法有2*10^3个顶点,则全局复杂度为O(10^6*10^6)=O(10^12

  • 问题内容: 最大跌幅是量化金融中用于评估已经历的最大负收益的常见风险度量。 最近,我变得不耐烦使用循环方法来计算最大跌幅。 我熟悉一个普遍的看法,即向量化的解决方案会更好。 问题是: 我可以将这个问题向量化吗? 这个解决方案是什么样的? 有什么好处? 编辑 我将亚历山大的答案修改为以下功能: 问题答案: 假定是收益的数据框架,其中每一列是单独的策略/经理/安全性,而每一行是一个新日期(例如,每月或

  • 我已经成功地实现了Bellman-Ford,当边具有负权重/距离时,找到最短路径的距离。我无法让它返回所有最短路径(当最短路径有联系时)。我设法用Dijkstra获得所有最短的路径(给定的一对节点之间)。贝尔曼-福特有可能吗?(只是想知道我是否在浪费时间)

  • 我试图使优化版的贝尔曼福特算法在最坏的情况下运行。优化版我的意思是,如果在放松1轮边后,在最短距离上没有进一步更新,则终止。 例如,一个具有7个顶点的简单连通加权有向图,使得从源顶点0运行优化贝尔曼-福特算法至少使用5轮来获得正确的最短路径。 图表不能包含负权重循环。i、 e.处理顶点0的所有传出边,然后处理顶点1的边,依此类推 我知道这和周期有关。但我不太确定绘制图表的策略是否符合要求。