Data structure and algorithm analysis (v)--Shortest Path algorithm

Source: Internet
Author: User

    • 0) Introduction

As the name suggests, the shortest path algorithm is to find the shortest path or distance from one point to another in a graph.

The shortest path algorithm is generally divided into four situations:

A) Shortest path with no weight

b) The shortest path with the right to heavy

c) A figure with a negative weight on the side

d) Non-circle diagram

PS: The above situation is directed to the graph.

    • 1) Shortest path with no weight

Is an example: suppose we take a point V3 as the initial point, calculate the point V3 to the path and distance (including Point V3) in the graph.

A) the V3 to V3 path length is 0.

b) Follow the V3 adjacency point to find the V1, then the V3 to V1 path length is 1; V6 is found, then the V3 to V6 path length is 1.

c) The adjacency point of the V3 has been found and the next point of V1,v6 is found.

D) The adjacency point of the V1 is v2,v4, and their corresponding path length is 2;v6 no adjacency point, the V6 part ends.

e) The adjacency point of the V2 is V4,V5, and V4 has been looked up, discarded, V2 to V5 's path is 3;V4, and V5,v7 has been looked up, discarded, v5 to V4 path length is 3.

f) to the end.

For this step, you can use a table to express it in order to understand it programmatically:

There are 3 variables representing states in the table:

A) DV represents the distance from the point s to each point in the graph, combined with the above example, S=v3.

b) PV indicates the forward point of the path at the midpoint of the diagram.

c) Known indicates whether the point is processed and processed, then marked as 1.

Pseudo code:

1 voidunweighted (Table T)2 {  3     intcurrdist; 4 Vertex v,w; 5      for(currdist=0; currdist<numvertex; currdist++)  6     {  7          forEach vertex V8             if(! T[V]. KNOWN&AMP;&AMP;T[V]. dist==currdist)9             {  TenT[V]. Known =True;  One                  forEach W adjacent to V A                     if(T[w]. Dist = =Infinity) -                     {   -T[W]. Dist = currdist+1;  theT[W]. Path =V;  -                     }   -             }   -     }   +}
View Code

Through topological sequencing, for the above program is also where there are improvements (line No. 07), we do not have to go to each vertex v to do so many steps, only need to be adjacent to the point to do just fine. Therefore, a queue is used to enqueue each of the processed points, and then its adjacency points are processed.

1 voidunweighted (Table T)2 {  3 Queue Q; 4 Vertex v,w; 5   6Q =Createqueue (Numvertex); 7 Makeempty (Q); 8Enqueue (S,Q);//S is the start Vertex9   Ten      while(!IsEmpty (Q)) One     {   AV =Dequeue (Q);  -T[V]. known=True;  -          forEach W adjacent to V the             if(T[w]. Dist = =Infinity) -             {   -T[W]. Dist = T[v]. dist+1;  -T[W]. Path =V;  + Enqueue (W,Q);  -             }   +     }   A Free (Q);  at}
View Code

    • 2) The shortest path with the right weight

The shortest path algorithm, called Dijkstra ' s algorithm, is entitled to heavy. With respect to the shortest path algorithm with no right weight, the shortest path algorithm with a weighted weight can be difficult, because the introduction of weights reverses the weight length of some paths.

Dijkstra ' s algorithm is a greedy algorithm. The greedy algorithm takes the maximum value at every stage of the algorithm. In general, the greedy algorithm can achieve better results. But greedy algorithm is flawed, that is, to achieve the local optimal, and may not be able to achieve the global optimal, for example, if you want to redeem 15 cent of the coin, and the coins have 12 points, 10 points, 5 points, 1 points, then according to the greedy algorithm, the final conversion result is 1 12 points, 3 a point , and the optimal result should be a 10-point, a 5-point. (Here we define the most optimal number of coins).

Dijkstra ' s algorithm steps are:

For each stage, Dijkstra's algorithm selects a minimum distance of Point v at all the unhandled points, and then defines the path to the given point S to V as the minimum path.

Here is an example:

The form of the corresponding table represents:

Here are the specific implementation instructions:

A) the initial point s=v1; calculates the shortest path from S to figure.

