public IEnumerable <Int32> PathFromSourceTo(Int32 vertice) { if (!this.IsConnectedToSource(vertice)) { return(null); } AST.Stack <Int32> path = new AST.Stack <Int32>(); for (int i = vertice; i != this.SourceVertice; i = this._edgeTo[i]) { path.Push(i); } path.Push(this.SourceVertice); return(path); }
public IEnumerable <Int32> ReversePost() { AST.Stack <Int32> reverse = new AST.Stack <Int32>(); foreach (Int32 vertice in this._postOrder) { reverse.Push(vertice); } return(reverse); }
public NFARegularExpressionSearch(String expression) { this._expression = expression; this._expressionSize = expression.Length; this._graph = new Digraph(this._expressionSize + 1); AST.Stack <Int32> operations = new AST.Stack <Int32>(); for (int i = 0; i < _expressionSize; i++) { int lp = i; if (expression[i] == '(' || expression[i] == '|') { operations.Push(i); } else if (expression[i] == ')') { Int32 or = operations.Pop(); // 2-way OR operator. if (expression[or] == '|') { lp = operations.Pop(); this._graph.AddEdge(lp, or + 1); this._graph.AddEdge(or, i); } else if (expression[or] == '(') { lp = or; } } // Closure operator (uses 1-character lookahead). if (i < this._expressionSize - 1 && expression[i + 1] == '*') { this._graph.AddEdge(lp, i + 1); this._graph.AddEdge(i + 1, lp); } if (expression[i] == '(' || expression[i] == '*' || expression[i] == ')') { this._graph.AddEdge(i, i + 1); } } if (operations.Count != 0) { throw new ArgumentException("Invalid regular expression.", nameof(expression)); } }
public IEnumerable <Edge> PathTo(Int32 vertice) { if (!this.HasPathTo(vertice)) { return(null); } AST.Stack <Edge> path = new AST.Stack <Edge>(); for (Edge e = this._edgeTo[vertice]; e != null; e = this._edgeTo[e.Source]) { path.Push(e); } return(path); }
public IEnumerable <Edge> PathTo(Int32 vertice) { if (this.HasNegativeCycle) { throw new InvalidOperationException("Negative cost cycle exists."); } if (!this.HasPathTo(vertice)) { return(null); } AST.Stack <Edge> path = new AST.Stack <Edge>(); for (Edge e = this._edgeTo[vertice]; e != null; e = this._edgeTo[e.Source]) { path.Push(e); } return(path); }
public static Double Evaluate(String expression) { AST.Stack <String> operations = new AST.Stack <String>(); AST.Stack <Double> values = new AST.Stack <Double>(); String[] tokens = expression.Split(" "); foreach (String token in tokens) { switch (token) { case "(": break; case "+": case "-": case "*": case "/": case "sqrt": operations.Push(token); break; case ")": // Pop, evaluate, and push result if token is ")". String operation = operations.Pop(); Double value = values.Pop(); switch (operation) { case "+": value = values.Pop() + value; break; case "-": value = values.Pop() - value; break; case "*": value = values.Pop() * value; break; case "/": value = values.Pop() / value; break; case "sqrt": value = Math.Sqrt(value); break; } values.Push(value); break; default: // If token is not operator or parentheses: push double value. values.Push(Double.Parse(token)); break; } } return(values.Pop()); }