Exemplo n.º 1
0
        /// <summary>
        /// Perform a breath-first-search (BFS) from the start atom. The <see cref="distTo"/>[]
        /// is updated on each iteration. The <see cref="routeTo"/>[] keeps track of our route back
        /// to the source. The method has aspects similar to Dijkstra's shortest path
        /// but we are working with vertices and thus our edges are unweighted and is
        /// more similar to a simple BFS.
        /// </summary>
        private void Compute(int[][] adjacent)
        {
            // queue is filled as we process each vertex
            int[] queue = new int[adjacent.Length];
            queue[0] = start;
            int n = 1;

            for (int i = 0; i < n; i++)
            {
                int v    = queue[i];
                int dist = distTo[v] + 1;
                foreach (int w in adjacent[v])
                {
                    if (dist > limit)
                    {
                        continue;
                    }

                    // distance is less then the current closest distance
                    if (dist < distTo[w])
                    {
                        distTo[w]   = dist;
                        routeTo[w]  = new SequentialRoute(this, routeTo[v], w);
                        nPathsTo[w] = nPathsTo[v];
                        queue[n++]  = w;
                    }
                    else if (distTo[w] == dist)
                    {
                        routeTo[w]   = new Branch(routeTo[w], new SequentialRoute(this, routeTo[v], w));
                        nPathsTo[w] += nPathsTo[v];
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Perform a breath-first-search (BFS) from the start atom. The <see cref="distTo"/>[]
        /// is updated on each iteration. The <see cref="routeTo"/>[] keeps track of our route back
        /// to the source. The method has aspects similar to Dijkstra's shortest path
        /// but we are working with vertices and thus our edges are unweighted and is
        /// more similar to a simple BFS. The ordering limits the paths found to only
        /// those in which all vertices precede the <see cref="start"/> in the given ordering.
        /// This ordering limits ensure we only generate paths in one direction.
        /// </summary>
        private void Compute(int[][] adjacent, int[] ordering)
        {
            // queue is filled as we process each vertex
            int[] queue = new int[adjacent.Length];
            queue[0] = start;
            int n = 1;

            for (int i = 0; i < n; i++)
            {
                int v    = queue[i];
                int dist = distTo[v] + 1;
                foreach (int w in adjacent[v])
                {
                    // distance is less then the current closest distance
                    if (dist < distTo[w])
                    {
                        distTo[w]   = dist;
                        routeTo[w]  = new SequentialRoute(this, routeTo[v], w); // append w to the route to v
                        nPathsTo[w] = nPathsTo[v];
                        precedes[w] = precedes[v] && ordering[w] < ordering[start];
                        queue[n++]  = w;
                    }
                    else if (distTo[w] == dist)
                    {
                        // shuffle paths around depending on whether there is
                        // already a preceding path
                        if (precedes[v] && ordering[w] < ordering[start])
                        {
                            if (precedes[w])
                            {
                                routeTo[w]   = new Branch(routeTo[w], new SequentialRoute(this, routeTo[v], w));
                                nPathsTo[w] += nPathsTo[v];
                            }
                            else
                            {
                                precedes[w] = true;
                                routeTo[w]  = new SequentialRoute(this, routeTo[v], w);
                            }
                        }
                    }
                }
            }
        }