Several shortest paths for hash algorithms

Source: Internet
Author: User
In-depth analysis of Shortest Path Algorithms

Body

Section 1 problem proposal and Solution
The so-called shortest path problem can be described in two cases.
Description 1: In graph theory, it refers to finding the shortest distance between two nodes in the graph. For example

Description 2: in real life, it refers to finding the nearest distance from one place to another. For example

The two situations above are essentially the same, that is, finding the shortest path from one point to another. Okay. The problem has been raised. How can this problem be solved? There are many ways to solve this problem. However, due to the different problem conditions of each path algorithm, we can select different path algorithms based on different situations.
This article introduces three Shortest Path Algorithms: Dijkstra algorithm, Floyd algorithm, and a * search algorithm.

Section 2 dikstra algorithm (Dijkstra algorithm)
This algorithm solves the shortest path from a single source point to another vertex in a directed graph.

The implementation process of the diketla algorithm is as follows:
Step 1: Use the weighted weiarcs matrix to represent a directed graph with weight. If the two vertices in the graph are connected, weiarcs [I] [J] is used to represent the Edge Weight of the two vertices. If VI and vj are not connected, that is, the edge <VI, VJ> does not exist, set weiarcs [I] [J] to ∞.
Step 2: Set S to the end point of the obtained Shortest Path originating from a vertex v, and the initial state of S is null. during initialization, place the originating vertex in the S collection. Then, starting from V, the initial value of the shortest path length that VI of other vertices in the graph may reach is d [I].
Step 3: select a vertex VJ so that VJ is the end point of the obtained Shortest Path starting from vertex v. In this case, S = s limit {VJ }.
Step 4: Modify the shortest path length from V to any vertex VK in the Set V-S (V is the set of graph vertices. If d [J] + weiarcs [J] [k] <D [K], d [k] = d [J] + weiarcs [J] [K].
Step 5: Repeat the operation of step 3 and Step 4 A total of N-1 times, thus we can obtain the shortest path from V to the remaining vertices in the graph.

Well, the implementation process is like this. However, it is not possible to simply describe the text. To express this process more bluntly, I think using image expressions is a good choice. As shown in

From the operational process table, we can see the shortest path from V0 to other points, as shown in

The code of the dikstra algorithm described in the above process is as follows:
Int route path (mgraph g, int v0, pathmatrix & P, shortpathtable & D) {// calculate the Shortest Path P [v] From V0 vertices in directed graph G to other vertices v using the distric algorithm and the weighted length d [v]. // If P [v] [W] is true, W is the vertex of the Shortest Path obtained from V0 to v. // If final [v] is true and only applies to V, the shortest path from V0 to V has been obtained. For (V = 0; v <G. vexmun; V ++) {final [v] = false; d [v] = G. weiarcs [V0] [v]; for (W = 0; W <G. vexnum; W ++) P [v] [W] = false; // set the null path if (d [v] <infinity) {P [v] [V0] = true; P [v] [v] = true ;}} d [V0] = 0; final [V0] = true; // initialization, V0 vertex belongs to the s set // start the main loop, obtain the shortest path from V0 to a vertex v each time, and add V to the s set for (I = 1; I <G. vexnum; I ++) // other G. vexnum-1 vertex {min = infinity; // The nearest known distance from the V0 vertex for (W = 0; W <G. vexnum; I ++) {If (! Final [w]) // W vertex in V-s {If (d [w] <min) // W vertex closer to V0 {v = W; min = d [w] ;}} final [v] = true; // Add the V closest to the V0 vertex to S for (W = 0; W <G. vexnum; W ++) // update the current calculation path and distance {If (! Final [w] & (min + G. weiarcs <D [w]) {d [w] = min + G. weiarcs [v] [W]; // P [w] = P [v] + P [w]; P [w] = P [v]; P [w] [W] = true ;}} return 0 ;}

OK, Dijkstra algorithm is finished.

Section 3 freyd algorithm (Floyd algorithm)
This algorithm solves the shortest path between two vertices in a directed weighted graph.

The design process of the Freudian algorithm is as follows:
The weighted matrix weiarcs is used to represent a directed graph with weight. If the two vertices in the graph are connected to vj, weiarcs [I] [J] is used to represent the Edge Weight of the two vertices. If VI and vj are not connected, that is, the edge <VI, VJ> does not exist, set weiarcs [I] [J] to ∞.
Requirement: Find the shortest path from node VI to node VJ.
Set d (I, j, k) ,... k) The length of the shortest path of the intermediate node. For example, if the path from VI to vj passes through the node Vm and node VK, it can be represented as: vi --> VM --> VK --> VJ.

So there are: 1. if the Shortest Path passes through the node VK, d (I, j, k) = d (I, K, k-1) + d (K, J, k-1 );
2. If the shortest path does not pass through the node VK, d (I, j, k) = d (I, j, k-1 ).
Therefore, the shortest path from VI to vj can be expressed:

D (I, j, k) = min (d (I, K, k-1) + d (K, J, k-1), D (I, j, k-1 )).

As shown in the figure below:

For the solution process, see:


The code of the freuch algorithm described in the above process is as follows:
Int route path (mgraph g, int v0, pathmatrix & P, shortpathtable & D) {// use the Floyd algorithm to find the shortest path between vertices V and w in the directed graph, P [v] [W] and its weighted length, d [v] [W]. // If P [v] [W] [u] is true, U is the vertex on the Shortest Path obtained from V to W for (V = 0; v <G. vexnum; V ++) for (W = 0; W <G. vexnum; W ++) {d [v] [W] = G. ARCs [v] [W]; If (d [v] [W] <infinity) // There is a direct path from V to W {P [v] [W] [u] = true; P [v] [W] [W] = true ;}} for (u = 0; U <G. vexnum; U ++) for (V = 0; v <G. vexnum; V ++) for (W = 0; W <G. vexnum; W ++) {If (d [v] [u] + d [u] [W] <D [v] [W]) // a shorter path from V to W. D [v] [W] = d [v] [u] <D [u] [W]; for (I = 0; I <G. vexnum; I ++) P [v] [W] [I] = P [v] [u] [I] | P [u] [W] [I];} return 0 ;}

Section 4 A * search algorithms
A * search algorithm, commonly known as the astar algorithm. This is an algorithm that has multiple nodes on the graph plane to find the lowest cost. It is often used in the mobile computing of the NPC in the game or the mobile computing of the BOT in the online game. Like Dijkstra, this algorithm can find a shortest path and perform heuristic search like BFs.

A * the core of an algorithm lies in the design of a valuation function: F (n) = g (n) + H (n ). Where, g (n) indicates the actual distance from the starting point to any point N, H (n) indicates the estimated distance from any vertex N to the target vertex, F (N) is the valuation of every possible test point. This valuation function follows the following features:
• If H (n) is 0, you only need to find G (N), that is, find the shortest path from the starting point to any vertex N, then convert it into a single-source shortest path problem, that is, Dijkstra algorithm;
• If H (n) <= "n to the actual distance from the target", the optimal solution can be obtained. In addition, the smaller the H (n), the more nodes to be calculated, and the lower the algorithm efficiency.

We can describe it as follows: the shortest distance from startpoint (SP) to endpoint (EP) is certain, so we can write a valuation function to estimate the shortest distance from the start point to the end point. If the program tries to move from the starting point along a line to another point in the path (otherpoint, abbreviated as OP ), the estimated distance from SP to EP obtained in this scheme is: the actual distance from SP to OP plus the estimated distance from op to EP estimated by the estimation function. In this way, no matter which step our program searches, we will get an estimate. After each decision, we will sort the evaluation value together with the scheme awaiting processing, then, pick out the solutions that are most likely to be part of the shortest path to be processed and start to the next step,
Keep repeating until the object is moved to the destination, or all the solutions have tried, but no path to the destination is found, it ends.

A * search algorithm diagram process see: http://blog.vckbase.com/panic/archive/2005/03/20/3778.html

Section 5
References: Data Structure (Yan Weimin) and a * search algorithm in Wikipedia

Conclusion
Think about writing and drawing ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.