public double CompareRoot <TVertex, TEdge>(IUndirectedGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>();

            foreach (var e in g.Edges)
            {
                distances[e] = g.AdjacentDegree(e.Source) + 1;
            }

            var prim    = new List <TEdge>(g.MinimumSpanningTreePrim(e => distances[e]));
            var kruskal = new List <TEdge>(g.MinimumSpanningTreeKruskal(e => distances[e]));

            var primCost    = prim.Sum(e => distances[e]);
            var kruskalCost = kruskal.Sum(e => distances[e]);

            TestConsole.WriteLine("prim cost: {0}", primCost);
            TestConsole.WriteLine("kruskal cost: {0}", kruskalCost);
            if (primCost != kruskalCost)
            {
                GraphConsoleSerializer.DisplayGraph(g);
                TestConsole.WriteLine("prim: {0}", String.Join(", ", Array.ConvertAll(prim.ToArray(), e => e.ToString() + ':' + distances[e])));
                TestConsole.WriteLine("krus: {0}", String.Join(", ", Array.ConvertAll(kruskal.ToArray(), e => e.ToString() + ':' + distances[e])));
                Assert.Fail("cost do not match");
            }

            return(kruskalCost);
        }
        public void Compute([PexAssumeNotNull] IVertexListGraph <string, Edge <string> > g)
        {
            GraphConsoleSerializer.DisplayGraph(g);

            WeaklyConnectedComponentsAlgorithm <string, Edge <string> > dfs =
                new WeaklyConnectedComponentsAlgorithm <string, Edge <string> >(g);

            dfs.Compute();

            Console.WriteLine("Weak components: {0}", dfs.ComponentCount);
            Assert.AreEqual(g.VertexCount, dfs.Components.Count);
            foreach (KeyValuePair <string, int> kv in dfs.Components)
            {
                Console.WriteLine("\t{0}: {1}", kv.Key, kv.Value);
            }

            foreach (KeyValuePair <string, int> kv in dfs.Components)
            {
                Assert.IsLowerEqual(0, kv.Value);
                Assert.IsLower(kv.Value, dfs.ComponentCount);
            }

            foreach (string vertex in g.Vertices)
            {
                foreach (Edge <string> edge in g.OutEdges(vertex))
                {
                    Assert.AreEqual(dfs.Components[edge.Source], dfs.Components[edge.Target]);
                }
            }
        }
        private static double CompareRoot <TVertex, TEdge>([NotNull] IUndirectedGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>();

            foreach (TEdge edge in graph.Edges)
            {
                distances[edge] = graph.AdjacentDegree(edge.Source) + 1;
            }

            TEdge[] prim    = graph.MinimumSpanningTreePrim(e => distances[e]).ToArray();
            TEdge[] kruskal = graph.MinimumSpanningTreeKruskal(e => distances[e]).ToArray();

            double primCost    = prim.Sum(e => distances[e]);
            double kruskalCost = kruskal.Sum(e => distances[e]);

            if (Math.Abs(primCost - kruskalCost) > double.Epsilon)
            {
                GraphConsoleSerializer.DisplayGraph(graph);
                Assert.Fail("Cost do not match.");
            }

            return(kruskalCost);
        }
예제 #4
0
        public void ComputeTrail(IMutableVertexAndEdgeListGraph <string, Edge <string> > g)
        {
            if (g.VertexCount == 0)
            {
                return;
            }

            GraphConsoleSerializer.DisplayGraph(g);

            int oddCount = 0;

            foreach (string v in g.Vertices)
            {
                if (g.OutDegree(v) % 2 == 0)
                {
                    oddCount++;
                }
            }

            int circuitCount = EulerianTrailAlgorithm <string, Edge <string> > .ComputeEulerianPathCount(g);

            if (circuitCount == 0)
            {
                return;
            }

            EulerianTrailAlgorithm <string, Edge <string> > trail = new EulerianTrailAlgorithm <string, Edge <string> >(g);

            trail.AddTemporaryEdges(new EdgeFactory <string>());
            trail.Compute();
            ICollection <ICollection <Edge <string> > > trails = trail.Trails();

            trail.RemoveTemporaryEdges();

            Console.WriteLine("trails: {0}", trails.Count);
            int index = 0;

            foreach (ICollection <Edge <string> > t in trails)
            {
                Console.WriteLine("trail {0}", index++);
                foreach (Edge <string> edge in t)
                {
                    Console.WriteLine("\t{0}", t);
                }
            }

            // lets make sure all the edges are in the trail
            Dictionary <Edge <string>, GraphColor> edgeColors = new Dictionary <Edge <string>, GraphColor>(g.EdgeCount);

            foreach (Edge <string> edge in g.Edges)
            {
                edgeColors.Add(edge, GraphColor.White);
            }
            foreach (ICollection <Edge <string> > t in trails)
            {
                foreach (Edge <string> edge in t)
                {
                    CollectionAssert.ContainsKey(edgeColors, edge);
                }
            }
        }