Beispiel #1
0
        public void TopologicalSortOnWeightedGraph()
        {
            var vertices = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            var graph = new DirectedWeightedALGraph <int, int>();

            graph.AddVertices(vertices);

            // Graph is:
            // 1 -> 3, 4
            // 2 -> 5
            // 2 <- 3, 6
            // 3 -> 2, 6
            // 3 <- 1, 4
            // 4 -> 3, 6, 7
            // 4 <- 1
            // 5 -> 7, 8
            // 5 <- 2, 8
            // 6 -> 2
            // 6 <- 3, 4
            // 7 -> 4
            // 7 <- 5, 8
            // 8 -> 7
            // 8 <- 5
            // 9 ->
            // 9 <-
            // Edges weight is the sum of the vertices
            graph.AddEdge(1, 3, 4);
            graph.AddEdge(1, 4, 5);
            graph.AddEdge(2, 5, 7);
            graph.AddEdge(3, 2, 5);
            graph.AddEdge(3, 6, 9);
            graph.AddEdge(4, 3, 7);
            graph.AddEdge(4, 6, 10);
            graph.AddEdge(4, 7, 11);
            graph.AddEdge(5, 7, 12);
            graph.AddEdge(5, 8, 13);
            graph.AddEdge(6, 2, 8);
            graph.AddEdge(8, 7, 15);

            // Expected topological sort
            // 1 4 3 6 2 5 8 7 9
            var expected = new int[] { 1, 4, 3, 6, 2, 5, 8, 7, 9 };
            var topSort  = graph.TopologicalSortSmallestFirst();

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.IsTrue(topSort[i] == expected[i]);
            }
        }