【问题标题】:Java JUNG EdmondsKarpMaxFlow getting stuck in infinite loopJava JUNG EdmondsKarpMaxFlow 陷入无限循环
【发布时间】:2020-05-28 18:29:59
【问题描述】:

我正在尝试使用 JUNG 的 EdmondsKarpMaxFlow 对象来查找有向图中所有节点对之间的最大流量。我创建了一个简单的有向图,并在每个节点组合上运行它,没有错误。这是供参考的工作示例: https://pastebin.com/TLsEduxZ

但是,当我在更复杂的图形上调用相同的“edkAlg.evaluate()”代码时,循环每次都会在某个边/迭代处停止。

public class SomeClass{
...
...
    MyEdmondsKarpMaxFlow edk = new MyEdmondsKarpMaxFlow(dirGraph);
    edk.runEdk();
}

public class MyEdmondsKarpMaxFlow {

    int edgeFlowMapId = 0;
    protected DirectedSparseMultigraph<GraphElements.MyVertex, GraphElements.MyEdge> dirGraph;
    protected Map<GraphElements.MyEdge, Double> edgeFlowMap = new HashMap<GraphElements.MyEdge, Double>();

    protected Transformer<GraphElements.MyEdge, Double> capTransformer = new Transformer<GraphElements.MyEdge, Double>() {
        public Double transform(GraphElements.MyEdge edge) {
            return edge.getCapacity();
        }
    };

    // This Factory produces new edges for use by the algorithm
    protected Factory<GraphElements.MyEdge> edgeFactory = new Factory<GraphElements.MyEdge>() {
        public GraphElements.MyEdge create() {
           return new GraphElements.MyEdge(Integer.toString(edgeFlowMapId++));
        }
    };

    public MyEdmondsKarpMaxFlow(DirectedSparseMultigraph<GraphElements.MyVertex, GraphElements.MyEdge> dirGraph) {
        this.dirGraph = dirGraph;
    }

    public void runEdk() {
        Collection<GraphElements.MyVertex> vertexCollection = dirGraph.getVertices();

        for (Iterator iterator1 = vertexCollection.iterator(); iterator1.hasNext(); ) {
            GraphElements.MyVertex v1 = (GraphElements.MyVertex) iterator1.next();
            Collection<GraphElements.MyVertex> vertexCollection2 = dirGraph.getVertices();

            for (Iterator iterator2 = vertexCollection2.iterator(); iterator2.hasNext(); ) {
                GraphElements.MyVertex v2 = (GraphElements.MyVertex) iterator2.next();
                if (v1.equals(v2)) continue;
                EdmondsKarpMaxFlow<GraphElements.MyVertex, GraphElements.MyEdge> edkAlg = new EdmondsKarpMaxFlow(dirGraph, v1, v2, capTransformer, edgeFlowMap, edgeFactory);
                edkAlg.evaluate();
                System.out.println("max edk flow between v1 and v2 is : " + edkAlg.getMaxFlow());
            }
        }
        System.out.println("FIN");
    }
}

我使用顶点和边的自定义定义,它们的行为符合预期,但比简单示例具有更多属性。该代码发现 v1 和 v2 之间的最大流量在前 201 次迭代之前非常好,但每次都卡在 '.evaluate()' 中(它每次都使用相同的对顺序,所以它总是卡在 problemNode123 ->问题Node456)。不太清楚我哪里出错了,网上也没有太多帮助,所以不胜感激!

【问题讨论】:

    标签: java algorithm infinite-loop jung edmonds-karp


    【解决方案1】:

    您没有提供足够的信息来确定,但问题几乎肯定与您没有为自定义节点和边缘对象定义 hashCode()equals() 的事实有关。

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 2015-07-02
      • 2017-05-19
      • 2020-05-13
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多