public static IDictionary <Edge <int>, Edge <int> > Get(AdjacencyGraph <int, Edge <int> > g)
        {
            var dfs      = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(g);
            var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.EdgesPredecessors);
            }
        }
        public static List <IEnumerable <TaggedEdge <string, string> > > GetPaths(AdjacencyGraph <string, TaggedEdge <string, string> > g)
        {
            var dfs      = new EdgeDepthFirstSearchAlgorithm <string, TaggedEdge <string, string> >(g);
            var recorder = new EdgePredecessorRecorderObserver <string, TaggedEdge <string, string> >();

            List <IEnumerable <TaggedEdge <string, string> > > results = new List <IEnumerable <TaggedEdge <string, string> > >();

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                var paths = recorder.AllPaths();
                foreach (var p in paths)
                {
                    results.Add(p);
                }
            }
            return(results);
        }
Exemple #3
0
        /// <summary>
        /// Executes edge dfs
        /// </summary>
        /// <param name="g"></param>
        public void TestAllActions(IVertexAndEdgeListGraph g, string path)
        {
            // Testing dfs all apth
            DepthFirstSearchAlgorithm  dfs  = new DepthFirstSearchAlgorithm(g);
            PredecessorRecorderVisitor visv = new PredecessorRecorderVisitor();

            dfs.RegisterPredecessorRecorderHandlers(visv);
            IVertex s0 = Traversal.FirstVertexIf(g.Vertices, new NameEqualPredicate("S0"));

            dfs.Compute(s0);
            Console.WriteLine("vertex paths");
            foreach (EdgeCollection ec in visv.AllPaths())
            {
                foreach (IEdge edge in ec)
                {
                    Console.WriteLine("{0}->{1}, {2}",
                                      ((NamedVertex)edge.Source).Name,
                                      ((NamedVertex)edge.Target).Name,
                                      ((NamedEdge)edge).Name
                                      );
                }
                Console.WriteLine();
            }
            // end of dfs test

            GraphvizAlgorithm gw = RenderActions(g, path);

            // The all action algorithms
            EdgeDepthFirstSearchAlgorithm edfs = CreateEdgeDfs(g);
            // add tracer
            AlgorithmTracerVisitor vis = AddTracer(edfs, path);
            // add predecessor recorder
            EdgePredecessorRecorderVisitor erec = AddPredecessorRecorder(edfs);

            // computing all actions
            edfs.Compute(s0);

            // display end path edges
            OutputEndPathEdges(erec);

            // display all actions path
            OutputAllActions(erec, gw, path);
        }
Exemple #4
0
        public void ProcessAllComponents(bool processAll)
        {
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);
            var edge21 = new Edge <int>(2, 1);
            var edge24 = new Edge <int>(2, 4);
            var edge25 = new Edge <int>(2, 5);

            var edge67 = new Edge <int>(6, 7);
            var edge68 = new Edge <int>(6, 8);
            var edge86 = new Edge <int>(8, 6);

            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                edge12, edge13, edge21, edge24, edge25,

                edge67, edge68, edge86
            });

            var algorithm = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph)
            {
                ProcessAllComponents = processAll
            };

            algorithm.Compute(1);

            if (processAll)
            {
                QuikGraphAssert.TrueForAll(algorithm.EdgesColors, pair => pair.Value == GraphColor.Black);
            }
            else
            {
                QuikGraphAssert.TrueForAll(
                    new[] { edge12, edge13, edge24, edge25 },
                    edge => algorithm.EdgesColors[edge] == GraphColor.Black);
                QuikGraphAssert.TrueForAll(
                    new[] { edge67, edge68, edge86 },
                    edge => algorithm.EdgesColors[edge] == GraphColor.White);
            }
        }
Exemple #5
0
        public override Problem[] Check(Method method)
        {
            this.populator.BuildGraphFromMethod(method);

            EdgeDepthFirstSearchAlgorithm edfs = new EdgeDepthFirstSearchAlgorithm(this.populator.Graph);
            NullStateVisitor vis = new NullStateVisitor(this.populator.Graph);

            edfs.RegisterEdgeColorizerHandlers(vis);

            vis.Log.WriteLine("-- Fault detection for {0}.{1}",
                              method.DeclaringType.ToString(),
                              method.Name
                              );

            int       index    = 0;
            ArrayList problems = new ArrayList();

            for (index = 0; index < method.Parameters.Length; ++index)
            {
                Parameter param = method.Parameters[index];
                vis.Clear();
                vis.Log.WriteLine("-- {0} parameter", param.Name);
                vis.ParameterIndex = index;
                edfs.Initialize();
                edfs.Compute(this.populator.Graph.Root);

                if (vis.Warnings.Count > 0 || vis.Errors.Count > 0)
                {
                    this.Problems.Add(
                        this.GetResolution(method.Name.Name, param.Name.Name)
                        );
                }
            }

            Problem[] ps = new Problem[problems.Count];
            problems.CopyTo(ps, 0);

            return(ps);
        }
