Ejemplo n.º 1
0
        public static IEnumerable <int> Get(AdjacencyGraph <int, Edge <int> > g)
        {
            var dfs      = new DepthFirstSearchAlgorithm <int, Edge <int> >(g);
            var recorder = new VertexRecorderObserver <int>();

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.Vertices);
            }
        }
Ejemplo n.º 2
0
        public void Attach()
        {
            // DFS is used for tests but result may change if using another search algorithm
            // or another starting point
            {
                var recorder = new VertexRecorderObserver <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.Vertices);
                }
            }

            {
                var recorder = new VertexRecorderObserver <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[] { 1, 2 },
                        recorder.Vertices);
                }
            }

            {
                var recorder = new VertexRecorderObserver <int>(new[] { 1 });

                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[] { 1, 1, 2 },  // Add without checking if vertex already exists
                        recorder.Vertices);
                }
            }

            {
                var recorder = new VertexRecorderObserver <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[] { 1, 2, 3, 4 },
                        recorder.Vertices);
                }
            }
        }