Example #1
0
        public void Should_throw_exception_when_traversing_cyclic_graph()
        {
            var found = new HashSet<int>();

            var algorithm = new DepthFirstSearchAlgorithm<int, SEdge<int>>(_adjacencyGraph);
            algorithm.DiscoverVertex += v => found.Add(v);
            algorithm.ExamineEdge += e => { if (algorithm.GetVertexColor(e.Target) != GraphColor.White) throw new Exception("cycle!"); };

            Assert.That(() => algorithm.Compute(10), Throws.Exception);
        }
Example #2
0
        public void Should_visit_all_vertices_reachable_from_one()
        {
            var found = new HashSet<int>();

            var algorithm = new DepthFirstSearchAlgorithm<int, SEdge<int>>(_adjacencyGraph);
            algorithm.DiscoverVertex += v => found.Add(v);
            algorithm.ExamineEdge += e => { if (algorithm.GetVertexColor(e.Target) != GraphColor.White) throw new Exception("cycle!"); };
            algorithm.Compute(1);

            Assert.That(found.OrderBy(i => i).ToArray(), Is.EquivalentTo(new[] {1, 2, 3, 4} ));
        }