コード例 #1
0
    public static void TestEdgeDisjointPath6(bool log = true, int loops = 1000)
    {
        for (int loop = 0; loop < loops; ++loop)
        {
            var g        = new UndirectedGraph <int>();
            var rnd      = new Random();
            int vertices = 10 + rnd.Next(1000);
            int edges    = vertices / 2 + rnd.Next(vertices * vertices / 4);
            for (int i = 0; i < edges; ++i)
            {
                g.AddEdge(rnd.Next(vertices), rnd.Next(vertices));
            }
            if (log)
            {
                Console.WriteLine(g);
            }

            var paths = g.FindEdgeDisjointPaths1(0, vertices - 1);
            if (paths.Count() > 0)
            {
                var usedEdges = CheckEDP(g, 0, vertices - 1, paths, log);
                var cut       = g.FindCutEDP(0, usedEdges);
                if (log)
                {
                    Console.WriteLine("Size of cut: {0}", cut.Count());
                    Console.Write("Cut: ");
                    foreach (var v in cut)
                    {
                        Console.Write("{0} ", v);
                    }
                    Console.Write("\n");
                }
                if (cut.Count() != paths.Count())
                {
                    throw new Exception("Cut is not the same size of set as edge-disjoint paths!");
                }
                foreach (var edge in cut)
                {
                    g.RemoveEdge(edge.Item1, edge.Item2);
                }
                if (g.FindPath(0, vertices - 1).Count() > 0)
                {
                    throw new Exception(String.Format("Graph {0} not disconnected by cut!", g));
                }
            }
            else
            {
                if (log)
                {
                    Console.WriteLine("No path, apparently.");
                }
                var path = g.FindPath(0, vertices - 1);
                if (path.Count() > 0)
                {
                    throw new Exception("No path found, but there is a path!");
                }
            }
        }
    }