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 }
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); }
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()); }
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); }
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); }
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); }
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 }
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); }
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); }
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); }
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); }
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); }
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()); }
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); }