Exemple #6
0
        public void AllMergedPath()
        {
            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                var graph = new AdjacencyGraph <int, Edge <int> >();

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.IsEmpty(recorder.AllMergedPaths());
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph without cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEquivalent(
                        new IEnumerable <Edge <int> >[]
                    {
                        new[] { edge12, edge24 },
                        new[] { edge13, edge31, edge14 },
                        new[] { /* edge13 can't be reused */ edge33, edge34 }
                    },
                        recorder.AllMergedPaths());
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph with cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var edge41 = new Edge <int>(4, 1);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34, edge41
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEquivalent(
                        new IEnumerable <Edge <int> >[]
                    {
                        new[] { edge12, edge24, edge41, edge13, edge31, edge14 },
                        new[] { /* edge12, edge24, edge41, edge13 can't be reused */ edge33, edge34 }
                    },
                        recorder.AllMergedPaths());
                }
            }
        }
Exemple #7
0
        public void Attach()
        {
            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                var graph = new AdjacencyGraph <int, Edge <int> >();

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.IsEmpty(recorder.EdgesPredecessors);
                    CollectionAssert.IsEmpty(recorder.EndPathEdges);
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                var graph = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVertexRange(new[] { 1, 2 });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.IsEmpty(recorder.EdgesPredecessors);
                    CollectionAssert.IsEmpty(recorder.EndPathEdges);
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph without cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEqual(
                        new Dictionary <Edge <int>, Edge <int> >
                    {
                        [edge14] = edge31,
                        [edge24] = edge12,
                        [edge31] = edge13,
                        [edge33] = edge13,
                        [edge34] = edge33
                    },
                        recorder.EdgesPredecessors);
                    CollectionAssert.AreEquivalent(
                        new[] { edge14, edge24, edge34 },
                        recorder.EndPathEdges);
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph with cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var edge41 = new Edge <int>(4, 1);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34, edge41
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEqual(
                        new Dictionary <Edge <int>, Edge <int> >
                    {
                        [edge13] = edge41,
                        [edge14] = edge31,
                        [edge24] = edge12,
                        [edge31] = edge13,
                        [edge33] = edge13,
                        [edge34] = edge33,
                        [edge41] = edge24
                    },
                        recorder.EdgesPredecessors);
                    CollectionAssert.AreEquivalent(
                        new[] { edge14, edge34 },
                        recorder.EndPathEdges);
                }
            }
        }
Exemple #8
0
        public void MergedPath()
        {
            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                var graph = new AdjacencyGraph <int, Edge <int> >();

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    var edge12 = new Edge <int>(1, 2);
                    var colors = new Dictionary <Edge <int>, GraphColor>
                    {
                        [edge12] = GraphColor.Black
                    };

                    // Not in the graph and edge marked as already used!
                    CollectionAssert.IsEmpty(recorder.MergedPath(edge12, colors));

                    // Not in the graph => return the edge itself
                    colors[edge12] = GraphColor.White;
                    CollectionAssert.AreEqual(
                        new[] { edge12 },
                        recorder.MergedPath(edge12, colors));
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph without cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    Dictionary <Edge <int>, GraphColor> colors = graph.Edges.ToDictionary(
                        edge => edge,
                        _ => GraphColor.White);

                    CollectionAssert.AreEqual(
                        new[] { edge12, edge24 },
                        recorder.MergedPath(edge24, colors));

                    // Already used
                    CollectionAssert.IsEmpty(recorder.MergedPath(edge24, colors));

                    CollectionAssert.AreEqual(
                        new[] { edge13, edge31 },
                        recorder.MergedPath(edge31, colors));
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph with cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var edge41 = new Edge <int>(4, 1);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34, edge41
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    Dictionary <Edge <int>, GraphColor> colors = graph.Edges.ToDictionary(
                        edge => edge,
                        _ => GraphColor.White);

                    CollectionAssert.AreEqual(
                        new[] { edge12, edge24, edge41 },
                        recorder.MergedPath(edge41, colors));

                    // Already used
                    CollectionAssert.IsEmpty(recorder.MergedPath(edge41, colors));

                    CollectionAssert.AreEqual(
                        new[] { edge13, edge33, edge34 },
                        recorder.MergedPath(edge34, colors));
                }
            }
        }
