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 RemoveEdge(Relation <T, P> relation) { if (relation == null) { Log.Error("Relation is null."); return; } if (!Edges.Remove(relation)) { Log.Error("Relation is not part of the graph: " + relation); return; } VerticesData[relation.Source].OutEdges.Remove(relation); VerticesData[relation.Target].InEdges.Remove(relation); if (!relation.IsSelfRelation) { recalcIsTree = true; if (!HasParents(relation.Target)) { RootVertices.Add(relation.Target); } } }
public virtual void AddVertex(T vertex) { if (vertex == null) { Log.Error("Vertex is null."); return; } if (VerticesData.ContainsKey(vertex)) { Log.Error("Vertex is already part of the graph: " + vertex); return; } VerticesData[vertex] = new VertexData <T, P>(vertex); recalcIsTree = true; RootVertices.Add(vertex); }
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); } }
public bool IsRoot(T vertex) { return(RootVertices.Contains(vertex)); }