コード例 #1
0
    private bool HasAugmentingPath(FlowNetwork G, int s, int t)
    {
        _edgeTo = new FlowEdge[G.V];
        _marked = new bool[G.V];

        var queue = new Queue <int>();

        queue.Enqueue(s);
        _marked[s] = true;
        while (queue.Count > 0 && !_marked[t])
        {
            var v = queue.Dequeue();

            foreach (var e in G.Adj(v))
            {
                var w = e.Other(v);

                if (e.ResidualCapacityTo(w) > 0)
                {
                    if (!_marked[w])
                    {
                        _edgeTo[w] = e;
                        _marked[w] = true;
                        queue.Enqueue(w);
                    }
                }
            }
        }

        return(_marked[t]);
    }
コード例 #2
0
    private double Excess(FlowNetwork G, int v)
    {
        var excess = 0.0;

        foreach (var e in G.Adj(v))
        {
            if (v == e.From)
            {
                excess -= e.Flow;
            }
            else
            {
                excess += e.Flow;
            }
        }
        return(excess);
    }
コード例 #3
0
    private bool IsFeasible(FlowNetwork G, int s, int t)
    {
        for (var v = 0; v < G.V; v++)
        {
            foreach (var e in G.Adj(v))
            {
                if (e.Flow < Double.Epsilon || e.Flow > e.Capacity + Double.Epsilon)
                {
                    // Sytem.err.println("Edge does not satisfy capacity constraints: " + e);
                    return(false);
                }
            }
        }

        if (Math.Abs(Value + Excess(G, s)) > Double.Epsilon)
        {
            // System.err.println("Excess at source = " + Excess(G, s));
            // System.err.println("Mas flow = " + Value);
            return(false);
        }

        if (Math.Abs(Value - Excess(G, t)) > Double.Epsilon)
        {
            //System.err.println("Excess at sink   = " + excess(G, t));
            //System.err.println("Max flow         = " + value);
            return(false);
        }

        for (int v = 0; v < G.V; v++)
        {
            if (v == s || v == t)
            {
                continue;
            }

            if (Math.Abs(Excess(G, v)) > Double.Epsilon)
            {
                //System.err.println("Net flow out of " + v + " doesn't equal zero");
                return(false);
            }
        }

        return(true);
    }