/// <inheritdoc />
        public int RemoveOutEdgeIf(TVertex vertex, EdgePredicate <TVertex, TEdge> predicate)
        {
            if (vertex == null)
            {
                throw new ArgumentNullException(nameof(vertex));
            }
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            IEdgeList <TVertex, TEdge> edges = _vertexEdges[vertex];
            var edgesToRemove = new EdgeList <TVertex, TEdge>(edges.Count);

            edgesToRemove.AddRange(edges.Where(edge => predicate(edge)));

            foreach (TEdge edge in edgesToRemove)
            {
                edges.Remove(edge);
                OnEdgeRemoved(edge);
            }

            EdgeCount -= edgesToRemove.Count;

            return(edgesToRemove.Count);
        }
Exemple #2
0
        /// <summary>
        /// Removes all out-edges of the <paramref name="vertex"/>
        /// where the <paramref name="predicate"/> is evaluated to true.
        /// </summary>
        /// <param name="vertex">The vertex.</param>
        /// <param name="predicate">Predicate to remove edges.</param>
        /// <returns>The number of removed edges.</returns>
        public int RemoveOutEdgeIf(int vertex, [NotNull, InstantHandle] EdgePredicate <int, TEdge> predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }
            if (!IsInGraph(vertex))
            {
                return(0);
            }

            int count = 0;

            for (int j = 0; j < VertexCount; ++j)
            {
                TEdge edge = _edges[vertex, j];
                if (edge != null && predicate(edge))
                {
                    RemoveEdge(edge);
                    ++count;
                }
            }

            return(count);
        }
Exemple #3
0
        public int RemoveOutEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(predicate, "predicate");

            EdgeList edges        = this.vertexEdges[v];
            EdgeList edgeToRemove = new EdgeList(edges.Count);

            foreach (TEdge edge in edges)
            {
                if (predicate(edge))
                {
                    edgeToRemove.Add(edge);
                }
            }
            foreach (TEdge edge in edgeToRemove)
            {
                edges.Remove(edge);
                this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
            }
            this.edgeCount -= edgeToRemove.Count;
            GraphContracts.Assert(this.edgeCount >= 0);

            return(edgeToRemove.Count);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilteredIncidenceGraph{TVertex,TEdge,TGraph}"/> class.
 /// </summary>
 /// <param name="baseGraph">Graph in which applying predicates.</param>
 /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
 /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
 public FilteredIncidenceGraph(
     TGraph baseGraph,
     VertexPredicate <TVertex> vertexPredicate,
     EdgePredicate <TVertex, TEdge> edgePredicate)
     : base(baseGraph, vertexPredicate, edgePredicate)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilteredUndirectedGraph{TVertex,TEdge,TGraph}"/> class.
 /// </summary>
 /// <param name="baseGraph">Graph in which applying predicates.</param>
 /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
 /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
 public FilteredUndirectedGraph(
     TGraph baseGraph,
     VertexPredicate <TVertex> vertexPredicate,
     EdgePredicate <TVertex, TEdge> edgePredicate)
     : base(baseGraph, vertexPredicate, edgePredicate)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilteredBidirectionalGraph{TVertex,TEdge,TGraph}"/> class.
 /// </summary>
 /// <param name="baseGraph">Graph in which applying predicates.</param>
 /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
 /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
 public FilteredBidirectionalGraph(
     [NotNull] TGraph baseGraph,
     [NotNull] VertexPredicate <TVertex> vertexPredicate,
     [NotNull] EdgePredicate <TVertex, TEdge> edgePredicate)
     : base(baseGraph, vertexPredicate, edgePredicate)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilteredVertexAndEdgeListGraph{TVertex,TEdge,TGraph}"/> class.
 /// </summary>
 /// <param name="baseGraph">Graph in which applying predicates.</param>
 /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
 /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
 public FilteredVertexAndEdgeListGraph(
     TGraph baseGraph,
     VertexPredicate <TVertex> vertexPredicate,
     EdgePredicate <TVertex, TEdge> edgePredicate)
     : base(baseGraph, vertexPredicate, edgePredicate)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilteredImplicitGraph{TVertex,TEdge,TGraph}"/> class.
 /// </summary>
 /// <param name="baseGraph">Graph in which applying predicates.</param>
 /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
 /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="baseGraph"/> is <see langword="null"/>.</exception>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="vertexPredicate"/> is <see langword="null"/>.</exception>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="edgePredicate"/> is <see langword="null"/>.</exception>
 public FilteredImplicitGraph(
     [NotNull] TGraph baseGraph,
     [NotNull] VertexPredicate <TVertex> vertexPredicate,
     [NotNull] EdgePredicate <TVertex, TEdge> edgePredicate)
     : base(baseGraph, vertexPredicate, edgePredicate)
 {
 }
        /// <inheritdoc />
        public int RemoveEdgeIf(EdgePredicate<TVertex, TEdge> predicate)
        {
            if (predicate is null)
                throw new ArgumentNullException(nameof(predicate));

            var edgesToRemove = new EdgeList<TVertex, TEdge>();
            edgesToRemove.AddRange(Edges.Where(edge => predicate(edge)));

            return RemoveEdgesInternal(edgesToRemove);
        }
        /// <inheritdoc />
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            return(RemoveEdges(
                       Edges.Where(edge => predicate(edge)).ToArray()));
        }
        public int RemoveOutEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            int edgeToRemoveCount = wrapped.RemoveOutEdgeIf(v, predicate);

            if (parent != null)
            {
                parent.RemoveOutEdgeIf(v, predicate);
            }
            return(edgeToRemoveCount);
        }
