Scratch game traditional guidance algorithm

1 Depth-first algorithm (DFS)

        This algorithm is not the shortest path algorithm, but a basic search algorithm, but it can be concluded whether the specified node can be found in the graph

General idea:

  1. Starting at the start node, mark it as visited.

  2. Checks if there are any unvisited nodes among the neighbors of the current node.

  3. If there are unvisited nodes, select one of the unvisited nodes as the next current node and mark it as visited. Then go back to step 2.

  4. If there is no unvisited node, backtrack to the previous node, that is, return to the upper level node of this node, and repeat step 2.

  5. Repeat step 4 until all nodes are visited.


Concrete implementation

recursion

        There is a global variable that stores a two-dimensional array or adjacency list, describing the entire graph

        Create a function dfs, the parameter marks the position of the current node in the graph, and the characteristics or position of the target node, and the return value can be whether it is found

        Enter the function, first set the current node as visited

        If the current node is the target, return true directly

        Read the unvisited nodes that can be reached around in a certain order, and call the dfs function recursively. If dfs returns true, the function returns true immediately. If dfs returns false, continue to look for nodes that can walk around

        If none of the surrounding nodes can go, that is, the function has been executed here, it will return false

the stack

  1. There is still a global variable that stores a two-dimensional array or adjacency list, describing the entire graph
  2. build a stack s
  3. Push the starting point onto the stack and mark the starting point as visited
  4. Loop as long as there are elements in the stack:
  5. Get the top element of the stack as the current node
  6. Pop off the top element of the stack
  7. If the current node satisfies the endpoint, jump out of the loop
  8. Read the unvisited nodes that can be reached around in a certain order, push them into the stack and mark them as visited

Breadth-first algorithm (BFS)

        Although generally learn dfs first and then learn bfs, but I think bfs is simpler than dfs

The idea is:

  1. Start with the start node, mark it as visited, and enqueue it.

  2. Take a node from the queue as the current node.

  3. Determine whether this node is the end point, if so, the number of cycles is the shortest path length, and can

  4. Checks if there are any unvisited nodes among all neighbors of the current node.

  5. If there are unvisited nodes, mark them as visited and enqueue them.

  6. Repeat steps 2-5 until the queue is empty.

Its implementation is more or less like this,


Dijkstra's Algorithm

        Now we look at an early heuristic search, which is the most widely referenced scenario in graph pathfinding because it takes into account edge weights, however, when edge weights are 0, it degenerates into breadth-first search

The idea is:

  1. Initialization: mark the start node as visited, set the distance from the start node to 0, and set the distances of all other nodes to infinity.

  2. Find the shortest path: Starting from the starting node, according to the currently known shortest distance, select a node with the smallest distance among unvisited nodes to visit.

  3. Update node distance: For the selected node, calculate the distance sum of the node and its adjacent nodes. If the distance to the adjacent node through the current node is smaller than the shortest distance recorded before, update the shortest distance.

  4. Mark visited: Mark the current node as visited, which means that the shortest path from the starting node to this node has been found.

  5. Repeat steps 2-4: Repeat steps 2 to 4 until all nodes are marked as visited or there are no nodes that are reachable. In this way, the shortest path of each node is determined.

  6. Output the shortest path: Finally, according to the calculated shortest path information, the specific path from the starting node to each node can be output

        The implementation is similar. It is not convenient to post the code now, so I can only talk about the idea. The general implementation is to set up an adjacency list instead of a two-dimensional array, because the two-dimensional array is inconvenient to describe the edge weight.


Floyd's Algorithm

The simplest algorithm Floyd's idea is:

  1. Initialization: Create a two-dimensional array D to save the shortest path distance between nodes. Initially, if there is an edge between two points, its distance is assigned to D; if there is no edge between two points, its distance is set to infinity.

  2. Iterative update: For each vertex k, traverse each pair of nodes i and j in a sequential fashion, trying to improve the shortest path between node i and node j through node k. That is, if the distance from node i to node j is greater than the distance from node i to node k plus the distance from node k to node j (ie D[i][k] + D[k][j]), update node i The distance to node j is D[i][k] + D[k][j].

  3. Repeat iterations: Continue iterative updates, each time considering a new intermediate node k, until all nodes have been considered as intermediate nodes once.

  4. Output the shortest path: After the iteration is completed, what is stored in the two-dimensional array D is the shortest path distance between any two points.

However, its time complexity is unacceptable, as high as O(n^3)


greedy search

        It takes the currently best choice at each step of selection, and hopes to reach the global optimum through a series of locally optimal choices, and will not look back.

The basic idea of ​​greedy search is as follows:

  1. Initialization: Determine the initial state of the problem.

  2. Selection Phase: Starting from the current state, a best action or decision is chosen according to some criterion. This choice is based on local information, it does not take into account previous choices or future outcomes.

  3. Update Status: Based on the selected action or decision, update the current status.

  4. Termination condition: If the current state satisfies the termination condition (reaching the end point), then end the search; otherwise, return to step 2.

        The specific implementation relies on a heuristic function to detect which point in the surroundings is closer to the end point

        The greedy algorithm is short-sighted, so the results obtained may not be correct, but it usually performs faster. Therefore, in some cases, greedy search may yield suboptimal solutions or fail to find any solutions. So the greedy algorithm is not suitable


best first search

        Best-First Search is a heuristic search algorithm that selects the next expanded node based on an evaluation function. The evaluation function is used to estimate the cost or distance from the current node to the target node, and the algorithm always selects the node with the lowest evaluation value for expansion.

The basic idea of ​​best-first search is as follows:

  1. Initialization: Determine the initial state of the problem.

  2. Maintain a priority queue of nodes to be expanded, and initially put the initial state into the queue.

  3. Repeat the following steps until a solution is found or the queue is empty:

