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 UndirectedTopologicalSortAlgorithm <int, Edge <int> >(graph);

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

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

            // Order in undirected graph is some strange thing,
            // here the order is more vertices ordered by depth
            CollectionAssert.AreEqual(
                new[] { 0, 1, 2, 3, 4 },
                algorithm.SortedVertices);
        }
        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 UndirectedTopologicalSortAlgorithm <int, Edge <int> >(graph);

            algorithm.Compute();

            // Order in undirected graph is some strange thing,
            // here the order is more vertices ordered by depth
            CollectionAssert.AreEqual(
                new[] { 1, 2, 4, 5, 7, 8, 6, 3 },
                algorithm.SortedVertices);
        }
        public void Compute([PexAssumeNotNull]IUndirectedGraph<string, Edge<string>> g)
        {
            UndirectedTopologicalSortAlgorithm<string, Edge<string>> topo =
                new UndirectedTopologicalSortAlgorithm<string, Edge<string>>(g);
            topo.AllowCyclicGraph = true;
            topo.Compute();

            Display(topo);
        }
        public static (string[], string) Get(UndirectedGraph <string, TaggedEdge <string, string> > graph)
        {
            var algorithm = new UndirectedTopologicalSortAlgorithm <string, TaggedEdge <string, string> >(graph);

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

            algorithm.Compute();

            Assert.IsNotNull(algorithm.SortedVertices);
            Assert.AreEqual(graph.VertexCount, algorithm.SortedVertices.Length);
        }
        public void UndirectedTopologicalSort_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 UndirectedTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph);

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

            algorithm = new UndirectedTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph)
            {
                AllowCyclicGraph = true
            };
            Assert.DoesNotThrow(() => algorithm.Compute());
        }
        public void SimpleGraphOneToAnother()
        {
            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>(3, 4)
            });

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

            algorithm.Compute();

            // Order in undirected graph is some strange thing,
            // here the order is more vertices ordered by depth
            CollectionAssert.AreEqual(
                new[] { 0, 1, 3, 4, 2 },
                algorithm.SortedVertices);
        }