コード例 #1
0
        public void MaxFlowStressTest()
        {
            for (var ph = 0; ph < 10000; ph++)
            {
                var n = Utilities.RandomInteger(2, 20);
                var m = Utilities.RandomInteger(1, 100);
                var(s, t) = Utilities.RandomPair(0, n - 1);
                if (Utilities.RandomBool())
                {
                    (s, t) = (t, s);
                }

                var fg = new FlowGraph(n);
                for (var i = 0; i < m; i++)
                {
                    var u = Utilities.RandomInteger(0, n - 1);
                    var v = Utilities.RandomInteger(0, n - 1);
                    var c = Utilities.RandomInteger(0, 10000);
                    fg.AddEdge(u, v, c);
                }

                var flow   = fg.MaxFlow(s, t);
                var dual   = 0L;
                var minCut = fg.MinCut(s);
                var flows  = new long[n];
                foreach (var edge in fg.GetEdges())
                {
                    flows[edge.From] -= edge.Flow;
                    flows[edge.To]   += edge.Flow;
                    if (minCut[edge.From] && !minCut[edge.To])
                    {
                        dual += edge.Capacity;
                    }
                }

                Assert.That(dual, Is.EqualTo(flow));
                Assert.That(flows[s], Is.EqualTo(-flow));
                Assert.That(flows[t], Is.EqualTo(flow));

                for (var i = 0; i < n; i++)
                {
                    if (i == s || i == t)
                    {
                        continue;
                    }
                    Assert.That(flows[i], Is.Zero);
                }
            }
        }
コード例 #2
0
        public void MaxFlowMinCutTest()
        {
            var fg = new FlowGraph(3);

            Assert.That(fg.AddEdge(0, 1, 2), Is.Zero);
            Assert.That(fg.AddEdge(1, 2, 1), Is.EqualTo(1));
            Assert.That(fg.MaxFlow(0, 2), Is.EqualTo(1));

            Assert.That(fg.GetEdge(0), Is.EqualTo(new FlowGraph.Edge(0, 1, 2, 1)));
            Assert.That(fg.GetEdge(1), Is.EqualTo(new FlowGraph.Edge(1, 2, 1, 1)));

            var minCut = fg.MinCut(0);

            Assert.That(minCut[0], Is.True);
            Assert.That(minCut[1], Is.True);
            Assert.That(minCut[2], Is.False);
        }
コード例 #3
0
        public void MaxFlowComplexTest()
        {
            var fg = new FlowGraph(2);

            Assert.That(fg.AddEdge(0, 1, 1), Is.Zero);
            Assert.That(fg.AddEdge(0, 1, 2), Is.EqualTo(1));
            Assert.That(fg.AddEdge(0, 1, 3), Is.EqualTo(2));
            Assert.That(fg.AddEdge(0, 1, 4), Is.EqualTo(3));
            Assert.That(fg.AddEdge(0, 1, 5), Is.EqualTo(4));
            Assert.That(fg.AddEdge(0, 0, 6), Is.EqualTo(5));
            Assert.That(fg.AddEdge(1, 1, 7), Is.EqualTo(6));
            Assert.That(fg.MaxFlow(0, 1), Is.EqualTo(15));

            Assert.That(fg.GetEdge(0), Is.EqualTo(new FlowGraph.Edge(0, 1, 1, 1)));
            Assert.That(fg.GetEdge(1), Is.EqualTo(new FlowGraph.Edge(0, 1, 2, 2)));
            Assert.That(fg.GetEdge(2), Is.EqualTo(new FlowGraph.Edge(0, 1, 3, 3)));
            Assert.That(fg.GetEdge(3), Is.EqualTo(new FlowGraph.Edge(0, 1, 4, 4)));
            Assert.That(fg.GetEdge(4), Is.EqualTo(new FlowGraph.Edge(0, 1, 5, 5)));

            var minCut = fg.MinCut(0);

            Assert.That(minCut[0], Is.True);
            Assert.That(minCut[1], Is.False);
        }