Exemple #12
0
        public void Construction()
        {
            VertexPredicate <int>            vertexPredicate = _ => true;
            EdgePredicate <int, Edge <int> > edgePredicate   = _ => true;

            var graph1         = new AdjacencyGraph <int, Edge <int> >();
            var filteredGraph1 = new FilteredEdgeListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >(
                graph1,
                vertexPredicate,
                edgePredicate);

            AssertGraphProperties(filteredGraph1, graph1);

            graph1         = new AdjacencyGraph <int, Edge <int> >(false);
            filteredGraph1 = new FilteredEdgeListGraph <int, Edge <int>, AdjacencyGraph <int, Edge <int> > >(
                graph1,
                vertexPredicate,
                edgePredicate);
            AssertGraphProperties(filteredGraph1, graph1, parallelEdges: false);

            var graph2         = new UndirectedGraph <int, Edge <int> >();
            var filteredGraph2 = new FilteredEdgeListGraph <int, Edge <int>, UndirectedGraph <int, Edge <int> > >(
                graph2,
                vertexPredicate,
                edgePredicate);

            AssertGraphProperties(filteredGraph2, graph2, false);

            graph2         = new UndirectedGraph <int, Edge <int> >(false);
            filteredGraph2 = new FilteredEdgeListGraph <int, Edge <int>, UndirectedGraph <int, Edge <int> > >(
                graph2,
                vertexPredicate,
                edgePredicate);
            AssertGraphProperties(filteredGraph2, graph2, false, false);

            #region Local function

            void AssertGraphProperties <TVertex, TEdge, TGraph>(
                FilteredEdgeListGraph <TVertex, TEdge, TGraph> g,
                TGraph expectedGraph,
                bool isDirected    = true,
                bool parallelEdges = true)
                where TEdge : IEdge <TVertex>
                where TGraph : IEdgeListGraph <TVertex, TEdge>
            {
                Assert.AreSame(expectedGraph, g.BaseGraph);
                Assert.AreEqual(isDirected, g.IsDirected);
                Assert.AreEqual(parallelEdges, g.AllowParallelEdges);
                Assert.AreSame(vertexPredicate, g.VertexPredicate);
                Assert.AreSame(edgePredicate, g.EdgePredicate);
                AssertEmptyGraph(g);
            }

            #endregion
        }
        public IsEulerianGraphAlgorithm(UndirectedGraph <TVertex, UndirectedEdge <TVertex> > graph)
        {
            var newGraph = new UndirectedGraph <TVertex, UndirectedEdge <TVertex> >(false, graph.EdgeEqualityComparer);

            newGraph.AddVertexRange(graph.Vertices);
            newGraph.AddEdgeRange(graph.Edges);
            EdgePredicate <TVertex, UndirectedEdge <TVertex> > isLoop = e => e.Source.Equals(e.Target);

            newGraph.RemoveEdgeIf(isLoop);
            this.graph = newGraph;
        }
        public int RemoveEdgeIf <TVertex, TEdge>(
            [PexAssumeUnderTest] EdgeListGraph <TVertex, TEdge> target,
            EdgePredicate <TVertex, TEdge> predicate
            )
            where TEdge : IEdge <TVertex>
        {
            // TODO: add assertions to method EdgeListGraphTVertexTEdgeTest.RemoveEdgeIf(EdgeListGraph`2<!!0,!!1>, EdgePredicate`2<!!0,!!1>)
            int result = target.RemoveEdgeIf(predicate);

            return(result);
        }