Exemple #9
0
        public void Path()
        {
            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                var graph = new AdjacencyGraph <int, Edge <int> >();

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    var edge12 = new Edge <int>(1, 2);
                    // Not in the graph => return the edge itself
                    CollectionAssert.AreEqual(
                        new[] { edge12 },
                        recorder.Path(edge12));
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph without cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEquivalent(
                        new[] { edge13, edge31, edge14 },
                        recorder.Path(edge14));

                    CollectionAssert.AreEquivalent(
                        new[] { edge13, edge33 },
                        recorder.Path(edge33));
                }
            }

            {
                var recorder = new EdgePredecessorRecorderObserver <int, Edge <int> >();

                // Graph with cycle
                var edge12 = new Edge <int>(1, 2);
                var edge13 = new Edge <int>(1, 3);
                var edge14 = new Edge <int>(1, 4);
                var edge24 = new Edge <int>(2, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var edge41 = new Edge <int>(4, 1);
                var graph  = new AdjacencyGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge13, edge14, edge24, edge31, edge33, edge34, edge41
                });

                var dfs = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEquivalent(
                        new[] { edge12, edge24, edge41, edge13, edge31, edge14 },
                        recorder.Path(edge14));

                    CollectionAssert.AreEquivalent(
                        new[] { edge12, edge24, edge41, edge13, edge33 },
                        recorder.Path(edge33));
                }
            }
        }
Exemple #10
0
        private static void RunEdgeDFSAndCheck <TVertex, TEdge>(
            IEdgeListAndIncidenceGraph <TVertex, TEdge> graph,
            int maxDepth = int.MaxValue)
            where TEdge : IEdge <TVertex>
        {
            var parents       = new Dictionary <TEdge, TEdge>();
            var discoverTimes = new Dictionary <TEdge, int>();
            var finishTimes   = new Dictionary <TEdge, int>();
            int time          = 0;
            var dfs           = new EdgeDepthFirstSearchAlgorithm <TVertex, TEdge>(graph)
            {
                MaxDepth = maxDepth
            };

            dfs.InitializeEdge += edge =>
            {
                Assert.AreEqual(GraphColor.White, dfs.EdgesColors[edge]);
            };

            dfs.StartEdge += edge =>
            {
                Assert.AreEqual(GraphColor.White, dfs.EdgesColors[edge]);
                Assert.IsFalse(parents.ContainsKey(edge));
                parents[edge]       = edge;
                discoverTimes[edge] = time++;
            };

            dfs.DiscoverTreeEdge += (edge, targetEdge) =>
            {
                parents[targetEdge] = edge;

                Assert.AreEqual(GraphColor.White, dfs.EdgesColors[targetEdge]);
                Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[parents[targetEdge]]);

                discoverTimes[targetEdge] = time++;
            };

            dfs.TreeEdge += edge =>
            {
                Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]);
            };

            dfs.BackEdge += edge =>
            {
                Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]);
            };

            dfs.ForwardOrCrossEdge += edge =>
            {
                Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]);
            };

            dfs.FinishEdge += edge =>
            {
                Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]);
                finishTimes[edge] = time++;
            };

            dfs.Compute();

            // Check
            // All vertices should be black
            foreach (TEdge edge in graph.Edges)
            {
                Assert.IsTrue(dfs.EdgesColors.ContainsKey(edge));
                Assert.AreEqual(dfs.EdgesColors[edge], GraphColor.Black);
            }

            foreach (TEdge e1 in graph.Edges)
            {
                foreach (TEdge e2 in graph.Edges)
                {
                    if (!e1.Equals(e2))
                    {
                        Assert.IsTrue(
                            finishTimes[e1] < discoverTimes[e2] ||
                            finishTimes[e2] < discoverTimes[e1] ||
                            (discoverTimes[e2] < discoverTimes[e1] && finishTimes[e1] < finishTimes[e2] && IsDescendant(parents, e1, e2)) ||
                            (discoverTimes[e1] < discoverTimes[e2] && finishTimes[e2] < finishTimes[e1] && IsDescendant(parents, e2, e1)));
                    }
                }
            }
        }