Java JUNG EdmondsKarpMaxFlow陷入无限循环中

问题描述 投票:0回答:1

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

然而,当我在一个更复杂的图上调用相同的 "edkAlg.evaluation() "代码时,循环每次都会在某个边缘迭代时停止。

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");
    }
}

我使用了自定义的Vertices和Edges定义,这些定义的行为符合预期,但只是比琐碎的例子有更多的属性。这段代码在最初的201次迭代中完美地找到了v1和v2之间的最大流量,但每次迭代后都卡在'.evaluate()'中(它每次使用相同的对的顺序,所以总是卡在problemNode123 -> problemNode456上)。不太清楚我哪里出错了,网上也没有太多帮助,所以很感谢任何指点!

java algorithm infinite-loop jung edmonds-karp
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.