public void SimpleGraph()
        {
            var graph = new UndirectedGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(2, 3),
                new Edge <int>(4, 2),
                new Edge <int>(4, 5),
                new Edge <int>(5, 6),
                new Edge <int>(7, 5),
                new Edge <int>(7, 8)
            });

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

            algorithm.Compute();

            // Order in undirected graph is some strange thing, here the order
            // is more vertices ordered from lower to higher adjacent vertices
            CollectionAssert.AreEqual(
                new[] { 1, 8, 3, 7, 2, 6, 4, 5 },
                algorithm.SortedVertices);
        }
        public void GraphWithSelfEdge()
        {
            var graph = new UndirectedGraph <int, Edge <int> >();

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

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

            Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute());

            algorithm = new UndirectedFirstTopologicalSortAlgorithm <int, Edge <int> >(graph)
            {
                AllowCyclicGraph = true
            };
            algorithm.Compute();

            // Order in undirected graph is some strange thing, here the order
            // is more vertices ordered from lower to higher adjacent vertices
            CollectionAssert.AreEqual(
                new[] { 0, 4, 1, 3, 2 },
                algorithm.SortedVertices);
        }
        public void Compute([PexAssumeNotNull]IUndirectedGraph<string, Edge<string>> g)
        {
            var topo =
                new UndirectedFirstTopologicalSortAlgorithm<string, Edge<string>>(g);
            topo.AllowCyclicGraph = true;
            topo.Compute();

            Display(topo);
        }
Exemple #4
0
        private static void Compute <TVertex, TEdge>([NotNull] IUndirectedGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            var algorithm = new UndirectedFirstTopologicalSortAlgorithm <TVertex, TEdge>(graph)
            {
                AllowCyclicGraph = true
            };

            algorithm.Compute();
        }
        public static (int[], string) Get(UndirectedGraph <int, Edge <int> > graph)
        {
            var algorithm = new UndirectedFirstTopologicalSortAlgorithm <int, Edge <int> >(graph);

            try
            {
                algorithm.Compute();
                return(algorithm.SortedVertices, null);
            }
            catch (System.Exception ex)
            {
                return(null, ex.Message);
            }
        }
        private static void RunUndirectedFirstTopologicalSortAndCheck <TVertex, TEdge>(
            IUndirectedGraph <TVertex, TEdge> graph,
            bool allowCycles)
            where TEdge : IEdge <TVertex>
        {
            var algorithm = new UndirectedFirstTopologicalSortAlgorithm <TVertex, TEdge>(graph)
            {
                AllowCyclicGraph = allowCycles
            };

            algorithm.Compute();

            Assert.IsNotNull(algorithm.SortedVertices);
            Assert.AreEqual(graph.VertexCount, algorithm.SortedVertices.Length);
            Assert.IsNotNull(algorithm.Degrees);
            Assert.AreEqual(graph.VertexCount, algorithm.Degrees.Count);
        }
        public void UndirectedFirstTopologicalSort_Throws()
        {
            var cyclicGraph = new UndirectedGraph <int, Edge <int> >();

            cyclicGraph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(2, 3),
                new Edge <int>(1, 4),
                new Edge <int>(3, 1)
            });

            var algorithm = new UndirectedFirstTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph);

            Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute());

            algorithm = new UndirectedFirstTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph)
            {
                AllowCyclicGraph = true
            };
            Assert.DoesNotThrow(() => algorithm.Compute());
        }