Data structure 20-Shortest path of graph

Source: Internet
Author: User
Tags min
Shortest Path

For a network diagram, the shortest path is the least-valued path of the edge weights passing between the two vertices, and we call the first vertex on the path the source point, and the last vertex is the end point. Dijkstra Algorithm


It is not all of a sudden V0 to V8 the shortest path, and step by step to find the shortest path between them, the process is based on the shortest path has been found on the basis of finding the shortest path of the farther vertex, and finally get the results you want.

Let's look at the idea.

1. Vertex v0 to v1 the shortest distance, the answer is 1, the path is direct V0 connection to V1

2. Because vertex v1 is also connected with V2, v3, v4,

We're both begging.

V0->v1->v2=1+3=4,

V0->v1->v3=1+7=8

V0->v1->v4=1+5=6

Now, the shortest distance to ask V0 to V2 is. The answer is 4.


Since vertex v2 is also connected with V4 and V5, we also seek v0->v2->v4 = 4+1=5 at the same time

V0->v2->v5=4+7=11. Here V0->v2 we are using the smaller 4 that we just calculated. At this point we also found that v0->v1->v2->v4=5 is smaller than v0->v1->v4=6. So the minimum distance v0 to V4 is now 5. As shown in figure

When we asked V0 to the shortest distance to V3, there were three sides to V3, except V6 did not study,

V0->v1->v3= the result is 8.

V0->v4->v3=5+2=7. So the shortest distance from V0 to V3 is 7.

Let's take a look at the code emulation run

#define Maxvex 9

#define INFINITY 65535

typedef int PATHARC[MAXVEX]; An array to store the shortest path subscript

typedef int SHORTPATHTABLE[MAXVEX]; Weights for storing to the shortest path of each point

/*dijkstra algorithm, the V0 vertex of the graph G to the remaining vertex v shortest path p[v] and the weighted length d[v]*/

The value of/*p[v] is the precursor vertex subscript, d[v] represents the shortest path length v0 to V and */

void Shortespath_dijkstra (mgraph g,int v0,patharc *p,shortpathtable *d)