a. Remove the node with the lowest evaluation value from the queue.

b. Check if the current node is the target node, if so, the solution has been found and the search ends.

c. Otherwise, expand all possible child nodes according to the current node, and calculate the evaluation value of each child node.

d. Insert child nodes into the queue in descending order of evaluation value.

        Best-first search uses an evaluation function to select the next node to expand, sorted by evaluation value. This sorting strategy makes the search algorithm more likely to move towards the direction close to the target, thus improving the search efficiency. However, best-first search does not guarantee to find the global optimal solution, because it only focuses on the current best node, and does not consider previous choices or future results, but the best-first algorithm is easier to get the optimal solution than greedy search solution, it sometimes considers backtracking, but still does not guarantee the optimal solution


A*

        A* (A-Star) is a heuristic search algorithm that combines best-first search and Dijkstra's algorithm. It maintains two values ​​during the search process: one is the actual cost (g value) from the start node to the current node, and the other is the estimated cost (h value) from the current node to the target node.

The basic idea of ​​A* algorithm is as follows:

  1. Initialization: Determine the initial state of the problem and put the starting node into the open list.

  2. Maintain two lists: open list: store nodes to be expanded, sorted by f value (f = g + h). Closed list: stores nodes that have been expanded.

  3. Repeat the following steps until a solution is found or the open list is empty:

a. Select the node with the smallest f value from the open list as the current node.

b. Check if the current node is the target node, if so, the solution has been found and the search ends.

c. Otherwise, move the current node from the open list to the closed list.

d. Expand all possible child nodes according to the current node, and calculate the g value and h value of each child node.

e. For each child node, if it is already in the open list or closed list, compare the new g value with the existing g value, and choose the smaller update; otherwise, add the child node to the open list.

        The A algorithm uses a heuristic estimation function to guide the search process, which combines the actual cost and the estimated cost. The g value represents the actual cost from the start node to the current node (via the already expanded path), and the h value is the estimated cost from the current node to the goal node, usually using a heuristic function (such as Manhattan distance or Euclidean distance) to calculate. The A algorithm selects the node with the smallest f value for expansion, and the node with a smaller f value has a higher priority.

        The A algorithm can guarantee to find the shortest path (best solution) under certain conditions, that is, the node with the smallest f value is the global optimal solution. However, in some cases, the A algorithm may be affected by the heuristic function and cannot find the optimal solution, or consume a lot of time and memory when the search space is very large.


B*

        Next, do some optimizations on A*

        In the A* algorithm, the h value is estimated through a heuristic function, and this estimated value has a great influence on the efficiency and quality of the search. However, there are situations where the heuristic function may not provide accurate estimates. For example, in some problems, the heuristic function may not obtain the accurate estimated cost of the target node, causing the search algorithm to be biased and unable to find the optimal solution.

        The B algorithm introduces a new concept - subgoal, and adopts a more flexible estimation method. In the B algorithm, the algorithm first generates a sub-goal, and then uses the sub-goal to correct the estimated value of the heuristic function. Specific steps are as follows:

  1. Initialization: Determine the initial state of the problem and put the starting node into the open list.

  2. Maintain two lists: open list: store nodes to be expanded, sorted by f value (f = g + h). Closed list: stores nodes that have been expanded.

  3. Repeat the following steps until a solution is found or the open list is empty: a. Select the node with the smallest f-value from the open list as the current node. b. Check if the current node is the target node, if so, the solution has been found and the search ends. c. Otherwise, move the current node from the open list to the closed list. d. Expand all possible child nodes according to the current node, and calculate the g value and h value of each child node. e. For each child node, revise the estimated value of the heuristic function according to the sub-objective and calculate a new f-value. f. Add the child node to the open list.

        The B algorithm obtains a more accurate path estimate by continuously adjusting the estimated value of the heuristic function. It uses sub-goals to guide the search process, revises the estimated value of the heuristic function according to the information of the sub-goals, and performs node expansion according to the new estimated values. This process allows the B algorithm to gradually revise the path estimate during the search and better adapt to the characteristics of the problem.


D*

        D* (D-Star) is an incremental algorithm based on change graph search and path planning. It is used for path planning on a known map of the environment and can be updated quickly when the map information changes.

The basic idea of ​​the D* algorithm is as follows:

  1. Initialization: Determine the initial state of the problem, including start and goal nodes, and initialize other related data structures.

  2. Maintain two main data structures: State Graph: It records information such as the state, cost, and connection relationship of each node. Priority Queue (Priority Queue): A queue sorted by cost, used to select the next expanded node.

  3. Repeat the following steps until a solution is found or a path cannot be found: a. Select the node with the lowest cost from the priority queue as the current node. b. Check whether the current node has changed (for example, cost update or connection relationship change), and if so, update the relevant information. c. Update the cost of the nodes adjacent to the current node, and calculate the new cost value. d. If the cost of the target node has changed, re-evaluate the path. Otherwise, expand along the best path. e. Reorder the priority queue based on the updated information.

        The D algorithm adapts to map changes by continuously updating the cost and connection relationship of nodes, and dynamically adjusts the path planning process. It uses an incremental search and update strategy, which can quickly re-plan the path after modifying the map or cost information. The D algorithm repeatedly selects and expands nodes until the target node is reached or a path cannot be found.


Reverse and bidirectional optimization

        Swap the starting point and the end point, and then execute the algorithm is the reverse. However, this does not have much optimization effect. Next, combine the forward and reverse

        The idea of ​​​​putting the forward search results into one array and the reverse into another until the two arrays overlap, indicating that they are found, which can effectively reduce the search range and achieve optimization.

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131946401