Exemple #15
0
        int IMutableEdgeListGraph <TVertex, TEdge> .RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            IMutableEdgeListGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(predicate != null);
            Contract.Ensures(Contract.Result <int>() == Contract.OldValue(Enumerable.Count(ithis.Edges, e => predicate(e))));
            Contract.Ensures(Enumerable.All(ithis.Edges, e => !predicate(e)));
            Contract.Ensures(ithis.EdgeCount == Contract.OldValue(ithis.EdgeCount) - Contract.Result <int>());

            return(default(int));
        }
        public int RemoveAdjacentEdgeIf <TVertex, TEdge>(
            [PexAssumeUnderTest] UndirectedGraph <TVertex, TEdge> target,
            TVertex v,
            EdgePredicate <TVertex, TEdge> predicate
            )
            where TEdge : IEdge <TVertex>
        {
            // TODO: add assertions to method UndirectedGraphTVertexTEdgeTest.RemoveAdjacentEdgeIf(UndirectedGraph`2<!!0,!!1>, !!0, EdgePredicate`2<!!0,!!1>)
            int result = target.RemoveAdjacentEdgeIf(v, predicate);

            return(result);
        }
        int IMutableUndirectedGraph <TVertex, TEdge> .RemoveAdjacentEdgeIf(
            TVertex vertex,
            EdgePredicate <TVertex, TEdge> predicate)
        {
            IMutableUndirectedGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(vertex != null);
            Contract.Requires(predicate != null);
            Contract.Ensures(Contract.Result <int>() == Contract.OldValue(Enumerable.Count(ithis.AdjacentEdges(vertex), e => predicate(e))));
            Contract.Ensures(Enumerable.All(ithis.AdjacentEdges(vertex), v => !predicate(v)));

            return(default(int));
        }
Exemple #18
0
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            List <TEdge> edges = new List <TEdge>();

            foreach (var edge in this.Edges)
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }
            return(this.RemoveEdges(edges));
        }
Exemple #19
0
        public IsHamiltonianGraphAlgorithm(UndirectedGraph <TVertex, UndirectedEdge <TVertex> > graph)
        {
            // Create new graph without parallel edges
            var newGraph = new UndirectedGraph <TVertex, UndirectedEdge <TVertex> >(false, graph.EdgeEqualityComparer);

            newGraph.AddVertexRange(graph.Vertices);
            newGraph.AddEdgeRange(graph.Edges);
            // Remove loops
            EdgePredicate <TVertex, UndirectedEdge <TVertex> > isLoop = e => e.Source.Equals(e.Target);

            newGraph.RemoveEdgeIf(isLoop);
            this.graph = newGraph;
            threshold  = newGraph.VertexCount / 2.0;
        }
        int IMutableBidirectionalGraph <TVertex, TEdge> .RemoveInEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            IMutableBidirectionalGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(predicate != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(ithis.ContainsVertex(v));
            Contract.Ensures(Enumerable.All(ithis.InEdges(v), e => predicate(e)));
            Contract.Ensures(Contract.Result <int>() == Contract.OldValue(Enumerable.Count(ithis.InEdges(v), e => predicate(e))));
            Contract.Ensures(ithis.InDegree(v) == Contract.OldValue(ithis.InDegree(v)) - Contract.Result <int>());

            return(default(int));
        }
        public bool RemoveEdge(EdgePredicate <TVertex, TEdge> edgePredicate)
        {
            TEdge edge;

            lock (SyncRoot)
                edge = Graph.Edges.FirstOrDefault(i => edgePredicate(i));

            if (edge == null)
            {
                return(false);
            }

            return(this.RemoveEdge(edge));
        }
