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
- #include <iostream>
- #include <cstring>
- #include <stack>
- Using namespace std;
- #define MAXN 100
- #define INF 99999
- int DIS[MAXN][MAXN]; //Store The distance between the highlights
- int LAST[MAXN]; //Store the previous node of each node
- int N; //number of storage nodes
- int search (int START,int END) { //Dijkstra algorithm
- bool Vis[maxn]={0}; //Tagged access condition
- Vis[start]=true;
- int LOWDIS[MAXN];
- For (int i=0;i<=n;i++) {
- Lowdis[i]=dis[start][i];
- Last[i]=start;
- }
- int Min,minn;
- For (int i=1;i<n;i++) {
- Min=inf;
- For (int j=1;j<=n;j++) {
- if (vis[j]==0) {
- if (lowdis[j]<min) {
- MIN=LOWDIS[J];
- Minn=j;
- }
- }
- }
- if (min<inf) {
- Vis[minn]=true;
- For (int j=0;j<=n;j++) { //update Lowdis array
- if (Lowdis[j]>dis[minn][j]+lowdis[minn]) {
- Lowdis[j]=dis[minn][j]+lowdis[minn];
- Last[j]=minn;
- }
- }
- }
- else return-1;
- }
- return lowdis[end];
- }
- void Showway (int START,int END) { //Print Shortest road information
- stack<int>; //Use stack to reverse output
- Way.push (END);
- int at=end;
- While (At!=start) {
- Way.push (Last[at]);
- At=last[at];
- }
- While (!way.empty ()) {
- Cout<<way.top () <<";
- Way.pop ();
- }
- }
- int main () { //test
- int m,a,b,c;
- For (int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) Dis[i][j]=dis[j][i]=inf;
- cout<<"How many locations are there in total number of roads?" <<endl;
- cin>>n>>m;
- 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;
- While (m--) {
- cin>>a>>b>>c;
- Dis[a][b]=dis[b][a]=c;
- }
- cout<<"Where to" <<endl;
- cin>>a>>b;
- cout<<"Minimum Distance:" <<search (A, b) <<endl;
- cout<<"Shortest road:";
- Showway (A, b);
- cout<<endl;
- }
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