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 TopologicalSorter(graph); var sortedNodes = new List <string>(topSorter.TopSort()); // Assert AssertTopologicallySorted(graph, sortedNodes); }
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 TopologicalSorter(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 TopologicalSorter(graph); var sortedNodes = new List <string>(topSorter.TopSort()); }
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(); Console.WriteLine("Topological sorting: {0}", string.Join(", ", sortedNodes)); // Topological sorting: A, B, E, D, C, F }
public void TestTopSortGraph2VerticesWithCycle() { // Arrange var graph = new Dictionary <string, List <string> >() { { "First", new List <string>() { "Second" } }, { "Second", new List <string>() { "First" } } }; // Act // Assert Assert.Throws <InvalidOperationException>(() => { var topSorter = new TopologicalSorter(graph); var sortedNodes = new List <string>(topSorter.TopSort()); AssertTopologicallySorted(graph, sortedNodes); }); }
public static void Main() { //var graph = new Dictionary<string, List<string>>() //{ // { "5", new List<string>() { "11" } }, // { "7", new List<string>() { "8", "11" } }, // { "8", new List<string>() { "9" } }, // { "11", new List<string>() { "2", "9", "10" } }, // { "9", new List<string>() { } } , // { "3", new List<string>() { "8", "10" } } , // { "2", new List<string>() { } } , // { "10", new List<string>() { } } , //}; //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>() { } } // corrected //}; 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(); Console.WriteLine("Topological sorting: {0}", string.Join(", ", sortedNodes)); // Topological sorting: A, B, E, D, C, F }
public void TestTopSortAcyclicGraph2Vertices() { // Arrange var graph = new Dictionary<string, List<string>> { { "First", new List<string> { "Second" } }, { "Second", new List<string> { } } }; // Act var topSorter = new TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert this.AssertTopologicallySorted(graph, sortedNodes); }
public void TestTopSortEmptyGraph() { // Arrange var graph = new Dictionary <string, List <string> >() { }; // Act var topSorter = new TopologicalSorter(graph); var sortedNodes = new List <string>(topSorter.TopSort()); // Assert AssertTopologicallySorted(graph, sortedNodes); }
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 TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert AssertTopologicallySorted(graph, sortedNodes); }
public static void Main() { // var customComparer = new CustomComparer(); var graph = new Dictionary <string, List <string> >(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] line = Console.ReadLine().Split(); if (line[2] == "before") { if (!graph.ContainsKey(line[0])) { graph.Add(line[0], new List <string>()); } if (!graph.ContainsKey(line[3])) { graph.Add(line[3], new List <string>()); } graph[line[0]].Add(line[3]); } else { if (!graph.ContainsKey(line[3])) { graph.Add(line[3], new List <string>()); } if (!graph.ContainsKey(line[0])) { graph.Add(line[0], new List <string>()); } graph[line[3]].Add(line[0]); } } var sorter = new TopologicalSorter(graph); var result = sorter.TopSort(); Console.WriteLine(string.Join("", result)); }
public void TestTopSortAcyclicGraph2Vertices() { // Arrange var graph = new Dictionary <string, List <string> >() { { "First", new List <string>() { "Second" } } }; // Act var topSorter = new TopologicalSorter(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 TopologicalSorter(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" } }, { "F", new List <string>() { } }, { "Z", new List <string>() { "A" } } }; // Act Assert.Throws <InvalidOperationException>(() => { var topSorter = new TopologicalSorter(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 TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert this.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 TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert this.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 TopologicalSorter(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 TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert this.AssertTopologicallySorted(graph, sortedNodes); }
public void TestTopSortEmptyGraph() { // Arrange var graph = new Dictionary<string, List<string>>() { }; // Act var topSorter = new TopologicalSorter(graph); var sortedNodes = new List<string>(topSorter.TopSort()); // Assert AssertTopologicallySorted(graph, sortedNodes); }