Exemple #22
0
        int IMutableIncidenceGraph <TVertex, TEdge> .RemoveOutEdgeIf(
            TVertex v,
            EdgePredicate <TVertex, TEdge> predicate)
        {
            IMutableIncidenceGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Requires(predicate != null);
            Contract.Ensures(Contract.Result <int>() == Contract.OldValue(ithis.OutEdges(v).Count(ve => predicate(ve))));
            Contract.Ensures(ithis.OutEdges(v).All(ve => !predicate(ve)));

            return(default(int));
        }
Exemple #23
0
        public IEnumerable <TVertex> GetAdjacentVertices(TVertexId vertexId, EdgeDirection direction,
                                                         EdgePredicate <TVertex, TEdge> edgePredicate = null, bool recursive = false)
        {
            if (!ContainsVertex(vertexId))
            {
                return(Enumerable.Empty <TVertex>());
            }

            var vertexIds = _graph.GetAdjacentVertices(vertexId, direction,
                                                       edge => edgePredicate == null || edgePredicate(GetEdge(edge.Id)),
                                                       recursive);

            return(vertexIds.Select(GetVertex));
        }
        /// <inheritdoc />
        public int RemoveInEdgeIf(TVertex vertex, EdgePredicate<TVertex, TEdge> predicate)
        {
            if (predicate is null)
                throw new ArgumentNullException(nameof(predicate));

            if (_vertexInEdges.TryGetValue(vertex, out IEdgeList<TVertex, TEdge> inEdges))
            {
                var edgesToRemove = new EdgeList<TVertex, TEdge>();
                edgesToRemove.AddRange(inEdges.Where(edge => predicate(edge)));
                return RemoveEdgesInternal(edgesToRemove);
            }

            return 0;
        }
Exemple #25
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FilteredGraph{TVertex,TEdge,TGraph}"/> class.
        /// </summary>
        /// <param name="baseGraph">Graph in which applying predicates.</param>
        /// <param name="vertexPredicate">Predicate to match vertex that should be taken into account.</param>
        /// <param name="edgePredicate">Predicate to match edge that should be taken into account.</param>
        public FilteredGraph(
            [NotNull] TGraph baseGraph,
            [NotNull] VertexPredicate <TVertex> vertexPredicate,
            [NotNull] EdgePredicate <TVertex, TEdge> edgePredicate)
        {
            if (baseGraph == null)
            {
                throw new ArgumentNullException(nameof(baseGraph));
            }

            BaseGraph       = baseGraph;
            VertexPredicate = vertexPredicate ?? throw new ArgumentNullException(nameof(vertexPredicate));
            EdgePredicate   = edgePredicate ?? throw new ArgumentNullException(nameof(edgePredicate));
        }
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeNotNull(predicate, "predicate");

            List <TEdge> edges = new List <TEdge>();

            foreach (var edge in this.Edges)
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }
            return(this.RemoveEdges(edges));
        }
        /// <inheritdoc />
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            var edgesToRemove = Edges.Where(edge => predicate(edge)).ToArray();

            foreach (TEdge edge in edgesToRemove)
            {
                _edges.Remove(edge);
            }
            return(edgesToRemove.Length);
        }
        public int RemoveInEdgeIf(int v, EdgePredicate <int, TEdge> edgePredicate)
        {
            int count = 0;

            for (int i = 0; i < this.VertexCount; ++i)
            {
                TEdge e = this.edges[i, v];
                if (e != null && edgePredicate(e))
                {
                    this.RemoveEdge(e);
                    count++;
                }
            }
            return(count);
        }
        public int RemoveOutEdgeIf(int v, EdgePredicate <int, TEdge> predicate)
        {
            int count = 0;

            for (int j = 0; j < this.VertexCount; ++j)
            {
                TEdge e = this.edges[v, j];
                if (e != null && predicate(e))
                {
                    this.RemoveEdge(e);
                    count++;
                }
            }
            return(count);
        }
Exemple #30
0
        public int RemoveAdjacentEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            var outEdges = this._adjacentEdges[v];
            var edges    = new List <TEdge>(outEdges.Count);

            foreach (var edge in outEdges)
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }

            this.RemoveEdges(edges);
            return(edges.Count);
        }