Shortest Path-Dijkstra (Dijkstra) algorithm

Source: Internet
Author: User

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 as the end point. The shortest path algorithm mainly includes Dijkstra (Dijkstra) algorithm and Freud (Floyd) algorithm. This article first describes the shortest path problem from one source point to the rest of the vertices.

This is an algorithm that produces the shortest path in order of increasing path length, and its approximate idea is this.

For example, ask for the shortest path of vertex v0 to V1 in Figure 7-7-3, which is obviously 1. Since vertex v1 is also connected to V2,V3,V4, at this point we have also obtained v0->v1->v2 = 1+3 = 4, V0->v1->v3 = 1 +7 = 8, v0->v1->v4 = 1+5 = 6.

Now we can know that the shortest distance from V0 to V2 is 4 rather than the 5,7-7-4 of the V0->v2 direct connection. As the vertex v2 is also connected with V4,V5, so at the same time we obtained v0->v2->v4 is v0->v1->v2->v4 = 4+1=5,v0->v2->v5 = 4+7 = 11, here v0-> V2 We're 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 shortest distance from V0 to V4 is now shown in 5,7-7-5.

When we ask V0 to the shortest path to V3, the three edges leading to V3, except V6 not studied, V0->v1->v3 = 8, and v0->v4->v3 = 5 +2 = 7, so the shortest path v0 to V3 is shown in 7,7-7-6.

As shown above, this algorithm is not all of a sudden v0 to the shortest path to V8, but a step by step to find the shortest distance between them, the process is based on the shortest path has been found on the basis of the shortest path to obtain more distant vertices, and finally get the desired results.


The program code is as follows: (adapted from "Big Talk data Structure")

C + + Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
Usingnamespace Std;

#define MAXEDGE20
#define Maxvex20
#define INFINITY65535

typedefstruct
{
int Vexs[maxvex];
int Arc[maxvex][maxvex];
int numvertexes, numedges;
} mgraph;

typedefint Patharc[maxvex];
typedefint Shortpathtable[maxvex];

/* Build Diagram */
void Createmgraph (Mgraph *g)
{
int I, J;

/* printf ("Please enter the number of sides and vertices:"); */
G->numedges =16;
G->numvertexes =9;

for (i =0; I < g->numvertexes; i++)/* Initialization diagram */
{
G->vexs[i] = i;
}

for (i =0; I < g->numvertexes; i++)/* Initialization diagram */
{
for (j =0; J < g->numvertexes; J + +)
{
if (i = = j)
G-&GT;ARC[I][J] =0;
Else
G-&GT;ARC[I][J] = g->arc[j][i] = INFINITY;
}
}

g->arc[0][1] =1;
g->arc[0][2] =5;
g->arc[1][2] =3;
g->arc[1][3] =7;
g->arc[1][4] =5;

g->arc[2][4] =1;
g->arc[2][5] =7;
g->arc[3][4] =2;
g->arc[3][6] =3;
g->arc[4][5] =3;

g->arc[4][6] =6;
g->arc[4][7] =9;
g->arc[4]:7] =5;
g->arc[6][7] =2;
g->arc[6][8] =7;

g->arc[7][8] =4;


