示例#1
0
        public static IDictionary <int, int> GetDiscoverTimes(AdjacencyGraph <int, Edge <int> > g)
        {
            var dfs      = new DepthFirstSearchAlgorithm <int, Edge <int> >(g);
            var recorder = new VertexTimeStamperObserver <int>();

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.DiscoverTimes);
            }
        }
示例#2
0
        public static IDictionary <string, int> GetFinishTimes(AdjacencyGraph <string, TaggedEdge <string, string> > g)
        {
            var dfs      = new DepthFirstSearchAlgorithm <string, TaggedEdge <string, string> >(g);
            var recorder = new VertexTimeStamperObserver <string>();

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.FinishTimes);
            }
        }
        public void Constructor()
        {
            var recorder = new VertexTimeStamperObserver <int>();

            CollectionAssert.IsEmpty(recorder.DiscoverTimes);
            CollectionAssert.IsEmpty(recorder.FinishTimes);

            var discoverTimes = new Dictionary <int, int>();

            recorder = new VertexTimeStamperObserver <int>(discoverTimes);
            Assert.AreSame(discoverTimes, recorder.DiscoverTimes);
            Assert.IsNull(recorder.FinishTimes);

            discoverTimes = new Dictionary <int, int>
            {
                [1] = 0,
                [2] = 1,
                [3] = 3,
                [4] = 2
            };
            recorder = new VertexTimeStamperObserver <int>(discoverTimes);
            Assert.AreSame(discoverTimes, recorder.DiscoverTimes);
            Assert.IsNull(recorder.FinishTimes);

            discoverTimes = new Dictionary <int, int>();
            var finishTimes = new Dictionary <int, int>();

            recorder = new VertexTimeStamperObserver <int>(
                discoverTimes,
                finishTimes);
            Assert.AreSame(discoverTimes, recorder.DiscoverTimes);
            Assert.AreSame(finishTimes, recorder.FinishTimes);

            discoverTimes = new Dictionary <int, int>
            {
                [1] = 0,
                [2] = 1,
                [3] = 3,
                [4] = 2
            };
            finishTimes = new Dictionary <int, int>
            {
                [1] = 4,
                [2] = 6,
                [3] = 5,
                [4] = 7
            };
            recorder = new VertexTimeStamperObserver <int>(
                discoverTimes,
                finishTimes);
            CollectionAssert.AreEqual(discoverTimes, recorder.DiscoverTimes);
            CollectionAssert.AreEqual(finishTimes, recorder.FinishTimes);
        }
        public void Attach()
        {
            // DFS is used for tests but result may change if using another search algorithm
            // or another starting point
            {
                var recorder = new VertexTimeStamperObserver <int>();

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

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

                    CollectionAssert.IsEmpty(recorder.DiscoverTimes);
                    CollectionAssert.IsEmpty(recorder.FinishTimes);
                }
            }

            {
                var recorder = new VertexTimeStamperObserver <int>();

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

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

                    CollectionAssert.AreEqual(
                        new Dictionary <int, int>
                    {
                        [1] = 0,
                        [2] = 2
                    },
                        recorder.DiscoverTimes);

                    CollectionAssert.AreEqual(
                        new Dictionary <int, int>
                    {
                        [1] = 1,
                        [2] = 3
                    },
                        recorder.FinishTimes);
                }
            }

            {
                var recorder = new VertexTimeStamperObserver <int>();

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

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

                    CollectionAssert.AreEqual(
                        new Dictionary <int, int>
                    {
                        [1] = 0,
                        [2] = 1,
                        [3] = 4,
                        [4] = 5
                    },
                        recorder.DiscoverTimes);

                    CollectionAssert.AreEqual(
                        new Dictionary <int, int>
                    {
                        [1] = 3,
                        [2] = 2,
                        [3] = 7,
                        [4] = 6
                    },
                        recorder.FinishTimes);
                }
            }
        }