app/src/main/java/org/jgrapht/alg/StoerWagnerMinimumCut.java
protected void minimumCutPhase(Set<V> a)
    {
        // The last and before last vertices added to A.
        Set<V> last = a, beforelast = null;

        // queue contains vertices not in A ordered by max weight of edges to A.
        PriorityQueue<VertexAndWeight> queue =
            new PriorityQueue<VertexAndWeight>();

        // Maps vertices to elements of queue
        Map<Set<V>, VertexAndWeight> dmap =
            new HashMap<Set<V>, VertexAndWeight>();

        // Initialize queue
        for (Set<V> v : workingGraph.vertexSet()) {
            if (v == a) {
                continue;
            }
            DefaultWeightedEdge e = workingGraph.getEdge(v, a);
            Double w = (e == null) ? 0.0 : workingGraph.getEdgeWeight(e);
            VertexAndWeight vandw = new VertexAndWeight(v, w, e != null);
            queue.add(vandw);
            dmap.put(v, vandw);
        }

        // Now iteratively update the queue to get the required vertex ordering

        while (!queue.isEmpty()) {
            Set<V> v = queue.poll().vertex;
            dmap.remove(v);

            beforelast = last;
            last = v;

            for (DefaultWeightedEdge e : workingGraph.edgesOf(v)) {
                Set<V> vc = Graphs.getOppositeVertex(workingGraph, e, v);
                VertexAndWeight vcandw = dmap.get(vc);
                if (vcandw != null) {
                    queue.remove(vcandw); //this is O(logn) but could be O(1)?
                    vcandw.active = true;
                    vcandw.weight += workingGraph.getEdgeWeight(e);
                    queue.add(vcandw); //this is O(logn) but could be O(1)?
                }
            }
        }

        // Update the best cut
        double w = vertexWeight(last);
        if (w < bestCutWeight) {
            bestCutWeight = w;
            bestCut = last;
        }

        //merge the last added vertices
        mergeVertices(beforelast, last);
    }
/**
     * Implements the MinimumCutPhase function of Stoer and Wagner
     */
Implements the MinimumCutPhase function of Stoer and Wagner
Implements the MinimumCutPhase function of Stoer and Wagner
protected void minimumCutPhase(Set<V> a)
    {
        Set<V> last = a, beforelast = null;
        PriorityQueue<VertexAndWeight> queue =
            new PriorityQueue<VertexAndWeight>();
        Map<Set<V>, VertexAndWeight> dmap =
            new HashMap<Set<V>, VertexAndWeight>();
        for (Set<V> v : workingGraph.vertexSet()) {
            if (v == a) {
                continue;
            }
            DefaultWeightedEdge e = workingGraph.getEdge(v, a);
            Double w = (e == null) ? 0.0 : workingGraph.getEdgeWeight(e);
            VertexAndWeight vandw = new VertexAndWeight(v, w, e != null);
            queue.add(vandw);
            dmap.put(v, vandw);
        }
        while (!queue.isEmpty()) {
            Set<V> v = queue.poll().vertex;
            dmap.remove(v);
            beforelast = last;
            last = v;
            for (DefaultWeightedEdge e : workingGraph.edgesOf(v)) {
                Set<V> vc = Graphs.getOppositeVertex(workingGraph, e, v);
                VertexAndWeight vcandw = dmap.get(vc);
                if (vcandw != null) {
                    queue.remove(vcandw); 
                    vcandw.active = true;
                    vcandw.weight += workingGraph.getEdgeWeight(e);
                    queue.add(vcandw); 
                }
            }
        }
        double w = vertexWeight(last);
        if (w < bestCutWeight) {
            bestCutWeight = w;
            bestCut = last;
        }
        mergeVertices(beforelast, last);
    }
protected void minimumCutPhase ( Set < V > a ) { Set < V > last = a , beforelast = null ; PriorityQueue < VertexAndWeight > queue = new PriorityQueue < VertexAndWeight > ( ) ; Map < Set < V > , VertexAndWeight > dmap = new HashMap < Set < V > , VertexAndWeight > ( ) ; for ( Set < V > v : workingGraph . vertexSet ( ) ) { if ( v == a ) { continue ; } DefaultWeightedEdge e = workingGraph . getEdge ( v , a ) ; Double w = ( e == null ) ? 0.0 : workingGraph . getEdgeWeight ( e ) ; VertexAndWeight vandw = new VertexAndWeight ( v , w , e != null ) ; queue . add ( vandw ) ; dmap . put ( v , vandw ) ; } while ( ! queue . isEmpty ( ) ) { Set < V > v = queue . poll ( ) . vertex ; dmap . remove ( v ) ; beforelast = last ; last = v ; for ( DefaultWeightedEdge e : workingGraph . edgesOf ( v ) ) { Set < V > vc = Graphs . getOppositeVertex ( workingGraph , e , v ) ; VertexAndWeight vcandw = dmap . get ( vc ) ; if ( vcandw != null ) { queue . remove ( vcandw ) ; vcandw . active = true ; vcandw . weight += workingGraph . getEdgeWeight ( e ) ; queue . add ( vcandw ) ; } } } double w = vertexWeight ( last ) ; if ( w < bestCutWeight ) { bestCutWeight = w ; bestCut = last ; } mergeVertices ( beforelast , last ) ; }
Implements the MinimumCutPhase function of Stoer and Wagner
Implements the MinimumCutPhase function of Stoer and Wagner
// The last and before last vertices added to A. // queue contains vertices not in A ordered by max weight of edges to A. // Maps vertices to elements of queue // Initialize queue // Now iteratively update the queue to get the required vertex ordering //this is O(logn) but could be O(1)? //this is O(logn) but could be O(1)? // Update the best cut //merge the last added vertices