public void SetRootVertex_Throws()
        {
            var graph     = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var algorithm = new ImplicitEdgeDepthFirstSearchAlgorithm <TestVertex, Edge <TestVertex> >(graph);

            SetRootVertex_Throws_Test(algorithm);
        }
        public void ClearRootVertex()
        {
            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var algorithm = new ImplicitEdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);

            ClearRootVertex_Test(algorithm);
        }
        public void Constructor()
        {
            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var algorithm = new ImplicitEdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new ImplicitEdgeDepthFirstSearchAlgorithm <int, Edge <int> >(null, graph);
            AssertAlgorithmProperties(algorithm, graph);

            algorithm.MaxDepth = 12;
            AssertAlgorithmProperties(algorithm, graph, 12);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                ImplicitEdgeDepthFirstSearchAlgorithm <TVertex, TEdge> algo,
                IIncidenceGraph <TVertex, TEdge> g,
                int maxDepth = int.MaxValue)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                CollectionAssert.IsEmpty(algo.EdgesColors);
                Assert.AreEqual(maxDepth, algo.MaxDepth);
            }

            #endregion
        }
        private static void RunImplicitEdgeDFSAndCheck <TVertex, TEdge>(
            IEdgeListAndIncidenceGraph <TVertex, TEdge> graph,
            TVertex sourceVertex,
            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 ImplicitEdgeDepthFirstSearchAlgorithm <TVertex, TEdge>(graph)
            {
                MaxDepth = maxDepth
            };

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

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

                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(sourceVertex);

            // Check
            if (maxDepth == int.MaxValue)
            {
                Assert.AreEqual(discoverTimes.Count, finishTimes.Count);
            }
            else
            {
                Assert.GreaterOrEqual(discoverTimes.Count, finishTimes.Count);
            }

            TEdge[] exploredEdges = finishTimes.Keys.ToArray();
            foreach (TEdge e1 in exploredEdges)
            {
                foreach (TEdge e2 in exploredEdges)
                {
                    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)));
                    }
                }
            }
        }