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));
            }
        }
Exemple #3
0
        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);
        }
        private void DFS(EdgeWeightedDigraph graph, Int32 vertice)
        {
            this._onStack[vertice] = true;
            this._visited[vertice] = true;

            foreach (Edge edge in graph.GetAdjacentVertices(vertice))
            {
                Int32 targetVertice = edge.Target;

                // Short circuit if directed cycle found.
                if (this._cycle != null)
                {
                    return;
                }

                // Found new vertex, so recur.
                if (!this._visited[targetVertice])
                {
                    this._edgeTo[targetVertice] = edge;

                    this.DFS(graph, targetVertice);
                }

                // trace back directed cycle
                else if (this._onStack[targetVertice])
                {
                    this._cycle = new AST.Stack <Edge>();

                    Edge tempEdge = edge;

                    while (tempEdge.Source != targetVertice)
                    {
                        this._cycle.Push(tempEdge);

                        tempEdge = this._edgeTo[tempEdge.Source];
                    }

                    this._cycle.Push(tempEdge);

                    return;
                }
            }

            this._onStack[vertice] = false;
        }
        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);
        }
        private void DFS(Digraph digraph, Int32 vertice)
        {
            this._onStack[vertice] = true;
            this._visited[vertice] = true;

            foreach (Int32 adjacent in digraph.GetAdjacentVertices(vertice))
            {
                if (this.HasCycle)
                {
                    return;
                }

                // The cycle occurrs when an adjacent vertice is on the stack of
                // "vertices being processed".
                // If vertice A is being processed (onStack) and one of its adjacents
                // points to A again, we have a cycle.

                if (!this._visited[adjacent])
                {
                    this._edgeTo[adjacent] = vertice;

                    this.DFS(digraph, adjacent);
                }
                else if (this._onStack[adjacent])
                {
                    this._cycle = new AST.Stack <Int32>();

                    for (int i = vertice; i != adjacent; i = this._edgeTo[i])
                    {
                        this._cycle.Push(i);
                    }

                    this._cycle.Push(adjacent);
                    this._cycle.Push(vertice);
                }
            }

            // If we fully processed a vertice, it means the vertice is no more on
            // the stack.

            this._onStack[vertice] = false;
        }
Exemple #7
0
        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());
        }