public void Compute()
        {
            this.joins.Clear();
            this.startVertex = null;
            UndirectedDepthFirstSearchAlgorithm udfs =
                new UndirectedDepthFirstSearchAlgorithm(this.VisitedGraph);
            udfs.TreeEdge+=new EdgeEventHandler(udfs_TreeEdge);
            udfs.StartVertex+=new VertexEventHandler(udfs_StartVertex);

            udfs.Compute();
        }
        public void Init()
        {
            parents = new VertexVertexDictionary();
            discoverTimes = new VertexIntDictionary();
            finishTimes = new VertexIntDictionary();
            time = 0;
            g = new BidirectionalGraph(true);
            dfs = new UndirectedDepthFirstSearchAlgorithm(g);

            dfs.StartVertex += new VertexEventHandler(this.StartVertex);
            dfs.DiscoverVertex += new VertexEventHandler(this.DiscoverVertex);
            dfs.ExamineEdge += new EdgeEventHandler(this.ExamineEdge);
            dfs.TreeEdge += new EdgeEventHandler(this.TreeEdge);
            dfs.BackEdge += new EdgeEventHandler(this.BackEdge);
            dfs.FinishVertex += new VertexEventHandler(this.FinishVertex);
        }
Exemple #3
0
        private void UndirectedDepthFirstSearch <TVertex, TEdge>(IUndirectedGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var parents       = new Dictionary <TVertex, TVertex>();
            var discoverTimes = new Dictionary <TVertex, int>();
            var finishTimes   = new Dictionary <TVertex, int>();
            int time          = 0;
            var dfs           = new UndirectedDepthFirstSearchAlgorithm <TVertex, TEdge>(g);

            dfs.StartVertex += args =>
            {
                Assert.Equal(GraphColor.White, dfs.VertexColors[args]);
                Assert.False(parents.ContainsKey(args));
                parents[args] = args;
            };

            dfs.DiscoverVertex += args =>
            {
                Assert.Equal(GraphColor.Gray, dfs.VertexColors[args]);
                Assert.Equal(GraphColor.Gray, dfs.VertexColors[parents[args]]);

                discoverTimes[args] = time++;
            };

            dfs.ExamineEdge += (sender, args) =>
            {
                Assert.Equal(GraphColor.Gray, dfs.VertexColors[args.Source]);
            };

            dfs.TreeEdge += (sender, args) =>
            {
                var source = args.Source;
                var target = args.Target;
                Assert.Equal(GraphColor.White, dfs.VertexColors[target]);
                parents[target] = source;
            };

            dfs.BackEdge += (sender, args) =>
            {
                Assert.Equal(GraphColor.Gray, dfs.VertexColors[args.Target]);
            };

            dfs.ForwardOrCrossEdge += (sender, args) =>
            {
                Assert.Equal(GraphColor.Black, dfs.VertexColors[args.Target]);
            };

            dfs.FinishVertex += args =>
            {
                Assert.Equal(GraphColor.Black, dfs.VertexColors[args]);
                finishTimes[args] = time++;
            };

            dfs.Compute();

            // check
            // all vertices should be black
            foreach (var v in g.Vertices)
            {
                Assert.True(dfs.VertexColors.ContainsKey(v));
                Assert.Equal(GraphColor.Black, dfs.VertexColors[v]);
            }

            foreach (var u in g.Vertices)
            {
                foreach (var v in g.Vertices)
                {
                    if (!u.Equals(v))
                    {
                        Assert.True(
                            finishTimes[u] < discoverTimes[v] ||
                            finishTimes[v] < discoverTimes[u] ||
                            (
                                discoverTimes[v] < discoverTimes[u] &&
                                finishTimes[u] < finishTimes[v] &&
                                IsDescendant(parents, u, v)
                            ) ||
                            (
                                discoverTimes[u] < discoverTimes[v] &&
                                finishTimes[v] < finishTimes[u] &&
                                IsDescendant(parents, v, u)
                            )
                            );
                    }
                }
            }
        }