Dijkstra Algorithm Small white description
Dijkstra algorithm, the book is actually very concise, look carefully, think carefully will understand.
If the space is not limited, of course, can be more detailed, then you follow the understanding, try to slowly speak.
One, the question:
The shortest path from a source point to a different point.
Note that the shortest path to a point is not thought of as a point. This is not the same idea.
Two, analysis.
Discard all the lemma, theorem, axiom ... of the shortest path in the book first.
Look at the picture first.
To find the shortest path from 0 pips to all points.
If, Xiaoming, stand at 0 o ' Day. He is very thirsty, the weather is very hot, no water to die of thirst, and every other point, there is a bottle of water, point and point between the transport is not the same, some cars, and some must walk.
Suppose the weight here represents the time spent between two points, such as 0 to the middle of the number 6, said. From 0 to 3 a slow train of 6 hours.
Open the map to see, in the end which is the shortest ah. He wanted to drink 3 points of water and found it to be 9 hours. Unexpectedly than first to 2, then to 4, 7 hours is long, then go to 4. You are silly. At 2 O ' Day, don't drink water and go to 4.
So Xiao Ming get a common sense, for the first bottle of water, need to transit point and arrive at the destination is certainly not the least time spent, at least can drink that transit station of water.
Go straight to the point and see which point takes less time. At least 1 points, as long as 3 hours.
(to the contrary, if there is a direct connection point C found a point D, and the distance is less than 3 hours, then the distance from 0 to the straight point C must be less than 3 hours < because also to subtract the distance from C to D, this and from 0 to all direct points, 0 to 1 is the least, 3 hours this fact violates)
Less nonsense, xiaoming directly from 0 to do the car to 1, spent 3 hours, drank to the first bottle of water, just drank water, Xiao Ming was Time machine, a sound, incredibly passed back 0 points. (because we want to start from 0, find the shortest path to all points, so xiaoming was sent.)
Xiao Ming subsided The pride that arose from the absurdity of the law,
Because Xiao Ming is still very thirsty, the weather is still very hot, do not drink water to die of thirst.
So where's the next closest point?
Xiao Ming began to think, 1 points is the closest to 0 straight. I've already drunk it. So to 1 points, the second near the direct connection point is not the latest?
Let's see. It's 6. It takes 4 hours.
The other direct connection points are all discarded first, because at this time 6 points are the closest to the direct connection point.
Is there any shorter than 4 hours? Yes, it was 1 points before, just 3 hours, but drank.
Xiao Ming thought for a while, there is the answer, the only possibility is that if the original 1 points, close to the place has a vertex. Then it is possible to be nearer than 6 points.
Take a look at the map, 1 to 4 and 5. Time is 5 and 8. The total time is 3+5 and 3+8. Significantly longer than 4 hours.
Xiao Ming immediately took the bus from 0 to 6 o ' clock. Drinking light water, no doubt, a sound of Xiao Ming was Time machine, and returned to 0 points.
But with the last thought.
Xiao Ming has a preliminary summary.
The next closest possible is 2 options to find a
1) and 0 directly connected to the point, but to remove 1, and 6. That is, and 0 is directly connected to the 3rd near Point.
2) from the earliest 1 points, can reach the point, although the last exclusion, but perhaps this time and the 3rd near the point of a spell.
I was about to look up the map. Xiao Ming suddenly thought. 6 points is the second most recent point. Maybe from 6, there are even shorter than the 3rd point.
At last, Xiao Ming arranged a plan. This is the Dijkstra algorithm.
Use an array of minrd[] to store the known shortest path.
With another array noget[], store all the points minus the end of all the shortest paths.
Each time from the shortest path minrd[], the endpoint of each shortest path is queried. Write down these end points and go straight to the point you never reached. and find the smallest.
Here to understand, if there is a shortest path through several points, such as 0->1->3->6->7. Xiao Ming drank 7 points of water last time,
It also means that he drank 6 points of water before, namely 0->1->3->6 is a shortest path,
0->1->3->6->7 is the shortest path from the 0->1->3->6, selected by brutal comparison of vertices of 6 vertices and other shortest paths.
This is in fact an introduction to the optimal path, the shortest path, the middle path is also the shortest path. Drinking water drank out the introduction.
Take pains, I want to write down every time the process of running the program.
1) Find water for the first time.
Known Shortest path
0 This is the origin, which is placed by default.
Have not yet reached the
1,2,3,4,5,6
Only one end 0. From the end, and the points have not yet reached the arc, find the most broken.
Compare V (0, 1), V (0,2), V (0,3), V (0,4), V (0,5), V (0,6)
0->1 shortest, added to the shortest path collection.
2)
Known Shortest path
0
0->1
Have not yet reached the
2,3,4,5,6
The end of the first shortest path is 0. The end of the second shortest path is 1
Starting at the end of 0 and 1, and the points that have not yet reached the arc, find the most broken.
Compare V (0, 2), V (0,3), V (0,4), V (0,5), V (0,6),
V (1, 2), V (1,3), V (1,4), V (1,5), V (1,6)
0->6 shortest, added to the shortest path collection
3)
Known Shortest path
0,
The first time 0->1 found it.
0->6 found the second time.
Have not yet reached the
2,3,4,5
Compare V (0,2), V (0,3), V (0,4), V (0,5)
V (1,3), V (1,4), V (1,5)
V (6,2), V (6,3), V (6,4), V (6,5)
Optimal path End 0,1,6,
Like 0->6->5 and 0->2,
Depending on the order of the program, one will be added, and the next one will join the other. You can also modify the program to let them join at once
C code is as follows (code readability is common)
structminroads{int*Road; intlength; intCost ;};intMain () {//Street-facing matrix, directly written out, there is no program generated, the number represents 2 points between the right of the arc, 1 means 2 points can not direct. intmatrix[7][7]={ 0,3,5,9,-1,6,4, 3,0,-1,-1,8,5,-1, 5,-1,0,-1,2,-1,-1, 9,-1,-1,0,-1,-1,-1, -1,8,2,-1,0,-1,-1, 6,5,-1,-1,-1,0,1, 4,-1,-1,-1,-1,1,0 }; structMinroads minrd[7];//initializing an array of shortest paths intI=0; for(i=0;i<7; i++) {Minrd[i].road=malloc(sizeof(int)*7); minrd[i].road[0]=0; Minrd[i].cost=0; Minrd[i].length=0; } intnoget[7]={0,1,2,3,4,5,6};//Initializes a collection of vertices that have never arrived. minrd[0].road[0]=0;//the source point itself (the starting point) as the first shortest path to join the shortest Path collection (contains 1 top. Right straight and for 0)minrd[0].length=1; minrd[0].cost=0; noget[0]=-1;//simply put the point of arrival, mark it as -1,0 add to change the value of index 0 to-1. When the Nouse element is all-1. Then it will all arrive. intGet_count=1;// //from each known shortest path, take a step and look. Compare all the resulting new paths to see who is the shortest. //The program description is to find out, in the MINRD, the right of each road, plus, the vertex of this road to the noget of all the vertices of the right straight in the smallest. while(get_count<7) { inti_noget,i_shortest; inttemp_short=0x7fffffff;//int is a signed number, the highest bit is 0, which indicates the maximum positive, inttemp_s, Tmep_d; for(i_shortest=0;i_shortest<7; i_shortest++)//All shortest paths { if(minrd[i_shortest].length!=0)// { for(i_noget=0;i_noget<7; i_noget++)//the vertex never reached { if(noget[i_noget]!=-1) { intx; X=minrd[i_shortest].road[minrd[i_shortest].length-1];//vertex of Shortest path inty=Noget[i_noget]; intNewshort; if(matrix[x][y]!=-1) {Newshort=minrd[i_shortest].cost+Matrix[x][y]; } Else{Newshort=0x7fffffff; } if(newshort<=Temp_short) {Temp_short=Newshort; temp_s=i_shortest; Tmep_d=I_noget; } } } } } inti_copy; for(i_copy=0; i_copy<minrd[temp_s].length;i_copy++) {Minrd[get_count].road[i_copy]=Minrd[temp_s].road[i_copy]; } Minrd[get_count].road[i_copy]=tmep_d; Minrd[get_count].length=minrd[temp_s].length+1; Minrd[get_count].cost+=Temp_short; Noget[tmep_d]=-1; Get_count++; } intI_print; for(i=0;i<7; i++) { for(i_print=0; i_print<minrd[i].length;i_print++) { if(i_print!=minrd[i].length-1) {printf ("%d->", Minrd[i].road[i_print]); } Else{printf ("%d", Minrd[i].road[i_print]); }} printf ("cost:%d", Minrd[i].cost); printf ("\ n"); }}
Results:
Pay attention again here
0->6->5. The best way to find out. There must be 0->6 this optimal path.
Close your eyes to think about, 0->6->5 is the best words, no reason, 0->6 not. If 0->6 is not, it is 0->x. So, 0->x->5, it must be shorter than 0->6->5.
Dijkstra Algorithm Small white description