private Subgraph FindSubgraph(Node node) { Subgraph subgraph = new Subgraph(graph); AddReachable(node, subgraph); return(subgraph); }
/// <summary> Adds all nodes and edges reachable from this node to the subgraph. /// Uses an explicit stack to avoid a large depth of recursion. /// /// </summary> /// <param name="node">a node known to be in the subgraph /// </param> private void AddReachable(Node startNode, Subgraph subgraph) { Stack nodeStack = new Stack(); nodeStack.Push(startNode); while (!(nodeStack.Count == 0)) { Node node = (Node)nodeStack.Pop(); AddEdges(node, nodeStack, subgraph); } }
/// <summary> Adds the argument node and all its out edges to the subgraph.</summary> /// <param name="node">the node to add /// </param> /// <param name="nodeStack">the current set of nodes being traversed /// </param> private void AddEdges(Node node, Stack nodeStack, Subgraph subgraph) { node.Visited = true; for (IEnumerator i = ((DirectedEdgeStar)node.OutEdges).Iterator(); i.MoveNext();) { DirectedEdge de = (DirectedEdge)i.Current; subgraph.Add(de.Edge); Node toNode = de.ToNode; if (!toNode.Visited) { nodeStack.Push(toNode); } } }