Пример #1
0
        public void NotSimple()
        {
            var g = new MfGraphInt(2);

            g.AddEdge(0, 1, 1).Should().Be(0);
            g.AddEdge(0, 1, 2).Should().Be(1);
            g.AddEdge(0, 1, 3).Should().Be(2);
            g.AddEdge(0, 1, 4).Should().Be(3);
            g.AddEdge(0, 1, 5).Should().Be(4);
            g.AddEdge(0, 0, 6).Should().Be(5);
            g.AddEdge(1, 1, 7).Should().Be(6);
            g.Flow(0, 1).Should().Be(15);

            MfGraphInt.Edge e;
            e = new MfGraphInt.Edge(0, 1, 1, 1);
            g.GetEdge(0).Should().Be(e);
            e = new MfGraphInt.Edge(0, 1, 2, 2);
            g.GetEdge(1).Should().Be(e);
            e = new MfGraphInt.Edge(0, 1, 3, 3);
            g.GetEdge(2).Should().Be(e);
            e = new MfGraphInt.Edge(0, 1, 4, 4);
            g.GetEdge(3).Should().Be(e);
            e = new MfGraphInt.Edge(0, 1, 5, 5);
            g.GetEdge(4).Should().Be(e);

            g.MinCut(0).Should().Equal(new[] { true, false });
        }
Пример #2
0
        public void Simple()
        {
            var g = new MfGraphInt(4);

            g.AddEdge(0, 1, 1).Should().Be(0);
            g.AddEdge(0, 2, 1).Should().Be(1);
            g.AddEdge(1, 3, 1).Should().Be(2);
            g.AddEdge(2, 3, 1).Should().Be(3);
            g.AddEdge(1, 2, 1).Should().Be(4);
            g.Flow(0, 3).Should().Be(2);

            MfGraphInt.Edge e;
            e = new MfGraphInt.Edge(0, 1, 1, 1);
            g.GetEdge(0).Should().Be(e);
            e = new MfGraphInt.Edge(0, 2, 1, 1);
            g.GetEdge(1).Should().Be(e);
            e = new MfGraphInt.Edge(1, 3, 1, 1);
            g.GetEdge(2).Should().Be(e);
            e = new MfGraphInt.Edge(2, 3, 1, 1);
            g.GetEdge(3).Should().Be(e);
            e = new MfGraphInt.Edge(1, 2, 1, 0);
            g.GetEdge(4).Should().Be(e);

            g.MinCut(0).Should().Equal(new[] { true, false, false, false });
        }
Пример #3
0
        public void Stress()
        {
            var mt = MTRandom.Create();

            for (int phase = 0; phase < 10000; phase++)
            {
                int n = mt.Next(2, 21);
                int m = mt.Next(1, 101);
                var(s, t) = mt.NextPair(0, n);
                if (mt.NextBool())
                {
                    (s, t) = (t, s);
                }

                var g = new MfGraphInt(n);
                for (int i = 0; i < m; i++)
                {
                    int u = mt.Next(0, n);
                    int v = mt.Next(0, n);
                    int c = mt.Next(0, 10001);
                    g.AddEdge(u, v, c);
                }
                int flow  = g.Flow(s, t);
                int dual  = 0;
                var cut   = g.MinCut(s);
                var vFlow = new int[n];
                foreach (var e in g.Edges())
                {
                    vFlow[e.From] -= e.Flow;
                    vFlow[e.To]   += e.Flow;
                    if (cut[e.From] && !cut[e.To])
                    {
                        dual += e.Cap;
                    }
                }
                dual.Should().Be(flow);
                vFlow[s].Should().Be(-flow);
                vFlow[t].Should().Be(flow);
                for (int i = 0; i < n; i++)
                {
                    if (i == s || i == t)
                    {
                        continue;
                    }
                    vFlow[i].Should().Be(0);
                }
            }
        }
Пример #4
0
        public void Cut()
        {
            var g = new MfGraphInt(3);

            g.AddEdge(0, 1, 2).Should().Be(0);
            g.AddEdge(1, 2, 1).Should().Be(1);
            g.Flow(0, 2).Should().Be(1);

            MfGraphInt.Edge e;
            e = new MfGraphInt.Edge(0, 1, 2, 1);
            g.GetEdge(0).Should().Be(e);
            e = new MfGraphInt.Edge(1, 2, 1, 1);
            g.GetEdge(1).Should().Be(e);


            g.MinCut(0).Should().Equal(new[] { true, true, false });
        }