Exemplo n.º 1
0
    static void Main()
    {
        //var graph = new Dictionary<string, List<string>>()
        //{
        //    { "IDEs", new List<string>() { "variables", "loops" } },
        //    { "variables", new List<string>() { "conditionals", "loops", "bits" } },
        //    { "loops", new List<string>() { "bits" } },
        //    { "conditionals", new List<string>() { "loops" } },
        //    { "bits" , new List<string>() }
        //};

        var graph = new Dictionary<string, List<string>>() {
            { "A", new List<string>() { "B", "C" } },
            { "B", new List<string>() { "D", "E" } },
            { "C", new List<string>() { "F" } },
            { "D", new List<string>() { "C", "F" } },
            { "E", new List<string>() { "D" } },
            { "F", new List<string>() { } },
        };

        //var graph = ReadGraph();
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = topSorter.TopSort();

        Console.WriteLine("Topological sorting: {0}",
            string.Join(", ", sortedNodes));

        // Topological sorting: A, B, E, D, C, F
    }
Exemplo n.º 2
0
    public void TestTopSortAcyclicGraph5Vertices()
    {
        // Arrange
        var graph = new Dictionary <string, List <string> >()
        {
            { "IDEs", new List <string>()
              {
                  "variables", "loops"
              } },
            { "variables", new List <string>()
              {
                  "conditionals", "loops", "bits"
              } },
            { "loops", new List <string>()
              {
                  "bits"
              } },
            { "conditionals", new List <string>()
              {
                  "loops"
              } }
        };

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 3
0
    public void TestTopSortGraph7VerticesWithCycle()
    {
        // Arrange
        var graph = new Dictionary <string, List <string> >()
        {
            { "A", new List <string>()
              {
                  "B"
              } },
            { "B", new List <string>()
              {
                  "C"
              } },
            { "C", new List <string>()
              {
                  "D", "E"
              } },
            { "D", new List <string>()
              {
                  "E"
              } },
            { "E", new List <string>()
              {
                  "F", "C"
              } },
            { "Z", new List <string>()
              {
                  "A"
              } }
        };

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());
    }
Exemplo n.º 4
0
    public void TestPerformanceGraph1000Vertices()
    {
        // Arrange
        const int nodesCount = 1000;
        var       graph      = new Dictionary <string, List <string> >();

        for (int i = 0; i < nodesCount; i++)
        {
            graph["node" + i] = new List <string>();
        }
        for (int i = 0; i < nodesCount - 50; i++)
        {
            for (int c = 25; c < i % 50; c++)
            {
                graph["node" + i].Add("node" + (i + c));
            }
        }

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 5
0
    public void TestTopSortEmptyGraph()
    {
        // Arrange
        var graph = new Dictionary <string, List <string> >()
        {
        };

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 6
0
    public void TestTopSortAcyclicGraph2Vertices()
    {
        // Arrange
        var graph = new Dictionary<string, List<string>>() {
            { "First", new List<string>() { "Second" } }
        };

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 7
0
    static void Main()
    {
        //var graph = new Dictionary<string, List<string>>()
        //{
        //    { "IDEs", new List<string>() { "variables", "loops" } },
        //    { "variables", new List<string>() { "conditionals", "loops", "bits" } },
        //    { "loops", new List<string>() { "bits" } },
        //    { "conditionals", new List<string>() { "loops" } },
        //};

        var graph = new Dictionary <string, List <string> > {
            { "A", new List <string>()
              {
                  "B", "C"
              } },
            { "B", new List <string>()
              {
                  "D", "E"
              } },
            { "C", new List <string>()
              {
                  "F"
              } },
            { "D", new List <string>()
              {
                  "C", "F"
              } },
            { "E", new List <string>()
              {
                  "D"
              } },
            { "F", new List <string>()
              {
              } },
        };

        //var topSorter = new TopologicalSorter(graph);
        //var sortedNodes = topSorter.TopSort();

        var topSorterDfs   = new TopologicalSorterDFS(graph);
        var sortedNodesDfs = topSorterDfs.TopSort();

        //Console.WriteLine("Topological sorting: {0}",
        //    string.Join(", ", sortedNodes));

        Console.WriteLine("Topological sorting with DFS: {0}",
                          string.Join(", ", sortedNodesDfs));

        // Topological sorting: A, B, E, D, C, F
    }
Exemplo n.º 8
0
    public void TestTopSortAcyclicGraph5Vertices()
    {
        // Arrange
        var graph = new Dictionary<string, List<string>>() {
            { "IDEs", new List<string>() { "variables", "loops" } },
            { "variables", new List<string>() { "conditionals", "loops", "bits" } },
            { "loops", new List<string>() { "bits" } },
            { "conditionals", new List<string>() { "loops" } }
        };

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 9
0
    public void TestTopSortAcyclicGraph2Vertices()
    {
        // Arrange
        var graph = new Dictionary <string, List <string> >()
        {
            { "First", new List <string>()
              {
                  "Second"
              } }
        };

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 10
0
    public void TestTopSortAcyclicGraph8Vertices()
    {
        // Arrange
        var graph = new Dictionary <string, List <string> >()
        {
            { "H", new List <string>()
              {
                  "G"
              } },
            { "G", new List <string>()
              {
              } },
            { "B", new List <string>()
              {
                  "A"
              } },
            { "A", new List <string>()
              {
              } },
            { "F", new List <string>()
              {
                  "B", "C", "E"
              } },
            { "C", new List <string>()
              {
                  "A"
              } },
            { "E", new List <string>()
              {
                  "C", "A"
              } },
            { "D", new List <string>()
              {
                  "A", "B"
              } },
        };

        // Act
        var topSorter   = new TopologicalSorterDFS(graph);
        var sortedNodes = new List <string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 11
0
    public void TestPerformanceGraph1000Vertices()
    {
        // Arrange
        const int nodesCount = 1000;
        var graph = new Dictionary<string, List<string>>();
        for (int i = 0; i < nodesCount; i++)
        {
            graph["node" + i] = new List<string>();
        }
        for (int i = 0; i < nodesCount - 50; i++)
        {
            for (int c = 25; c < i % 50; c++)
            {
                graph["node" + i].Add("node" + (i + c));
            }
        }

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 12
0
    public void TestTopSortAcyclicGraph8Vertices()
    {
        // Arrange
        var graph = new Dictionary<string, List<string>>() {
            { "H", new List<string>() { "G" } },
            { "G", new List<string>() { } },
            { "B", new List<string>() { "A" } },
            { "A", new List<string>() { } },
            { "F", new List<string>() { "B", "C", "E" } },
            { "C", new List<string>() { "A" } },
            { "E", new List<string>() { "C", "A" } },
            { "D", new List<string>() { "A", "B" } },
        };

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }
Exemplo n.º 13
0
    public void TestTopSortGraph7VerticesWithCycle()
    {
        // Arrange
        var graph = new Dictionary<string, List<string>>()
        {
            { "A", new List<string>() { "B" } },
            { "B", new List<string>() { "C" } },
            { "C", new List<string>() { "D", "E" } },
            { "D", new List<string>() { "E" } },
            { "E", new List<string>() { "F", "C" } },
            { "Z", new List<string>() { "A" } }
        };

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());
    }
Exemplo n.º 14
0
    public void TestTopSortGraph1Vertex()
    {
        // Arrange
        var graph = new Dictionary<string, List<string>>() {
            { "A", new List<string>() { } }
        };

        // Act
        var topSorter = new TopologicalSorterDFS(graph);
        var sortedNodes = new List<string>(topSorter.TopSort());

        // Assert
        AssertTopologicallySorted(graph, sortedNodes);
    }