コード例 #1
0
        private void BFS(IGraph graph, Int32 sourceVertice)
        {
            AST.Queue <Int32> queue = new AST.Queue <Int32>();

            // We start at source, so we're the shortest path to source.
            // Shortest path found.
            this._shortestPathToVerticeMap[sourceVertice] = true;

            queue.Enqueue(sourceVertice);

            while (!queue.IsEmpty)
            {
                Int32 vertice = queue.Dequeue(); // Remove the next vertice from the queue.

                foreach (Int32 adjacentVertice in graph.GetAdjacentVertices(vertice))
                {
                    // For every unmarked adjacent vertice:
                    //   - Save the last vertice that connected to it.
                    //   - Mark the shortest path for the adjacent vertice as found.
                    //   - Add it to the queue to be visied later.
                    // When no more adjacent vertices are found, start dequeuing vertices
                    // so we can visit its adjacent vertices.

                    if (!this._shortestPathToVerticeMap[adjacentVertice])
                    {
                        this._edgeTo[adjacentVertice] = vertice;
                        this._shortestPathToVerticeMap[adjacentVertice] = true;

                        queue.Enqueue(adjacentVertice);
                    }
                }
            }
        }
コード例 #2
0
        public IEnumerable <String> GetKeysWithPrefix(String prefix)
        {
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }

            AST.Queue <String> queue = new AST.Queue <String>();

            Node node = this.Get(this._root, prefix, 0);

            if (node == null)
            {
                return(queue);
            }

            if (node.Value != null)
            {
                queue.Enqueue(prefix);
            }

            this.GetKeysWithPrefix(node.Middle, new StringBuilder(prefix), queue);

            return(queue);
        }
コード例 #3
0
        public BellmanFordShortestPathAlgorithm(EdgeWeightedDigraph graph, Int32 vertice)
        {
            this._distTo  = new double[graph.VerticesCount];
            this._edgeTo  = new Edge[graph.VerticesCount];
            this._onQueue = new Boolean[graph.VerticesCount];

            for (int v = 0; v < graph.VerticesCount; v++)
            {
                this._distTo[v] = Double.PositiveInfinity;
            }

            this._distTo[vertice] = 0.0;

            this._queue = new AST.Queue <Int32>();
            this._queue.Enqueue(vertice);
            this._onQueue[vertice] = true;

            while (!this._queue.IsEmpty && !this.HasNegativeCycle)
            {
                Int32 v = this._queue.Dequeue();

                this._onQueue[v] = false;

                this.Relax(graph, v);
            }
        }
コード例 #4
0
        public IEnumerable <String> GetKeysThatMatch(String pattern)
        {
            AST.Queue <String> results = new AST.Queue <String>();

            this.GetKeysThatMatch(this._root, new StringBuilder(), pattern, results);

            return(results);
        }
コード例 #5
0
        public IEnumerable <String> GetKeysThatMatch(String pattern)
        {
            AST.Queue <String> queue = new AST.Queue <String>();

            this.GetKeysThatMatch(this._root, new StringBuilder(), 0, pattern, queue);

            return(queue);
        }
コード例 #6
0
        public IEnumerable <String> GetKeys()
        {
            AST.Queue <String> queue = new AST.Queue <String>();

            this.GetKeysWithPrefix(this._root, new StringBuilder(), queue);

            return(queue);
        }
コード例 #7
0
        public IEnumerable <String> GetKeysWithPrefix(String prefix)
        {
            AST.Queue <String> results = new AST.Queue <String>();

            Node node = this.Get(this._root, prefix, 0);

            this.GetKeysWithPrefix(node, new StringBuilder(prefix), results);

            return(results);
        }
コード例 #8
0
        private LazyPrimMSTAlgorithm(EdgeWeightedGraph graph)
        {
            this._visited = new Boolean[graph.VerticesCount];
            this._crossingEdgesByWeight    = new MinPQ <Edge>(1);
            this._minimunSpanningTreeEdges = new AST.Queue <Edge>();

            // Run Prim from all vertices to get a minimum spanning forest.
            for (int i = 0; i < graph.VerticesCount; i++)
            {
                if (!this._visited[i])
                {
                    this.Prim(graph, i);
                }
            }
        }
