예제 #1
0
        //this method performs the BFS search
        public bool Search()
        {
            //create a std queue of edges
            Queue <GraphEdge> queue = new Queue <GraphEdge>();

            //create a dummy edge and put on the queue
            GraphEdge dummy = new GraphEdge(source, source);

            queue.Enqueue(dummy);

            //mark the source node as visited
            visited[source] = Visited;

            //while there are edges in the queue keep searching
            while (queue.Count != 0)
            {
                //remove the edge from the queue
                GraphEdge next = queue.Dequeue();

                //mark the parent of this node
                route[next.To] = next.From;

                //put it on the tree. (making sure the dummy edge is not placed on the tree)
                if (next != dummy)
                {
                    spanningTree.Add(next);
                }

                //exit if the target has been found
                if (next.To == target)
                {
                    return(true);
                }

                //push the edges leading from the node at the end of this edge
                //onto the queue
                foreach (GraphEdge edge in graph.edges[next.To])
                {
                    //if the node hasn't already been visited we can push the
                    //edge onto the queue
                    if (visited[edge.To] == Unvisited)
                    {
                        queue.Enqueue(edge);

                        //and mark it visited
                        visited[edge.To] = Visited;
                    }
                }
            }

            //no path to target
            return(false);
        }
예제 #2
0
        //this method performs the DFS search
        public bool Search()
        {
            //create a std stack of edges
            Stack <GraphEdge> stack = new Stack <GraphEdge>();

            //create a dummy edge and put on the stack
            GraphEdge dummy = new GraphEdge(source, source);

            stack.Push(dummy);

            //while there are edges in the stack keep searching
            while (stack.Count != 0)
            {
                //remove the edge from the stack
                GraphEdge next = stack.Pop();

                //make a note of the parent of the node this edge points to
                route[next.To] = next.From;

                //put it on the tree. (making sure the dummy edge is not placed on the tree)
                if (next != dummy)
                {
                    spanningTree.Add(next);
                }

                //and mark it visited
                visited[next.To] = Visited;

                //if the target has been found the method can return success
                if (next.To == target)
                {
                    return(true);
                }

                //push the edges leading from the node this edge points to onto
                //the stack (provided the edge does not point to a previously
                //visited node)
                foreach (GraphEdge edge in graph.edges[next.To])
                {
                    if (visited[edge.To] == Unvisited)
                    {
                        stack.Push(edge);
                    }
                }
            }

            //no path to target
            return(false);
        }
예제 #3
0
        //
        //  use to add he eight neighboring edges of a graph node that
        //  is positioned in a grid layout
        public static void AddAllNeighboursToGridNode(NavGraph graph, int row, int col, int numCellsX, int numCellsY)
        {
            for (int i = -1; i <= +1; ++i)
            {
                for (int j = -1; j <= +1; ++j)
                {
                    //skip if equal to this node
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    int nodeX = col + j;
                    int nodeY = row + i;

                    //check to see if this is a valid neighbour
                    if (ValidNeighbour(nodeX, nodeY, numCellsX, numCellsY))
                    {
                        //calculate the distance to this node
                        Vector2 posNode      = graph.GetNode(row * numCellsX + col).Position;
                        Vector2 posNeighbour = graph.GetNode(nodeY * numCellsX + nodeX).Position;

                        float dist = Vector2.Length(posNeighbour - posNode);

                        GraphEdge newEdge;

                        newEdge = new GraphEdge(row * numCellsX + col, nodeY * numCellsX + nodeX, dist);
                        graph.AddEdge(newEdge);

                        //if graph is not a diagraph then an edge needs to be added going
                        //in the other direction
                        if (!graph.IsDigraph())
                        {
                            newEdge = new GraphEdge(nodeY * numCellsX + nodeX, row * numCellsX + col, dist);
                            graph.AddEdge(newEdge);
                        }
                    }
                }
            }
        }