public void Compute([PexAssumeNotNull]IUndirectedGraph<string, Edge<string>> g) { UndirectedTopologicalSortAlgorithm<string, Edge<string>> topo = new UndirectedTopologicalSortAlgorithm<string, Edge<string>>(g); topo.AllowCyclicGraph = true; topo.Compute(); Display(topo); }
public void UndirectedTopologicalSort_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 UndirectedTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph); Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute()); algorithm = new UndirectedTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph) { AllowCyclicGraph = true }; Assert.DoesNotThrow(() => algorithm.Compute()); }
public void SimpleGraphOneToAnother() { 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>(3, 4) }); var algorithm = new UndirectedTopologicalSortAlgorithm <int, Edge <int> >(graph); algorithm.Compute(); // Order in undirected graph is some strange thing, // here the order is more vertices ordered by depth CollectionAssert.AreEqual( new[] { 0, 1, 3, 4, 2 }, algorithm.SortedVertices); }
private void Display(UndirectedTopologicalSortAlgorithm<string, Edge<string>> topo) { int index = 0; foreach (string v in topo.SortedVertices) Console.WriteLine("{0}: {1}", index++, v); }