Dijela algorithm for shortest path algorithm

Source: Internet
Author: User
Dijkstra is a typical shortest path routing algorithm used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses many computing nodes. Dijkstra algorithm is a representative short-circuit algorithm.

Dijkstra is a typical shortest path routing algorithm used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses many computing nodes.

Dijkstra algorithm is a representative short-circuit algorithm. it has been described in detail in many professional courses, such as data structure, graph theory, and operational research.

The basic idea is to set the vertex set S and constantly make greedy choices to expand the set. A vertex belongs to the set S and is known only when the shortest path length from the source to the vertex is known.

Initially, S contains only the source. If u is a vertex of G, the path from source to u and passing through only the vertex in S is called a special path from source to u, use the dist array to record the shortest path length corresponding to each vertex. Dijkstra algorithm extracts vertex u with the shortest special path length from the V-S each time, add u to S, and make necessary modifications to the array dist. Once S contains all vertices in V, dist records the shortest path length between the source and all other vertices.

For example, for directed graphs in the following table, the Dijkstra algorithm is used to calculate the shortest path between source vertex 1 and other vertices.

Dijkstra algorithm iteration process:

Have a good understanding of the topic!

The specific implementation is as follows (C/C ++ ):

/*************************************** *? About :???? Dijkstra algorithm implementation of digraph *? Author :??? Tanky? Woo *? Blog :????? Www.WuTianQi.com *************************************** /# include?
 
  
Using? Namespace? Std; const? Int? Maxnum? =? 100; const? Int? Maxint? =? 999999; void? Dijkstra (int? N ,? Int? V ,? Int? * Dist ,? Int? * Prev ,? Int? C [maxnum] [maxnum]) {bool? S [maxnum]; ??? //? Determine whether the point has been saved to the for (int? I = 1 ;? I <= n ;? ++ I) {dist [I]? =? C [v] [I]; s [I]? =? 0 ;????? //? If (dist [I]? ==? Maxint) prev [I]? =? 0; else prev [I]? =? V;} dist [v]? =? 0; s [v]? =? 1 ;//? Sequentially place the node not included in the S set, take the node with the minimum dist [] value, and put it in the combined S //? Once S contains all vertices in V, dist records the shortest path length from the source point to all other vertices for (int? I = 2 ;? I <= n ;? ++ I) {int? Tmp? =? Maxint; int? U? =? V ;//? Find the dist [j] minimum value for (int? J = 1 ;? J <= n ;? ++ J) if ((! S [j])? &&? Dist [j]
  
   
= 1 ;? -- I) if (I ?! =? 1) cout? <
   ? "; Else cout? <
   >? N ;//? Number of input paths cin >>? Line; int? P ,? Q ,? Len ;?????????? //? Input p ,? Q two points and their path length //? Initialize c [] [] as maxint for (int? I = 1 ;? I <= n ;? ++ I) for (int? J = 1 ;? J <= n ;? ++ J) c [I] [j]? =? Maxint; for (int? I = 1 ;? I <= line ;? ++ I) {cin? >>? P?>? Q? >>? Len; if (len?
   
   

Input data:
5
7
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60
Output data:
999999 10 999999 30 100
10 999999 50 999999 999999
999999 50 999999 20 10
30 999999 20 999999 60
100 999999 10 60 999999
Minimum path length from the source to the last vertex: 60
The path from the source point to the last vertex is: 1-> 4-> 3-> 5

The last two questions are provided. you can apply the template directly:
1. HDOJ 1874 smooth engineering resumption
Http://www.wutianqi.com /? P = 1894

2. the shortest path of HDOJ 2544
Http://www.wutianqi.com /? P = 1892

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.