예제 #1
0
        /// <summary>
        /// Tests all actions for adjacencygraph and filtered graph.
        /// </summary>
        public void Test()
        {
            // The all action algorithms
            AdjacencyGraph g = GraphProvider.Fsm();

            Console.WriteLine("Graph: {0} vertices, {1} edges, {2} eulerian trails",
                              g.VerticesCount,
                              g.EdgesCount,
                              EulerianTrailAlgorithm.ComputeEulerianPathCount(g)
                              );

            // do layout
            EulerianTrailAlgorithm euler = CreateEulerianTrail(g);
            EdgeCollection         temps = euler.AddTemporaryEdges(g);

            Console.WriteLine("Added {0} temporary edges", temps.Count);
            foreach (NamedEdge ne in temps)
            {
                ne.Name = "temporary";
            }
            euler.Compute();
            euler.RemoveTemporaryEdges(g);

            Console.WriteLine("Circuit: {0} edges",
                              euler.Circuit.Count);
            foreach (NamedEdge e in euler.Circuit)
            {
                Console.WriteLine("{0}->{1} ({2})",
                                  (NamedVertex)e.Source,
                                  (NamedVertex)e.Target,
                                  e.Name
                                  );
            }


            Console.WriteLine("Trails:");
            foreach (EdgeCollection ec in euler.Trails(
                         Traversal.FirstVertexIf(g.Vertices, new NameEqualPredicate("S0")))
                     )
            {
                foreach (IEdge edge in ec)
                {
                    Console.WriteLine("{0}->{1}, {2}",
                                      ((NamedVertex)edge.Source).Name,
                                      ((NamedVertex)edge.Target).Name,
                                      ((NamedEdge)edge).Name
                                      );
                }
                Console.WriteLine();
            }


            Console.WriteLine("Testing AdjacencyGraph");
            TestAllActions(GraphProvider.Fsm(), @"../EdgeDfs");

            // testing on filtered graph
            Console.WriteLine("Testing FilteredVertexAndEdgeListGraph");
            TestAllActions(GraphProvider.FilteredFsm(), @"../FilteredEdgeDfs");
        }
        private static void ComputeTrails <TVertex, TEdge>(
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph,
            TVertex root,
            Func <TVertex, TVertex, TEdge> edgeFactory,
            out ICollection <TEdge>[] trails,
            out TEdge[] circuit)
            where TEdge : IEdge <TVertex>
        {
            trails  = new ICollection <TEdge> [0];
            circuit = new TEdge[0];

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

            if (circuitCount == 0)
            {
                return;
            }

            TEdge[] graphEdges = graph.Edges.ToArray();

            var algorithm = new EulerianTrailAlgorithm <TVertex, TEdge>(graph);

            algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t));
            TEdge[] augmentedGraphEdges = graph.Edges.ToArray();
            Assert.GreaterOrEqual(augmentedGraphEdges.Length, graphEdges.Length);
            TEdge[] temporaryEdges = augmentedGraphEdges.Except(graphEdges).ToArray();
            Assert.AreEqual(augmentedGraphEdges.Length - graphEdges.Length, temporaryEdges.Length);

            algorithm.Compute();
            trails = algorithm.Trails(root).ToArray();
            algorithm.RemoveTemporaryEdges();
            Assert.IsNotNull(algorithm.Circuit);
            circuit = algorithm.Circuit;

            // Lets make sure all the edges are in the trail
            var edges = new HashSet <TEdge>();

            foreach (TEdge edge in graph.Edges)
            {
                Assert.IsTrue(edges.Add(edge));
            }

            foreach (ICollection <TEdge> trail in trails)
            {
                Assert.AreEqual(graph.EdgeCount, edges.Count);
                QuikGraphAssert.TrueForAll(
                    trail,
                    // Edge in graph or part of temporary ones but is a root
                    edge => edges.Contains(edge) ||
                    (temporaryEdges.Contains(edge) && Equals(edge.Source, root)));
            }
        }
        private static void ComputeTrails <TVertex, TEdge>(
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph,
            Func <TVertex, TVertex, TEdge> edgeFactory,
            out ICollection <TEdge>[] trails,
            out TEdge[] circuit)
            where TEdge : IEdge <TVertex>
        {
            trails  = new ICollection <TEdge> [0];
            circuit = new TEdge[0];

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

            if (circuitCount == 0)
            {
                return;
            }

            var algorithm = new EulerianTrailAlgorithm <TVertex, TEdge>(graph);

            algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t));
            algorithm.Compute();
            trails = algorithm.Trails().ToArray();
            algorithm.RemoveTemporaryEdges();
            Assert.IsNotNull(algorithm.Circuit);
            circuit = algorithm.Circuit;

            // Lets make sure all the edges are in the trail
            var edges = new HashSet <TEdge>();

            foreach (TEdge edge in graph.Edges)
            {
                Assert.IsTrue(edges.Add(edge));
            }

            foreach (ICollection <TEdge> trail in trails)
            {
                Assert.AreEqual(graph.EdgeCount, edges.Count);
                QuikGraphAssert.TrueForAll(
                    trail,
                    edge => edges.Contains(edge));
            }
        }
        private static void ComputeTrail <TVertex, TEdge>(
            [NotNull] IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

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

            if (circuitCount == 0)
            {
                return;
            }

            var algorithm = new EulerianTrailAlgorithm <TVertex, TEdge>(graph);

            algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t));
            algorithm.Compute();
            var trails = algorithm.Trails();

            algorithm.RemoveTemporaryEdges();

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

            foreach (TEdge edge in graph.Edges)
            {
                edgeColors.Add(edge, GraphColor.White);
            }

            foreach (ICollection <TEdge> trail in trails)
            {
                foreach (TEdge edge in trail)
                {
                    Assert.IsTrue(edgeColors.ContainsKey(edge));
                }
            }
        }
예제 #5
0
        private static string ComputeTrails <TVertex, TEdge>(
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> g,
            TVertex root,
            Func <TVertex, TVertex, TEdge> edgeFactory,
            out ICollection <TEdge>[] trails,
            out TEdge[] circuit)
            where TEdge : IEdge <TVertex>
        {
            trails  = new ICollection <TEdge> [0];
            circuit = new TEdge[0];

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

            if (circuitCount == 0)
            {
                return("No Eulerian path found.");
            }

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

            algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t));

            algorithm.Compute();
            try
            {
                trails = algorithm.Trails(root).ToArray();
            }
            catch (System.Exception ex)
            {
                return(ex.Message);
            }

            algorithm.RemoveTemporaryEdges();
            circuit = algorithm.Circuit;
            return(null);
        }
 public void ComputeEulerianPathCount_Throws()
 {
     // ReSharper disable once AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(
         () => EulerianTrailAlgorithm <int, Edge <int> > .ComputeEulerianPathCount(null));
 }
 public int ComputeEulerianPathCount(AdjacencyGraph <int, Edge <int> > graph)
 {
     return(EulerianTrailAlgorithm <int, Edge <int> > .ComputeEulerianPathCount(graph));
 }