b) Locate the point v2,v4 of the V1 adjacency, marking the length of the path from S to V2,v4, and v1 labeled T[V1]. Known=1.

c) because the V4 path length is small, select V4, find the V4 adjacency Point v3,v5,v6,v7, label their path length, and v1 annotations to the t[v4]. Known=1.

D) All existing unhandled paths are V2 shortest, so the V2 is processed to find the V2 adjacency point v4,v5; v2 to v4,v5 path is longer than the V4,V5 path long, so it is not updated. At the same time T[v2]. Known=1.

e) All existing unhandled paths have the shortest v3,v5, processing v3, adjacency points of v1,v6, paths of 7, 8, and V3 to V6, because 8<9, so there is no need to update the distance v3 to v1. At the same time T[v3]. Known=1.

f) for V5, the path of the adjacency point v7,v5 to V7 is 3+6>5 and therefore not updated; T[V5]. Known=1.

g) The path of the adjacency point for processing v7,v7 is now 5+1<8 for V6,V7 to V6, so it needs to be updated and T[V7]. Known=1.

h) Now the processing v6,v6 has no adjacency point, so it can be ended. At the same time T[V6]. Known=1.

Pseudo-code implementation:

1typedefintVertex; 2typedefintDisttype; 3   4 structTableentry5 {  6 List Header; 7     intknown; 8 Disttype Dist; 9 Vertex Path; Ten }   One    A #defineNotavertex (-1) -typedefstructTableentry Table[numvertex];  -    the voidinittable (Vertex start,graph g,table T) - {   -     inti;  - readgraph (g,t);  +      for(i=0; i<numvertex;i++)   -     {   +T[i]. Known =0;  AT[i]. Dist =Infinity;  atT[i]. Path =Notavertex;  -     }   -T[start]. Dist =0;  - }   -    - voidDijkstra (Table T) in {   - Vertex v,w;  to      for(;;)  +     {   -V =Findsmallestunknowndistancevertex ();  the         if(v==Notavertex) *              Break;  $T[V]. Known =Ture; Panax Notoginseng          forEach W adjacent to V -             if(!T[v]. Known) the                 if(T[v]. dist+cvw<T[w]. Dist) +                 {   ADecrease (t[w]. Dist to T[v]. dist+CVW);  theT[W]. Path =V;  +                 }   -     }   $}
View Code

Relative to the shortest path with no weight, here the weights are updated to Dist = Dist + Cvw;

    • 3) The weight of the edge is a negative figure

The weight of the edge is negative, which would be a bad thing to do with the two methods above, because as long as the loop negative edges, the path can be infinitely smaller, as shown in:

The shortest path to point 1 to 6 is 2? Of course not, we can't find the shortest path, because we can go down the path 1,6,5,7,6 such a time, the path is-3, because 6,5,7,6 is a circle, we can keep looping and the path is negative, so we cannot determine the shortest path of point 1 to 6.

So how do you solve this problem?

A) to all weights plus a positive value, is the title weight is non-negative, the above example, all the weights plus 11, you can apply the Dijkstra method processing.

    • 4) Non-circle diagram

The Dijkstra method can also be applied to the non-circle graph, and the non-circle graph can be regarded as a special case of the above. For the non-circle graph, you can use the order of topological sorting to select the starting point, the weight of the update can be sorted by the order of the topology, so the algorithm can be done one at a time, this can be seen as the improvement of the Dijkstra method, and is a simple direction of improvement.

No-circle graphs can simulate problems like downhill skiing, which have always been needed along the downward direction and there should be no laps.

The largest application of non-circle graphs is not here, but is an application called Critical path analysis method.

As shown in the following:

This picture means that in order to do things in D, a and B must be done first, while a A, a, and a, can be parallel. So the problem in this diagram is not to find the shortest path, but to find the shortest time, and the latest time, required to complete all the points in the graph in parallel.

To solve this problem, we need to convert the above Action node graph to the following event node graph

For the event node graph, it's just a matter of finding the longest path of the first event to the last event.

Assuming that ECI represents the earliest completion time of node I, then use the following law

The earliest completion time results are:

Transferred from: http://blog.csdn.net/changyuanchn/article/details/17077379

Data structure and algorithm analysis (v)--Shortest Path 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.