public void DepthFirstSearch(TKey key, ProcessVertex preProcess = null, ProcessVertex postProcess = null) { var v = GetVertex(key); DataStructures.Stack <Vertex> vStack = new DataStructures.Stack <Vertex>(); v.Viewed = true; vStack.Push(v); preProcess?.Invoke(v); while (!vStack.IsEmpty) { bool neighboursFound = false; foreach (var vert in Nearest(vStack.Peek().Key).Where(vrt => !vrt.Viewed)) { neighboursFound = true; preProcess?.Invoke(vert); vert.Viewed = true; vStack.Push(vert); break; } if (!neighboursFound) { var vert = vStack.Pop(); postProcess?.Invoke(vert); vert.Viewed = true; } } }
/// <summary> /// Traverse graph in depth first order /// Returns traverse path as List /// Time Complexity: O(V+E) /// where V is number of vertices in the graph and E is number of edges in the graph /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="graph">Graph to be traversed</param> /// <param name="first">start point in graph</param> /// <returns>Traverse path</returns> public static List <T> DepthFirstSearch <T>(IGraph <T> graph, T first) { List <T> result = new List <T>(); DataStructures.Stack <T> stack = new DataStructures.Stack <T>(); HashSet <T> visited = new HashSet <T>(); stack.Push(first); while (!stack.IsEmpty()) { T current = stack.Pop(); visited.Add(current); result.Add(current); foreach (var neighbour in graph.GetNeighbours(current)) { if (!visited.Contains(current)) { stack.Push(neighbour); } } } return(result); }
static public bool AreParanthesesCorrect(string text) { DataStructures.Stack <char> stack = new DataStructures.Stack <char>(); foreach (char character in text) { if (!IsParantheses(character)) { continue; } if (IsOpening(character)) { stack.Push(character); } else { if (stack.IsEmpty) { return(false); } char c = stack.Pop(); if (!ArePair(c, character)) { return(false); } } } return(stack.IsEmpty); }
//Iterative depth-first search that returns the node with the target data. public static GraphNode <T> DFS <T>(this SimpleGraph <T> graph, T target) { if (graph == null || graph.Nodes.Count == 0) { return(null); } var visited = new HashSet <GraphNode <T> >(); var stack = new DataStructures.Stack <GraphNode <T> >(); stack.Push(graph.Root); while (!stack.IsEmpty) { GraphNode <T> curr = stack.Pop(); if (visited.Contains(curr)) { continue; } if (curr.Data.Equals(target)) { return(curr); } visited.Add(curr); foreach (GraphNode <T> neighbor in curr.Neighbors) { stack.Push(neighbor); } } return(null); }
private readonly int M; // number of states public Regex(string regexp) { DataStructures.Stack <int> ops = new DataStructures.Stack <int>(); re = regexp.ToCharArray(); M = re.Length; G = new Digraph(M + 1); for (int i = 0; i < M; i++) { int lp = i; if (re[i] == '(' || re[i] == '|') { ops.Push(i); } else if (re[i] == ')') { int or = ops.Pop(); if (re[or] == '|') { lp = ops.Pop(); G.AddEdge(lp, or + 1); G.AddEdge(or, i); } else { lp = or; } } if (i < M - 1 && re[i + 1] == '*') // lookahead { G.AddEdge(lp, i + 1); G.AddEdge(i + 1, lp); } if (re[i] == '(' || re[i] == '*' || re[i] == ')') { G.AddEdge(i, i + 1); } } }
//Iterative depth-first search that returns the path to the target public static List <GraphNode <T> > DFSPathTo <T>(this SimpleGraph <T> graph, T target) { if (graph == null || graph.Nodes.Count == 0) { return(null); } var visited = new HashSet <GraphNode <T> >(); var stack = new DataStructures.Stack <GraphNode <T> >(); stack.Push(graph.Root); GraphNode <T> curr = null; var path = new List <GraphNode <T> >(); while (!stack.IsEmpty) { curr = stack.Pop(); if (visited.Contains(curr)) { continue; } if (curr.Data.Equals(target)) { break; } visited.Add(curr); foreach (GraphNode <T> neighbor in curr.Neighbors) { stack.Push(neighbor); } } if (!curr.Data.Equals(target)) { return(null); } while (curr.Origin != null && !curr.Equals(graph.Root)) { path.Add(curr); curr = curr.Origin; } path.Add(curr); path.Reverse(); return(path); }
private void UndoLastAction() { if (undo.Count > 0) { ICommand action = undo.Pop(); Log.Info("Undo: executing {0}", action.Verb); action.Execute; } else { Log.Info("Undo: there is nothing to undo"); } }
/// <summary> /// Depth-First traversal (traverse nodes farther from the start node before the nodes that are closer). Complexity is linear. /// </summary> /// <param name="startingNode">Node to start traversal from</param> /// <param name="processNodeAction">Action delegate executed for each node</param> public static void DepthFirstTraversal(Node <TNodeValue, TLinkProperty> startingNode, Action <Node <TNodeValue, TLinkProperty> > processNodeAction) { if (startingNode == null) { throw new ArgumentNullException("startingNode"); } if (startingNode.Graph == null) { throw new Exception("Node is not associated with a graph"); } var mountedNodes = new Dictionary <Node <TNodeValue, TLinkProperty>, bool>(startingNode.Graph.NodesCount); var stack = new DataStructures.Stack <Node <TNodeValue, TLinkProperty> >(); stack.Push(startingNode); mountedNodes.Add(startingNode, true); while (stack.Count != 0) { var n = stack.Pop(); if (processNodeAction != null) { processNodeAction(n); } foreach (var l in n.Links) { if (n == l.Node1 && !mountedNodes.ContainsKey(l.Node2)) { stack.Push(l.Node2); mountedNodes.Add(l.Node2, true); } if (n == l.Node2 && !mountedNodes.ContainsKey(l.Node1)) { stack.Push(l.Node1); mountedNodes.Add(l.Node1, true); } } } }
public int FindStronglyConnectedComponents() { int componentsCount = 0; // 1. Reverse edges before running Depth First Search first time ReverseEdges(); EnforceOrder = true; // 2. Run depth first search to create vertices list // in reversed finishing time order var ftStack = new DataStructures.Stack <Vertex>(); for (int i = Vertices.Count - 1; i >= 0; i--) { if (!Vertices[i].Viewed) { DepthFirstSearch(Vertices[i].Key, null, (Vertex v) => ftStack.Push(v)); } } // Restore initial edges and viewed state before DFS second run ReverseEdges(); foreach (var vertex in Vertices) { vertex.Viewed = false; } // 3. Finally run DFS in reverse finishing time order; // count strongly connected components and mark vertices // belonging to each component with component's number (secondary number) while (!ftStack.IsEmpty) { var vertex = ftStack.Pop(); if (!vertex.Viewed) { componentsCount++; DepthFirstSearch(vertex.Key, (Vertex v) => v.SecondaryOrder = componentsCount, null); } } return(componentsCount); }