/// <summary> /// Gets the incoming edges to a key, regardless if the node has been added or not. /// </summary> public List <TKey> GetIncoming(TKey key) { if (!IncomingEdges.TryGetValue(key, out HashSet <TKey> edges)) { return(new List <TKey>()); } return(edges.ToList()); }
private IDictionary <TVertex, IList <TEdge> > GetIncomingEdgesMap(TVertex vertex) { if (IncomingEdges.TryGetValue(vertex, out var map)) { return(map); } map = InnerMap; OutgoingEdges[vertex] = InnerMap; IncomingEdges[vertex] = map; return(map); }
private void AddEdges(TKey key, IList <TKey> outgoing) { OutgoingEdges.Add(key, new HashSet <TKey>(outgoing)); foreach (var dest in outgoing) { if (!IncomingEdges.TryGetValue(dest, out HashSet <TKey> incoming)) { IncomingEdges[dest] = new HashSet <TKey> { key } } ; else { incoming.Add(key); } } }
/// <summary> /// Checks if adding the node causes a cycle. /// </summary> public Boolean CausesCycle(TKey key, IList <TKey> outgoing) { if (outgoing.Contains(key)) { return(true); // Self cycle } if (!IncomingEdges.TryGetValue(key, out HashSet <TKey> incoming) || incoming.Count == 0) { return(false); // No incoming edges, so we can't have a cycle. } // If a path exists from any outgoing edge to any incoming edge, adding the node will cause a cycle. foreach (TKey start in outgoing) { foreach (TKey end in incoming) { if (PathExists(start, end)) { return(true); } } } return(false); }
public IEnumerable <TVertex> GetParents(TVertex vertex) => IncomingEdges.TryGetValue(vertex, out var parentMap) ? parentMap.Keys : Enumerable.Empty <TVertex>();