The specific explanation of fragmented algorithm several shortest paths

Source: Internet
Author: User

deep Analytic Shortest path algorithm

body

The problem of the first section and the solution method
The so-called shortest path problem, can say there are two situations to describe the narrative.
Descriptive narrative one: in graph theory, refers to finding the shortest distance between two nodes in a graph. For example with

descriptive narrative two: in real life, it means finding the nearest distance from one place to another. For example with

The essence of the above two situations is the same, that is, to find a point to the shortest path of a point. Well, the problem has been raised, how to solve it? The solution to this problem is still much more, just because each path algorithm corresponding to the problem conditions, we can according to different circumstances, choose a different path algorithm.
This article introduces three shortest path algorithms, each of which are: the Dykstra algorithm (Dijkstra algorithm), the Freud algorithm (Floyd algorithm), and a * search algorithm.

section II Dykstra algorithm (Dijkstra algorithm)
This algorithm solves the problem of the shortest path of a single source point to another vertex in a graph.

the implementation steps for the Dykstra algorithm are as follows:
The first step: using the weighted matrix Weiarcs to represent the weighted graph, assuming that the two vertices VI and VJ are connected, then use WEIARCS[I][J] to represent the weights of the edges formed by the two vertices; assuming that VI and VJ are not connected, that the <vi,vj> side does not exist, Then set weiarcs[i][j] to ∞.
Step Two: Set S to the set of the end point of the shortest path originating from a vertex V, and the initial state of S is empty, and the originating vertex is placed in the S collection when initialized. Then the initial value of the shortest path length from V to the remaining vertex VI in the figure is d[i].
The third step: Select a vertex VJ, so that VJ is currently the end of a shortest path from vertex v. This makes S = S∪{VJ}.
Fourth step: Change from V to set V-s (V for the collection of graph vertices) VK can reach the shortest path length. Assuming d[j]+weiarcs[j][k] < D[k], then d[k] = D[j] + weiarcs[j][k].
Fifth step: Repeated operation of the third and fourth steps of a total of N-1 times, thus can be obtained from the V to the rest of the graph of the shortest path of each vertex.

Well, that's the way it's done. Just the text description of the light is not possible, to more straightforward expression of the process, I think the use of image representation is a very good choice. For example, as seen in the



from the calculation procedure table, we know the shortest path of V0 to the remaining points, for example

The above process describes the code of the Dykstra algorithm described below for example:
int ShortPath (mgraph g,int v0,pathmatrix &p,shortpathtable &d) {//Dykstra algorithm is used to find the shortest path to the v0 vertex to the remaining vertex v in Figure G P[v] and the weighted length d[v]. If P[V][W] is true, then W is the vertex on the shortest path currently obtained from V0 to V. FINAL[V] is true when and only if v∈s, that is, 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 Empty Path if (D[v] < INFINITY) {P[v][v0] = true;p[v][v] = TRUE;}} D[v0] = 0;final[v0] = true;//initialization, the v0 vertex belongs to the S collection//start main loop, each time the shortest path v0 to a vertex v is obtained, and V is added to the S collection for (i = 1; i < g.vexnum; i++)// The remaining g.vexnum-1 vertices {min = infinity;//the recent distance from V0 point 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;//to v0 vertex recent v increased to s in for (w = 0;w < g.vexnum;w++)//update current Max 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 introduced.

section III Freud algorithm (Floyd algorithm)
the algorithm solves the problem of the shortest path between two vertices in a weighted graph.

The design steps of the Freud algorithm are as follows:
Using the weighted matrix Weiarcs to represent the weighted undirected graph, assuming that the two vertices VI and VJ are connected, using WEIARCS[I][J] to represent the weights of the edges formed by the two vertices; assuming that VI and VJ are not connected, that the <vi,vj> side does not exist, Then set weiarcs[i][j] to ∞.
Requirements: Find the shortest path of node VI to the node VJ.
Set D (i,j,k) to the length of the shortest path from node VI to node VJ with VK (Vk∈ (0,1,... k)) nodes as intermediate nodes. For example: From VI to VJ this path through node VMs and node VK, then can be expressed as: VI-->VM-->VK-->VJ.

So, there are: 1. If the shortest path passes through the node VK, then 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, then D (i,j,k) = d (i,j,k-1).
Therefore, the shortest path of the VI to VJ can be expressed as:
d (i,j,k) = min (d (i,k,k-1) + D (k,j,k-1), D (i,j,k-1)).
The old way, the steps illustrated are as follows:

the process of solving is shown in:



The above process describes the code of the Freud algorithm described in the following example:
int ShortPath (mgraph g,int v0,pathmatrix &p,shortpathtable &d) {//Use Floyd algorithm to find the shortest path between each pair of vertices V and W in the direction graph P[v][w] and its weighted length d[ V][W]. If P[v][w][u] is true, then U is the vertex for (v = 0;v < g.vexnum;v++) on the shortest path currently evaluated from V to W for (w = 0;w < g.vexnum;w++) {D[v][w] = g.arcs[v][ W];if (D[v][w] < INFINITY)//from V to W there is a direct path {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 through u 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;}

Fourth section A * search algorithm
A * Search algorithm, commonly known as A-Star algorithm. This is a path with multiple nodes on the tiling surface, and the lowest pass cost algorithm is obtained. Often used in-game NPC mobile Computing, or online game bot mobile computing. The algorithm, like the Dijkstra algorithm, can find a shortest path and, like BFS, carries out a revelation-based search.

The core part of a * algorithm is the design of one of its valued functions: F (N) =g (n) +h (n). , g (n) represents the actual distance from the starting point to a point N, and H (n) represents the estimated distance from the random vertex N to the target vertex, and f (n) is the estimate of each possible test point. This valuation function follows the following characteristics:
Suppose H (n) is 0, only needs g (n), that is, to find the shortest path from the starting point to the random vertex N, it is converted to the single source shortest path problem, i.e. the Dijkstra algorithm;
Assuming h (n) <= "N to the actual distance of the target", the optimal solution must be obtained. And the smaller the H (N), the more nodes need to be computed, the lower the algorithm efficiency.

We can describe the narrative in this way: the shortest distance from the starting point (StartPoint, abbreviated to SP) to the end point (EndPoint, abbreviated to EP) is certain, so we can write an estimate function to estimate the shortest distance from the starting point to the end point. Assuming the program is trying to move from the starting point along a route to a path with a point (Otherpoint, abbreviated to OP), then we think this method has the estimated distance from SP to EP: The actual distance from SP to OP plus the estimated distance from OP to EP that the function estimates. So, regardless of where our program search is going, we get an estimate, and after each decision, we sort the evaluation values with the scenarios awaiting processing, and then we pick out the scenarios that are most likely to be part of the shortest route in the various scenarios to be processed, and then move on to the next step, looping until the object moves to the destination. Or all of the schemes have been tried, but no path to the destination has been found to end.

the graphical process of a * search algorithm can be seen in: http://blog.vckbase.com/panic/archive/2005/03/20/3778.html

section Fifth related notes
Reference Data: Data structure (Min), Wikipedia, A * search algorithm

Sixth Quarter concluding remarks
think, write, Draw ...

The specific explanation of fragmented algorithm several shortest paths

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.