/// <summary> /// Selects a node in graph using path relative to <c>startingNode</c> or graph roots, as provided by <see cref="GetRootNodes(DirectedGraph)"/> /// </summary> /// <param name="graph">The graph.</param> /// <param name="path">The path.</param> /// <param name="pathSeparator">The path separator.</param> /// <param name="startingNode">The starting node.</param> /// <returns>Null if path failed or node</returns> public static Node SelectNode(this DirectedGraph graph, String path, String pathSeparator = "/", Node startingNode = null) { var pathParts = path.SplitSmart(pathSeparator, "", true, true); Node head = startingNode; foreach (var pathPart in pathParts) { if (head == null) { // looking for root node var roots = graph.GetRootNodes(); head = roots.FirstOrDefault(x => x.Id.Equals(pathPart)); } else { var children = graph.GetLinked(head, false); head = children.FirstOrDefault(x => x.Id.Equals(pathPart)); // looking for outbound links } if (head == null) { break; } } return(head); }
/// <summary> /// Gets all linked nodes . /// </summary> /// <param name="node">The node.</param> /// <param name="inverse">if set to <c>true</c> [inverse].</param> /// <param name="distanceLimit">The distance limit.</param> /// <returns></returns> public static List <Node> GetAllLinked(this DirectedGraph graph, Node node, Boolean inverse = false, Int32 distanceLimit = 100) { List <Node> output = new List <Node>(); List <Node> iteration = new List <Node> { node }; Int32 c = 0; while (iteration.Any()) { List <Node> newIteration = new List <Node>(); foreach (var i in iteration) { newIteration.AddRange(graph.GetLinked(i, inverse)); } output.AddRange(newIteration); iteration = newIteration; c++; if (c > distanceLimit) { break; } } return(output); }