/** * Calculates the minimum cut in the graph, that is, the minimum cut over all $s-t$ pairs. The * same result can be obtained with the {@link org.jgrapht.alg.StoerWagnerMinimumCut} * implementation. After invoking this method, the source/sink partitions corresponding to the * minimum cut can be queried through the {@link #getSourcePartition()} and * {@link #getSinkPartition()} methods. After computing the Gomory-Hu Cut tree, this method runs * in $O(N)$ time. * * @return weight of the minimum cut in the graph */ public double calculateMinCut() { if (this.gomoryHuTree == null) this.gomoryHuTree = this.getGomoryHuTree(); DefaultWeightedEdge cheapestEdge = gomoryHuTree .edgeSet().stream().min(Comparator.comparing(gomoryHuTree::getEdgeWeight)) .orElseThrow(() -> new RuntimeException("graph is empty?!")); lastInvokedSource = gomoryHuTree.getEdgeSource(cheapestEdge); lastInvokedTarget = gomoryHuTree.getEdgeTarget(cheapestEdge); sourcePartitionLastInvokedSource = null; return gomoryHuTree.getEdgeWeight(cheapestEdge); }
@Override public Set<V> getSourcePartition() { if (sourcePartitionLastInvokedSource != null) return sourcePartitionLastInvokedSource; if (this.gomoryHuTree == null) this.gomoryHuTree = this.getGomoryHuTree(); Set<DefaultWeightedEdge> pathEdges = this.findPathBetween(gomoryHuTree, lastInvokedSource, lastInvokedTarget); DefaultWeightedEdge cheapestEdge = pathEdges.stream().min(Comparator.comparing(gomoryHuTree::getEdgeWeight)).orElseThrow( () -> new RuntimeException("path is empty?!")); // Remove the selected edge from the gomoryHuTree graph. The resulting graph consists of 2 // components V source = gomoryHuTree.getEdgeSource(cheapestEdge); V target = gomoryHuTree.getEdgeTarget(cheapestEdge); gomoryHuTree.removeEdge(cheapestEdge); // Return the vertices in the component with the source vertex sourcePartitionLastInvokedSource = new ConnectivityInspector<>(gomoryHuTree).connectedSetOf(lastInvokedSource); // Restore the internal tree structure by putting the edge back gomoryHuTree.addEdge(source, target, cheapestEdge); return sourcePartitionLastInvokedSource; }
for (DefaultWeightedEdge edge : edges) { V source = gomoryHuTree.getEdgeSource(edge); V target = gomoryHuTree.getEdgeTarget(edge); double edgeWeight = gomoryHuTree.getEdgeWeight(edge);
final Spot target = graph.getEdgeTarget( edge ); model.addEdge( source, target, graph.getEdgeWeight( edge ) );
final Spot target = graph.getEdgeTarget( edge ); model.addEdge( source, target, graph.getEdgeWeight( edge ) );
final Spot target = graph.getEdgeTarget( edge ); if ( !graph.containsEdge( source, target ) )