for (i =0; I < g->numvertexes; i++)
{
for (j = i; J < g->numvertexes; J + +)
{
G->arc[j][i] = g->arc[i][j];
}
}

}
/* Dijkstra algorithm, the shortest path of the POS vertex to the remaining vertex v to the net G P[v] and the weighted length d[v] */
/* P[v] The value is the precursor vertex subscript, d[v] represents the shortest path length for Pos to V and/*
/* POS Value 0~MG.NUMVERTEXS-1 */
void Shortestpath_dijkstra (Mgraph MG,int POS, Patharc P, shortpathtable D)
{
Int V, W, K, Min;
int Final[maxvex];/* Final[w]=1 indicates the shortest path to vertex pos to W */
for (v =0; v < mg.numvertexes; v++)
{
FINAL[V] =0;/* All vertices are initialized to unknown shortest path state */
D[V] = Mg.arc[pos][v];/* Add weights to vertices with pos points */
P[V] =0;/* Initialize path array p = 0 */
}

D[pos] =0;/* Indicates that the source point POS does not have a path to itself */
P[pos] =-1;/*-1 indicates no predecessor vertex */
Final[pos] =1;/* POS to POS do not require path */
/* Start the main loop and get the shortest path of POS to a v vertex at a time */
for (v =1; v < mg.numvertexes; v++)
{
min = INFINITY;/* Closest distance to the POS vertex currently known */
for (w =0; W < mg.numvertexes; w++)/* Find the nearest vertex from POS */
{
if (!final[w] && d[w] < min)
{
K = W;
min = d[w];/* W vertex closer to POS vertex */
}
}
FINAL[K] =1;/* Place the nearest vertex currently found at 1 */
for (w =0; W < mg.numvertexes; w++)/* Fix current Shortest path and distance */
{
if (!final[w] && (min + mg.arc[k][w] < d[w]))
{
/* Description found a shorter path, modify d[w] and p[w] */
D[w] = min + mg.arc[k][w];/* Modify the current path length */
P[W] = k;
}
}
}
/* End loop if p[w] = 0; indicates that the predecessor of Vertex W is POS */
}


int main (void
{
Mgraph MG;
Patharc P;
Shortpathtable D;
int I, j, pos =2;
Createmgraph (&AMP;MG);
Shortestpath_dijkstra (MG, POS, P, D);

cout <<"The shortest path in reverse order is as follows:" << Endl;
for (i =8; I >=0; i--)
{
j = i;
while (p[j]! =-1 && p[j]! =0)
{
cout <<"V" << J << "<-"  <<  "V"  << p[ J] <<              j = P[j];
        }
        cout <<  "V"  << j <<  "<-"  << " V " << pos << "    ";
        cout << endl;

    }
    cout << endl;

    return 0;
}
The output is:


Where the Createmgraph function is created by the adjacency matrix 7-7-7 shows.


I believe that through the above analysis, we can carry out their own cycle run program analysis, after the end of the cycle final = {1, 1, 1, 1, 1, 1, 1, 1, 1} indicates that all vertices have completed the shortest path to find work. At this point d = {4, 3, 0, 3, 1, 4, 6, 8, 12}, notice that we said earlier that the Dijkstra algorithm can find the shortest path of a source point to another vertex, and now we have the POS = 2 given in the program above, that is, the source point is V2, so d[2] = 0 Represents a path that is not to itself. The D array represents the shortest path length v2 to each vertex, such as d[8] =1+2 + 3 + 2 + 4 = 12. At this point P = {1, 0,-1, 4, 0, 4, 3, 6, 7}, it can be understood that p[2] = 1 means that V2 has no precursor vertex, p[1] = p[4] = 0 indicates that the precursor vertices of V1 and V4 are source points v2. Again such as p[8] = 7, indicating that the precursor of V8 is V7, again by p[7] = 6, indicating that the precursor of V7 is V6; p[6] = 3 means V6 is the precursor of V3, so you can get V2 to v8 the shortest path is V2->v4->v3->v6->v7-> V8, the output from the above program can also verify our speculation.

In fact, the final return of the array D and Arrays p, is to get V2 to any vertex of the shortest path and path length, that is, we solve the Dijkstra algorithm from a source point to the remaining vertices of the shortest path problem. The time complexity of this algorithm can be obtained from the loop nesting O (n^2), if we want to get the shortest path of any vertex to the rest of the vertices? The simplest way is to do each vertex as the source point of the Dijkstra algorithm, equal to the original algorithm on the basis of another cycle, at this time the entire algorithm is the complexity of O (n^3).

Shortest Path-Dijkstra (Dijkstra) algorithm

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.