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 Constructor()
        {
            var graph     = new UndirectedGraph <int, Edge <int> >();
            var algorithm = new UndirectedFirstTopologicalSortAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

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

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

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

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                UndirectedFirstTopologicalSortAlgorithm <TVertex, TEdge> algo,
                IUndirectedGraph <TVertex, TEdge> g,
                bool allowCycles = false)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                Assert.IsNull(algo.SortedVertices);
                CollectionAssert.IsEmpty(algo.Degrees);
                Assert.AreEqual(allowCycles, algo.AllowCyclicGraph);
            }

            #endregion
        }
        public void Compute([PexAssumeNotNull]IUndirectedGraph<string, Edge<string>> g)
        {
            var topo =
                new UndirectedFirstTopologicalSortAlgorithm<string, Edge<string>>(g);
            topo.AllowCyclicGraph = true;
            topo.Compute();

            Display(topo);
        }
Ejemplo n.º 5
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());
        }
 private void Display(UndirectedFirstTopologicalSortAlgorithm<string, Edge<string>> topo)
 {
     int index = 0;
     foreach (string v in topo.SortedVertices)
         Console.WriteLine("{0}: {1}", index++, v);
 }