The algorithm of single source shortest path-dijkstra

Source: Internet
Author: User

The Dijkstra algorithm is used to solve the single source shortest path problem, and can solve the minimum distance from one point to all remaining nodes.

Principle:

Using greedy selection, divides all the nodes into two parts, has found the shortest path of the point and not to find the shortest path point, the original source point is the only one to find the shortest distance point, each time never find the shortest path point in the selected distance from the source point of the nearest point is set to find the shortest path point and record the corresponding shortest distance.

Specific process:

The time bottleneck of the algorithm is how to get the distance from a node to the source point and find the number of the shortest distance point.

The workaround is to spend extra space to define a Lowdis array that represents the shortest distance from the corresponding subscript node to the source point. If a node and the source point are directly connected at initialization time, the corresponding element is initialized to the corresponding path length, otherwise it is initialized to an infinite inf, each time the node with the lowest value is selected, and the Lowdis array is updated, the reason for the update is that there may be lowdis[i]> after adding a new node. Lowdis[minn]+dis[minn][i], where Minn is the number of the newly found node with the shortest distance, that is, the current node may be closer by Minn node.

If you want to count the shortest Path node information, you can add a last array, hold the previous node number for each node, and then output it in reverse order.

Example code: Give the information of a graph to find the shortest distance and shortest path from one point to another point.

[CPP]View PlainCopy
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stack>
  4. Using namespace std;
  5. #define MAXN 100
  6. #define INF 99999
  7. int DIS[MAXN][MAXN]; //Store The distance between the highlights
  8. int LAST[MAXN]; //Store the previous node of each node
  9. int N; //number of storage nodes
  10. int search (int START,int END) { //Dijkstra algorithm
  11. bool Vis[maxn]={0}; //Tagged access condition
  12. Vis[start]=true;
  13. int LOWDIS[MAXN];
  14. For (int i=0;i<=n;i++) {
  15. Lowdis[i]=dis[start][i];
  16. Last[i]=start;
  17. }
  18. int Min,minn;
  19. For (int i=1;i<n;i++) {
  20. Min=inf;
  21. For (int j=1;j<=n;j++) {
  22. if (vis[j]==0) {
  23. if (lowdis[j]<min) {
  24. MIN=LOWDIS[J];
  25. Minn=j;
  26. }
  27. }
  28. }
  29. if (min<inf) {
  30. Vis[minn]=true;
  31. For (int j=0;j<=n;j++) { //update Lowdis array
  32. if (Lowdis[j]>dis[minn][j]+lowdis[minn]) {
  33. Lowdis[j]=dis[minn][j]+lowdis[minn];
  34. Last[j]=minn;
  35. }
  36. }
  37. }
  38. else return-1;
  39. }
  40. return lowdis[end];
  41. }
  42. void Showway (int START,int END) { //Print Shortest road information
  43. stack<int>; //Use stack to reverse output
  44. Way.push (END);
  45. int at=end;
  46. While (At!=start) {
  47. Way.push (Last[at]);
  48. At=last[at];
  49. }
  50. While (!way.empty ()) {
  51. Cout<<way.top () <<";
  52. Way.pop ();
  53. }
  54. }
  55. int main () { //test
  56. int m,a,b,c;
  57. For (int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) Dis[i][j]=dis[j][i]=inf;
  58. cout<<"How many locations are there in total number of roads?"  <<endl;
  59. cin>>n>>m;
  60. cout<<"Enter road information (enter the two location number and length of the connection, such as 1 2 3 for 1, 2 for roads with a length of 3)" <<endl;
  61. While (m--) {
  62. cin>>a>>b>>c;
  63. Dis[a][b]=dis[b][a]=c;
  64. }
  65. cout<<"Where to" <<endl;
  66. cin>>a>>b;
  67. cout<<"Minimum Distance:" <<search (A, b) <<endl;
  68. cout<<"Shortest road:";
  69. Showway (A, b);
  70. cout<<endl;
  71. }

Attach a simple set of test data

5 7

1 2 3 2 3 4 3 4 5 4 5 6 1 3 1 4 1 10 1 5 99

1 5

The algorithm of single source shortest path-dijkstra

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.