{

int v,w,k,min;

int Final[maxvex]; Final[w]=1 represents the shortest path of vertex v0 to VW

for (v=0;v<g.numvertexes;v++)

{

final[v]=0; All vertices are initialized to unknown shortest path state

(*d) [v] = g.arc[v0][v];

(*p) [V] = 0;

}

(*d) [V0]=0;

Final[v0]=1;

/** starts the main loop, each time the shortest path to a v vertex is obtained v0

for (v=1;v<g.numvertexes;v++)

{

min=infiity;

for (w=0;w<g.numvertexes;w++)

{

if (!final[w]&& (*d) [w]<min)

{

K=w;

min = (*d) [w];

}

}

Final[k]=1; Place the nearest vertex currently found at 1

for (w=0;w<g.numvertexes;w++)

{

if (!final[w]&& (min+g.arc[k][w]< (*D) [W]))

{

(*d) [W]=min*g.arc[k][w];

(*p) [W] = k;

}

}

}

}

Analysis Process:

1. The program starts running, and the final array on line 4th is the mark for V0 to whether the shortest path has been obtained for a vertex, and if v0 to VW already has the result, then final[w]=1;

2. Line 5th to 10th is the work of initializing the data. At this point the final array value is 0, indicating that all points have not been evaluated for the shortest path. The D array is {65535,1,5,65535,65535,65535,65535,65535}. Because the Benquan values of V0 and V1 and V2 are 1 and 5. The P array is all 0, indicating that there are no paths at this time.

3. Line 11th, indicating V0 to v0 itself, weights and results of 0. The D array is {0,1,5,65535,65535,65535,65535,65535}. The 12th line indicates that the V0 point is already the shortest path, so final[0]=1. At this point the final array is {1,0,0,0,0,0,0,0,0} at this time the entire initialization operation is completed

4. Line 13th ~33, the main cycle, each cycle to find v0 and a vertex of the shortest path. Therefore v starts from 1 instead of 0.

5. The maximum value of the shilling min=65535, with the W cycle, compared to d[w] to find the minimum min=1,k=1.

6. Line 24th, represented by K=1, indicates that the closest vertex to V0 is V1, and by d[1]=1, the shortest distance from V0 to V1 at this time is 1. So set the v1 corresponding to final[1] to 1. The final array is {1,1,0,0,0,0,0,0,0} at this time

7. Line 25th to 32nd is a cycle, and this cycle is critical. The aim is to calculate the edges of the V1 and other vertices on the basis of the shortest path of the v0 to V1 that has just been found, and to get the shortest distance v0 to them, as shown in figure

Because at this time min=1, so the original d[2]=5, now v0->v1->v2=d[2]=min+3=4;

V0->v1->v3=d[3]=min+7=8, v0->v1->v4=d[4]=min+5=6, so the current value of the D array is {0,1,4,8,6,65535, 65535, 65535, 65535}. And p[2]=1,p[3]=1,p[4]=1, it means the shortest path v0 to v2,v3,v4 points, their precursor is v1. The P array value is {0,0,1,1,1,0,0,0,0} at this time

8. Start the cycle again, v=2. Line 15th to 23rd, on the W loop, attention to Final[0]=1 and final1[1]=1, by the 18th line of!final[w], v0 and V1 do not participate in the minimum value acquisition. The minimum value of min=4,k=2 is found by cyclic comparison.

9. Line 24th, by k=2, indicates that the shortest path V0 to V2 has been found, and that the shortest distance is 4 by d[2]=4. So set the V2 corresponding final[2] to 1, at which time the final array is {1,1,1,0,0,0,0,0,0}

10. Line 25th to 32nd. On the basis of finding the shortest path of V0 and V2 just now, the edges of v0 and other vertices are calculated, and the shortest distance between v0 and their current is obtained, as shown in figure. Because Min=4, so originally d[4]=6, now v0->v2->v4=d[4]=min+1 = 4; V0->v2->v5=d[4]=min+7 = 11, so the current value of the D array is: {0,1,4,8,5,11, 65535, 65535, 65535}. And the original p[4]=1 and now the p[4]=2,p[5]=2, it represents v0 to V4, V5 point of the shortest path their precursor is v2. At this point the P array value is: {0,0,1,1,2,2,0,0,0}.

11. Start the cycle again, v=3. Line 15th to 23rd, get the minimum value min=5,k=4.

12. On line 24th, the shortest path v0 to V4 has been calculated by k=4, and the shortest distance is known by d[4]=5 to be 5. Therefore, the v4 corresponding FINAL[4] is set to 1. The final array is {1,1,1,0,1,0,0,0,0} at this time

13. Line 25th to 32nd calculates the edges of the V4 and other vertices. Get V0 with their current shortest distance, because min=5, so originally d[3]=8, now v0->v4->v3=d[3]=min+2=7, originally d[5]=11, now v0->v4->v5=d[5]=min+3= 8, in addition v0->v4->v6=d[6]=min+6=11,v0->v4->v7=d[7]=min+9=14. So the current value of the D array is: {0,1,4,7,5,8,11,14,65535}. And the original p[3]=1, at this time p[3]=4, the original p[5]=2, at this time the p[5]=4, another p[6]=4,p[7]=4, it said V0 to V3, V5, V6, V7 points of the shortest path their precursor is v4. At this point the value of the P array is: {0,0,1,4,2,4,4,4,0}

14. The subsequent cycle is exactly the same. Get the final result

The final array {1,1,1,1,1,1,1,1,1}, which indicates that all vertices have completed the shortest path to find work. At this point the D array is {0,1,4,7,5,8,10,12,16}, which represents the shortest path number v0 to each vertex,

The P array at this time is {0,0,1,4,2,4,3,6,7}. For example p[8]=7 it means V0 to V8 the shortest path, vertex V8 the precursor vertex is V7, then p[7]=6, V7 is v6,p[6]=3, the precursor is v3. In this way, the shortest path from V0 to V8 is V8<-v7<-v6<-v3<-v2<-v1<-v0, which is v0->v1->v2->v4->v3->v6->v7- >v8.

In fact, the final return of the array D and P, is to get V0 to any vertex of the shortest path and path length. For example, the shortest path to V0 to V8 is not V5, but we already know the shortest path v0 to V5. D[5]=8, by p[5] can know that its precursor apex is v4. So the shortest path from V0 to V5 is v0->v1->v2->v4->v5.

The Dijkstra (Dijkstra) algorithm solves the shortest path problem from one source point to the remaining vertex. Time complexity O (n2)

Now if I want to ask for the shortest path to any other point. Then there has to be a loop to deal with the complexity and become O (N3)

Then we will ask for a shortest path algorithm as follows

Freud (Floyd), which asks all vertices to the time complexity of all vertices is also O (N3), but its algorithm is very concise and elegant. Freud (Floyd) algorithm

Let's take a look at a simple and secure connected network of 3 vertices as shown below.

We first define two-dimensional arrays d[3][3] and p[3][3],d to represent the shortest path weights for vertex to vertex

P represents the minimum path precursor matrix for the corresponding vertex.

Before parsing any vertices, we named D as D-1, which is actually the adjacency matrix of the initial graph. Named P as P-1, initialized to the matrix shown in the diagram

First we analyze the shortest path to the other vertex after all vertices have been v0. Because there are only three vertices, you need to see V1->V0->V2 and get d-1[1][0]+d-1[0][2]=2+1=3.

D-1[1][2] indicates that the weight of the v1->v2 is 5, and we find d-1[1][0]>d-1[1][0]+d-1[0][2]. The popular saying is that V1->v0->v2 is closer to the distance than the direct v1->v2. So we let d-1[1][2]=d-1[1][0]+d-1[0][2]=3, the same d-1[2][1] is also equal to 3. So there's the D0 matrix.

Because of the change, so the P-matrix corresponding p-1[1][2] and p-1[2][1] is also modified to the current transit vertex v0 subscript 0, so there is a P0 view of the conversion process

Next, D0 and P0 continue to process the shortest path of all vertices through V1 and v2 to the other vertex, and get D1 and P1, D2, and P2 to complete the shortest path calculation for all vertices to all vertices

Here's a complex diagram of the deduction.

The code is as follows

typedef int PATHMATIRX[MAXVEX][MAXVEX];

typedef int SHORTPATHTABLE[MAXVEX][MAXVEX];

The/*floyd algorithm is used to find the vertex v to the remaining vertex W shortest path p[v][w] and the weighted length of the net graph G d[v][w]*/

void Shortestpath_floyd (mgraph g,pathmatirx *p,shortpathtable *d)

{

int v,w,k;

for (W=0;V<G.NUMVERTEXES;++V)

{

for (W=0;W<G.NUMVERTEXES;++W)

{

(*d) [V] [W] = g.matirx[v][w]; initialization, which is the weighted value between the corresponding points

(*p) [V]   [W] = W; Initialize P

}

}

for (K=0;K<G.NUMVERTEXES;++K)

{

for (V=0;V<G.NUMVERTEXES;++V)

{

for (W=0;W<G.NUMVERTEXES;++W)

{

if ((*d) [v][w]> (*d) [v][k]+ (*d) [k][w])

{

(*d) [V] [W] = (*d) [v][k]+ (*d) [k][w];

(*p) [V] [W]= (*p) [v][k];

}

}

}

}

}

1. The program starts running, and the 4th to 11th line is to initialize D and p so that they become the two matrices in the figure below. From the matrix also get V0->V1 road weight value is 1,v0->v2 road weight is 5,v0->v3 Infinity line, so the path weight value is maximum value 65535.

2. The 12th to 25th Line, is the main loop of the algorithm, a total of three nested, K represents the transfer vertex subscript. V represents the starting vertex, and W represents the end vertex.

3. When k=0, that is, all the vertices are v0 transit, calculate whether there is a change in the shortest path. Unfortunately the result is that there is no change, as shown below

4. When k=1, that is, all the vertices are V1 transit. At this time, when V=0, originally d[0][2]=5, now due to d[0][1]+d[1][2]=4. Therefore, the 20th line of code, the two take its minimum value, get d[0][2]=4, the same can get d[0][3]=8, d[0][4]=6, when v=2, 3, 4 o'clock, also modify some data. Modification of weights should also be made to p[v][k]

At this point, even if our shortest path is complete, you can see that the value of the V0 line of the matrix is exactly the same as the D array obtained by the Dijkstra algorithm {0,1,4,7,5,8,10,12,16}

Path V0 to V8 P[0][8]=1 first through v1, then replace 1 0 to get p[1][8]=2 instructions after 2, then 2 instead of 1 p[2][8]=4 instructions after 4, and then 4 to replace 2 p[4][8]=3 instructions after 3 ...

Last yesterday to V0->v1->v2->v4->v3->v6->v7->v8

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.