private void ComputeTrail <TVertex, TEdge>(
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> g,
            Func <TVertex, TVertex, TEdge> edgeCreator)
            where TEdge : IEdge <TVertex>
        {
            if (g.VertexCount == 0)
            {
                return;
            }

            int oddCount = 0;

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

            int circuitCount = EulerianTrailAlgorithm <TVertex, TEdge> .ComputeEulerianPathCount(g);

            if (circuitCount == 0)
            {
                return;
            }

            var trail = new EulerianTrailAlgorithm <TVertex, TEdge>(g);

            trail.AddTemporaryEdges((s, t) => edgeCreator(s, t));
            trail.Compute();
            var trails = trail.Trails();

            trail.RemoveTemporaryEdges();

            //TestConsole.WriteLine("trails: {0}", trails.Count);
            //int index = 0;
            //foreach (var t in trails)
            //{
            //    TestConsole.WriteLine("trail {0}", index++);
            //    foreach (Edge<string> edge in t)
            //        TestConsole.WriteLine("\t{0}", t);
            //}

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

            foreach (var edge in g.Edges)
            {
                edgeColors.Add(edge, GraphColor.White);
            }
            foreach (var t in trails)
            {
                foreach (var edge in t)
                {
                    Assert.True(edgeColors.ContainsKey(edge));
                }
            }
        }
        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);

        }
예제 #3
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);
                }
            }
        }
예제 #4
0
        public static void Create <TEdge>(
            IMutableVertexAndEdgeListGraph <MazeGraphVertex, TEdge> g,
            VertexFactory <MazeGraphVertex> vertexFactory,
            EdgeFactory <MazeGraphVertex, TEdge> edgeFactory,
            Random rnd,
            MazeGraphOptions opts
            ) where TEdge : IEdge <MazeGraphVertex>
        {
            Contract.Requires(g != null);
            Contract.Requires(vertexFactory != null);
            Contract.Requires(edgeFactory != null);
            Contract.Requires(rnd != null);
            Contract.Requires(opts.vertexCount > 0);
            Contract.Requires(opts.edgeCount >= 0);
            Contract.Requires(
                !(!g.AllowParallelEdges && !opts.selfEdges) ||
                opts.edgeCount <= opts.vertexCount * (opts.vertexCount - 1)                // directed graph
                );

            var vertices = new MazeGraphVertex[opts.vertexCount];

            for (int i = 0; i < opts.vertexCount; ++i)
            {
                g.AddVertex(vertices[i] = vertexFactory());
            }

            var freeVertices = new List <MazeGraphVertex> (vertices);

            MazeGraphVertex a;
            MazeGraphVertex b;
            int             j, k;

            j = k = 0;
            while (j < opts.edgeCount)
            {
                a = freeVertices[rnd.Next(opts.vertexCount - k)];
                do
                {
                    b = vertices[rnd.Next(opts.vertexCount)];
                }while (opts.selfEdges == false && a.Equals(b));

                if (g.AddEdge(edgeFactory(a, b)))
                {
                    if (g.OutDegree(a) >= opts.branchingFactor)
                    {
                        freeVertices.Remove(a);
                        ++k;
                    }
                    ++j;
                }
            }

            j = k = 0;
            while (j < opts.treasures)
            {
                a = MazeGraphFactory.GetVertex(g, rnd);
                if (!(a.HasTreasure))
                {
                    a.HasTreasure = true;
                    ++j;
                }
            }

            while (k < opts.startingPoints)
            {
                b = MazeGraphFactory.GetVertex(g, rnd);
                if (!(b.StartingPoint))
                {
                    b.StartingPoint = true;
                    ++k;
                }
            }
        }