コード例 #9
0
        public DepthFirstOrder(EdgeWeightedDigraph weightedGraph)
        {
            this._pre       = new Int32[weightedGraph.VerticesCount];
            this._post      = new Int32[weightedGraph.VerticesCount];
            this._postOrder = new AST.Queue <Int32>();
            this._preOrder  = new AST.Queue <Int32>();
            this._visited   = new Boolean[weightedGraph.VerticesCount];

            for (int i = 0; i < weightedGraph.VerticesCount; i++)
            {
                if (!this._visited[i])
                {
                    this.DFS(weightedGraph, i);
                }
            }
        }
コード例 #10
0
        private Int32 _postCounter;                    // counter for postorder numbering.

        public DepthFirstOrder(Digraph digraph)
        {
            this._pre       = new Int32[digraph.VerticesCount];
            this._post      = new Int32[digraph.VerticesCount];
            this._postOrder = new AST.Queue <Int32>();
            this._preOrder  = new AST.Queue <Int32>();
            this._visited   = new Boolean[digraph.VerticesCount];

            for (int i = 0; i < digraph.VerticesCount; i++)
            {
                if (!this._visited[i])
                {
                    this.DFS(digraph, i);
                }
            }
        }
コード例 #11
0
        private void GetKeysWithPrefix(Node node, StringBuilder prefix, AST.Queue <String> queue)
        {
            if (node == null)
            {
                return;
            }

            this.GetKeysWithPrefix(node.Left, prefix, queue);

            if (node.Value != null)
            {
                queue.Enqueue(prefix.ToString() + node.Char);
            }

            this.GetKeysWithPrefix(node.Middle, prefix.Append(node.Char), queue);

            prefix.Remove(prefix.Length - 1, 1);

            this.GetKeysWithPrefix(node.Right, prefix, queue);
        }
コード例 #12
0
        private void GetKeysWithPrefix(Node node, StringBuilder prefix, AST.Queue <String> results)
        {
            if (node == null)
            {
                return;
            }

            if (node.Value != null)
            {
                results.Enqueue(prefix.ToString());
            }

            for (char c = this.Alphabet.MinChar; c <= this.Alphabet.MaxChar; c++)
            {
                prefix.Append(c);

                this.GetKeysWithPrefix(node.Next[c], prefix, results);

                prefix.Remove(prefix.Length - 1, 1);
            }
        }
コード例 #13
0
        private void GetKeysThatMatch(Node node, StringBuilder prefix, String pattern, AST.Queue <String> results)
        {
            if (node == null)
            {
                return;
            }

            Int32 index = prefix.Length;

            if (index == pattern.Length && node.Value != null)
            {
                results.Enqueue(prefix.ToString());
            }

            if (index == pattern.Length)
            {
                return;
            }

            Char c = pattern[index];

            if (c == '.')
            {
                for (char ch = this.Alphabet.MinChar; ch <= this.Alphabet.MaxChar; ch++)
                {
                    prefix.Append(ch);

                    this.GetKeysThatMatch(node.Next[ch], prefix, pattern, results);

                    prefix.Remove(prefix.Length - 1, 1);
                }
            }
            else
            {
                prefix.Append(c);

                this.GetKeysThatMatch(node.Next[c], prefix, pattern, results);

                prefix.Remove(prefix.Length - 1, 1);
            }
        }
コード例 #14
0
        private void GetKeysThatMatch(Node node, StringBuilder prefix, Int32 index, String pattern, AST.Queue <String> queue)
        {
            if (node == null)
            {
                return;
            }

            Char c = pattern[index];

            if (c == '.' || c < node.Char)
            {
                this.GetKeysThatMatch(node.Left, prefix, index, pattern, queue);
            }

            if (c == '.' || c == node.Char)
            {
                if (index == pattern.Length - 1 && node.Value != null)
                {
                    queue.Enqueue(prefix.ToString() + node.Char);
                }

                if (index < pattern.Length - 1)
                {
                    this.GetKeysThatMatch(node.Middle, prefix.Append(node.Char), index + 1, pattern, queue);

                    prefix.Remove(prefix.Length - 1, 1);
                }
            }

            if (c == '.' || c > node.Char)
            {
                this.GetKeysThatMatch(node.Right, prefix, index, pattern, queue);
            }
        }