/// <summary> /// Iteratively removes the dangling links from the rank map /// </summary> public void RemoveDanglingLinks() { VertexCollection danglings = new VertexCollection(); do { danglings.Clear(); // create filtered graph IVertexListGraph fg = new FilteredVertexListGraph( this.VisitedGraph, new InDictionaryVertexPredicate(this.ranks) ); // iterate over of the vertices in the rank map foreach (IVertex v in this.ranks.Keys) { // if v does not have out-edge in the filtered graph, remove if (fg.OutDegree(v) == 0) { danglings.Add(v); } } // remove from ranks foreach (IVertex v in danglings) { this.ranks.Remove(v); } // iterate until no dangling was removed }while(danglings.Count != 0); }
public void OutEdges_Throws() { var graph1 = new AdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> >(); var filteredGraph1 = new FilteredVertexListGraph < EquatableTestVertex, Edge <EquatableTestVertex>, AdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> > >( graph1, _ => true, _ => true); OutEdges_NullThrows_Test(filteredGraph1); OutEdges_Throws_Test(filteredGraph1); var graph2 = new AdjacencyGraph <int, Edge <int> >(); var filteredGraph2 = new FilteredVertexListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph2, vertex => vertex < 4, _ => true); graph2.AddVertexRange(new[] { 1, 2, 3, 4, 5 }); // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => filteredGraph2.OutEdges(4)); Assert.Throws <VertexNotFoundException>(() => filteredGraph2.OutEdges(5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public void TryGetEdges_Throws() { var filteredGraph = new FilteredVertexListGraph <TestVertex, Edge <TestVertex>, AdjacencyGraph <TestVertex, Edge <TestVertex> > >( new AdjacencyGraph <TestVertex, Edge <TestVertex> >(), _ => true, _ => true); TryGetEdges_Throws_Test(filteredGraph); }
public void ContainsEdge_Throws() { var filteredGraph = new FilteredVertexListGraph <TestVertex, Edge <TestVertex>, AdjacencyGraph <TestVertex, Edge <TestVertex> > >( new AdjacencyGraph <TestVertex, Edge <TestVertex> >(), _ => true, _ => true); ContainsEdge_SourceTarget_Throws_Test(filteredGraph); }
public void ContainsVertex_Throws() { var filteredGraph = new FilteredVertexListGraph <TestVertex, Edge <TestVertex>, AdjacencyGraph <TestVertex, Edge <TestVertex> > >( new AdjacencyGraph <TestVertex, Edge <TestVertex> >(), vertex => true, edge => true); ContainsVertex_Throws_Test(filteredGraph); }
private bool IsOptimal(MaximumFlowAlgorithm maxFlow) { // check if mincut is saturated... FilteredVertexListGraph residualGraph = new FilteredVertexListGraph( maxFlow.VisitedGraph, new ReversedResidualEdgePredicate(maxFlow.ResidualCapacities, maxFlow.ReversedEdges) ); BreadthFirstSearchAlgorithm bfs = new BreadthFirstSearchAlgorithm(residualGraph); VertexIntDictionary distances = new VertexIntDictionary(); DistanceRecorderVisitor vis = new DistanceRecorderVisitor(distances); bfs.RegisterDistanceRecorderHandlers(vis); bfs.Compute(sink); return distances[source] >= maxFlow.VisitedGraph.VerticesCount; }
public void OutEdge_Throws() { var graph1 = new AdjacencyGraph <int, Edge <int> >(); OutEdge_Throws_Test( graph1, (vertexPredicate, edgePredicate) => new FilteredVertexListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph1, vertexPredicate, edgePredicate)); var graph2 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var filteredGraph2 = new FilteredVertexListGraph <TestVertex, Edge <TestVertex>, AdjacencyGraph <TestVertex, Edge <TestVertex> > >( graph2, _ => true, _ => true); OutEdge_NullThrows_Test(filteredGraph2); }
public void Construction() { VertexPredicate <int> vertexPredicate = _ => true; EdgePredicate <int, Edge <int> > edgePredicate = _ => true; var graph = new AdjacencyGraph <int, Edge <int> >(); var filteredGraph = new FilteredVertexListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph, graph); graph = new AdjacencyGraph <int, Edge <int> >(false); filteredGraph = new FilteredVertexListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph, graph, false); #region Local function void AssertGraphProperties <TVertex, TEdge, TGraph>( FilteredVertexListGraph <TVertex, TEdge, TGraph> g, TGraph expectedGraph, bool parallelEdges = true) where TEdge : IEdge <TVertex> where TGraph : IVertexListGraph <TVertex, TEdge> { Assert.AreSame(expectedGraph, g.BaseGraph); Assert.IsTrue(g.IsDirected); Assert.AreEqual(parallelEdges, g.AllowParallelEdges); Assert.AreSame(vertexPredicate, g.VertexPredicate); Assert.AreSame(edgePredicate, g.EdgePredicate); AssertNoVertex(g); } #endregion }