CondensateEdges <TVertex, TEdge>( #if !NET20 this #endif IBidirectionalGraph <TVertex, TEdge> visitedGraph, VertexPredicate <TVertex> vertexPredicate ) where TEdge : IEdge <TVertex> { //Contract.Requires(visitedGraph != null); //Contract.Requires(vertexPredicate != null); var condensated = new BidirectionalGraph <TVertex, MergedEdge <TVertex, TEdge> >(); var condensator = new EdgeMergeCondensationGraphAlgorithm <TVertex, TEdge>( visitedGraph, condensated, vertexPredicate); condensator.Compute(); return(condensated); }
/// <inheritdoc /> public int RemoveVertexIf(VertexPredicate <TVertex> predicate) { if (predicate is null) { throw new ArgumentNullException(nameof(predicate)); } var verticesToRemove = new VertexList <TVertex>(); verticesToRemove.AddRange(Vertices.Where(vertex => predicate(vertex))); foreach (TVertex vertex in verticesToRemove) { ClearAdjacentEdges(vertex); _adjacentEdges.Remove(vertex); } NotifyVerticesRemoved(verticesToRemove); return(verticesToRemove.Count); }
private static void RunEdgesCondensationAndCheck <TVertex, TEdge>( [NotNull] IBidirectionalGraph <TVertex, TEdge> graph, [NotNull] VertexPredicate <TVertex> predicate) where TEdge : IEdge <TVertex> { IMutableBidirectionalGraph <TVertex, MergedEdge <TVertex, TEdge> > condensedGraph = graph.CondensateEdges(predicate); Assert.IsNotNull(condensedGraph); Assert.LessOrEqual(condensedGraph.VertexCount, graph.VertexCount); TVertex[] vertices = condensedGraph.Vertices.ToArray(); foreach (MergedEdge <TVertex, TEdge> edge in condensedGraph.Edges) { Assert.Contains(edge.Source, vertices); Assert.Contains(edge.Target, vertices); Assert.Positive(edge.Edges.Count); Assert.Contains(edge.Edges.First().Source, vertices); Assert.Contains(edge.Edges.Last().Target, vertices); } }
public void Construction() { VertexPredicate <int> vertexPredicate = _ => true; EdgePredicate <int, Edge <int> > edgePredicate = _ => true; var graph = new AdjacencyGraph <int, Edge <int> >(); var filteredGraph = new FilteredVertexAndEdgeListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph, graph); graph = new AdjacencyGraph <int, Edge <int> >(false); filteredGraph = new FilteredVertexAndEdgeListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph, graph, false); #region Local function void AssertGraphProperties <TVertex, TEdge, TGraph>( FilteredVertexAndEdgeListGraph <TVertex, TEdge, TGraph> g, TGraph expectedGraph, bool parallelEdges = true) where TEdge : IEdge <TVertex> where TGraph : IVertexAndEdgeListGraph <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); AssertEmptyGraph(g); } #endregion }
public void MergeVertexIf(VertexPredicate <TVertex> vertexPredicate, EdgeFactory <TVertex, TEdge> edgeFactory) { Contract.Requires(vertexPredicate != null); Contract.Requires(edgeFactory != null); // storing vertices to merge var mergeVertices = new VertexList <TVertex>(this.VertexCount / 4); foreach (var v in this.Vertices) { if (vertexPredicate(v)) { mergeVertices.Add(v); } } // applying merge recursively foreach (var v in mergeVertices) { MergeVertex(v, edgeFactory); } }
public void data_updates_on_insert() { var a = new VertexPredicate <TestNode>(v => v.Id % 2 == 1); var subgraph = _graph.GetChildGraph(v => v.Id % 2 == 0); var subgraph2 = _graph.GetChildGraph(v => v.Id % 2 == 1); _graph.Vertices.First(x => x.Id == 12).Testdata = "12"; //insert var list = new List <Edge <TestNode> >(); var node1 = new TestNode(12) { Testdata = "13" }; //MUST NOT UPDATE WITH NEW DATA var node2 = new TestNode(126) { Testdata = "126" }; var edge = new Edge <TestNode>(node1, node2); list.Add(edge); _graph.AddVerticesAndEdgeRange(list); Assert.That(_graph.Vertices.Count() == 22); Assert.That(_graph.Edges.Count() == 21); Assert.That(subgraph.Vertices.Count() == 11); Assert.That(subgraph.Edges.Count() == 5); Assert.That(subgraph2.Vertices.Count() == 11); Assert.That(subgraph2.Edges.Count() == 6); Assert.That(_graph.Vertices.First(x => x.Id == 126).Testdata == "126"); Assert.That(subgraph.Vertices.First(x => x.Id == 126).Testdata == "126"); Assert.That(subgraph2.Vertices.Count(x => x.Id == 126) == 0); Assert.That(_graph.Vertices.First(x => x.Id == 12).Testdata == "12"); Assert.That(subgraph.Vertices.First(x => x.Id == 12).Testdata == "12"); Assert.That(subgraph2.Vertices.Count(x => x.Id == 12) == 0); }
public void Construction() { VertexPredicate <int> vertexPredicate = _ => true; EdgePredicate <int, Edge <int> > edgePredicate = _ => true; var graph = new UndirectedGraph <int, Edge <int> >(); var filteredGraph = new FilteredUndirectedGraph <int, Edge <int>, UndirectedGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph); graph = new UndirectedGraph <int, Edge <int> >(false); filteredGraph = new FilteredUndirectedGraph <int, Edge <int>, UndirectedGraph <int, Edge <int> > >( graph, vertexPredicate, edgePredicate); AssertGraphProperties(filteredGraph, false); #region Local function void AssertGraphProperties <TVertex, TEdge, TGraph>( FilteredUndirectedGraph <TVertex, TEdge, TGraph> g, bool parallelEdges = true) where TEdge : IEdge <TVertex> where TGraph : IUndirectedGraph <TVertex, TEdge> { Assert.AreSame(graph, g.BaseGraph); Assert.IsFalse(g.IsDirected); Assert.AreEqual(parallelEdges, g.AllowParallelEdges); Assert.AreSame(vertexPredicate, g.VertexPredicate); Assert.AreSame(edgePredicate, g.EdgePredicate); Assert.IsNotNull(g.EdgeEqualityComparer); AssertEmptyGraph(g); } #endregion }
/// <summary> /// Removes each vertex that matches the predicate vertex and creates new edges between all the vertices /// that were connected to the deleted vertex. /// </summary> /// <param name="vertexPredicate">The predicate that indicates which vertices need to be removed.</param> /// <param name="edgeFactory">The delegate that is used to create new edges.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="vertexPredicate"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="edgeFactory"/> is <see langword="null" />. /// </exception> public void MergeVertexIf(VertexPredicate <TVertex> vertexPredicate, EdgeFactory <TVertex, TEdge> edgeFactory) { { Lokad.Enforce.Argument(() => vertexPredicate); Lokad.Enforce.Argument(() => edgeFactory); } var list = new List <TVertex>(VertexCount / 4); foreach (var vertex in this.Vertices) { if (vertexPredicate(vertex)) { list.Add(vertex); } } foreach (var vertex in list) { this.MergeVertex(vertex, edgeFactory); } }
public FilteredGraph( TGraph baseGraph, VertexPredicate <TVertex> vertexPredicate, EdgePredicate <TVertex, TEdge> edgePredicate ) { if (baseGraph == null) { throw new ArgumentNullException("baseGraph"); } if (vertexPredicate == null) { throw new ArgumentNullException("vertexPredicate"); } if (edgePredicate == null) { throw new ArgumentNullException("edgePredicate"); } this.baseGraph = baseGraph; this.vertexPredicate = vertexPredicate; this.edgePredicate = edgePredicate; }
public void MergeVertexIf(VertexPredicate <TVertex> vertexPredicate, IEdgeFactory <TVertex, TEdge> edgeFactory) { GraphContracts.AssumeNotNull(vertexPredicate, "vertexPredicate"); GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory"); // storing vertices to merge VertexList mergeVertices = new VertexList(this.VertexCount / 4); foreach (TVertex v in this.Vertices) { if (vertexPredicate(v)) { mergeVertices.Add(v); } } // applying merge recursively foreach (TVertex v in mergeVertices) { MergeVertex(v, edgeFactory); } }
/// <summary> /// Removes all vertices that match the given predicate. /// </summary> /// <param name="pred">The predicate.</param> /// <returns>The number of vertices that were removed.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="pred"/> is <see langword="null" />. /// </exception> public int RemoveVertexIf(VertexPredicate <TVertex> pred) { { Lokad.Enforce.Argument(() => pred); } var verticesToRemove = new List <TVertex>(); foreach (var vertex in Current.Vertices) { if (pred(vertex)) { verticesToRemove.Add(vertex); } } foreach (var vertex in verticesToRemove) { RemoveVertex(vertex); } return(verticesToRemove.Count); }
/// <summary> /// Removes vertices matching the given <paramref name="vertexPredicate"/> and merges all their /// connections to other vertices. /// </summary> /// <param name="vertexPredicate">Predicate to match vertices.</param> /// <param name="edgeFactory">Factory method to create an edge.</param> public void MergeVerticesIf( VertexPredicate <TVertex> vertexPredicate, EdgeFactory <TVertex, TEdge> edgeFactory) { if (vertexPredicate is null) { throw new ArgumentNullException(nameof(vertexPredicate)); } if (edgeFactory is null) { throw new ArgumentNullException(nameof(edgeFactory)); } // Storing vertices to merge var mergeVertices = new VertexList <TVertex>(VertexCount / 4); mergeVertices.AddRange(Vertices.Where(vertex => vertexPredicate(vertex))); // Applying merge recursively foreach (TVertex vertex in mergeVertices) { MergeVertex(vertex, edgeFactory); } }
int IMutableVertexSet <TVertex> .RemoveVertexIf(VertexPredicate <TVertex> pred) { throw new NotImplementedException(); }
public void MergeIf_Test( [NotNull] IEnumerable <int> setupVertices, [NotNull, ItemNotNull] IEnumerable <EquatableEdge <int> > setupEdges, [NotNull, InstantHandle] VertexPredicate <int> vertexPredicate, int expectedVerticesRemoved, int expectedEdgesAdded, int expectedEdgesRemoved, [NotNull] IEnumerable <int> expectedVertices, [NotNull, ItemNotNull] IEnumerable <EquatableEdge <int> > expectedEdges) { int verticesAdded = 0; int edgesAdded = 0; int verticesRemoved = 0; int edgesRemoved = 0; var graph = new BidirectionalGraph <int, EquatableEdge <int> >(); graph.AddVertexRange(setupVertices); graph.AddEdgeRange(setupEdges); graph.VertexAdded += v => { Assert.IsNotNull(v); ++verticesAdded; }; graph.VertexRemoved += v => { Assert.IsNotNull(v); // ReSharper disable once AccessToModifiedClosure ++verticesRemoved; }; graph.EdgeAdded += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesAdded; }; graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; graph.MergeVerticesIf(vertexPredicate, (source, target) => new EquatableEdge <int>(source, target)); CheckCounters(); AssertHasVertices(graph, expectedVertices); EquatableEdge <int>[] edges = expectedEdges.ToArray(); if (!edges.Any()) { AssertNoEdge(graph); } else { AssertHasEdges(graph, edges); } #region Local function void CheckCounters() { Assert.AreEqual(0, verticesAdded); Assert.AreEqual(expectedVerticesRemoved, verticesRemoved); Assert.AreEqual(expectedEdgesAdded, edgesAdded); Assert.AreEqual(expectedEdgesRemoved, edgesRemoved); verticesRemoved = 0; edgesAdded = 0; edgesRemoved = 0; } #endregion }
public ImmutableBidirectionalGraph <TVertex, TEdge> RemoveVertexIf(VertexPredicate <TVertex> p) => CloneAndMutate(i => i.RemoveVertexIf(p));