public virtual void RemoveVertex(T vertex) { if (vertex == null) { Log.Error("Vertex is null."); return; } var affectedEdges = Edges .Where(e => e.Source == vertex || e.Target == vertex) .ToArray(); var neighbors = affectedEdges .Select(e => e.Opposite(vertex)) .Except(new[] { vertex }); // a self edge would make the vertex its own neighbor foreach (var edge in affectedEdges) { RemoveEdge(edge); } if (!VerticesData.Remove(vertex)) { Log.Error("Vertex is not a member: " + vertex); return; } // maintain root data recalcIsTree = true; RootVertices.Remove(vertex); // add the targets that have no more in-edges RootVertices.UnionWith(neighbors.Where(v => !HasParents(v))); }
public virtual void AddEdge(Relation <T, P> relation) { if (relation == null || relation.Source == null || relation.Target == null) { Log.Error("Relation or vertex is null."); return; } if (!VerticesData.ContainsKey(relation.Source)) { Log.Error("Relation source is missing from the graph: " + relation.Source); return; } if (!VerticesData.ContainsKey(relation.Target)) { Log.Error("Relation target is missing from the graph: " + relation.Target); return; } if (!Edges.Add(relation)) { Log.Error("Relation is already part of the graph: " + relation); return; } VerticesData[relation.Source].OutEdges.Add(relation); VerticesData[relation.Target].InEdges.Add(relation); // maintain root data if (!relation.IsSelfRelation) { recalcIsTree = true; RootVertices.Remove(relation.Target); } }