Dijkstra algorithm details

Source: Internet
Author: User

Dijkstra algorithm details:
The classic Dijkstra algorithm is often used to solve the single-source shortest path problem. The essence of the algorithm is to generate the Shortest Path in sequence by increasing the path length.

The following describes the general process of the algorithm:

1. Initialize all nodes and set the start point as a mark to enter the following cycle
2. Find the smallest and unlabeled point in the shortest path to a certain point (represented by a one-dimensional array)
For example:
Array Subscript: 0 1 2 3 4 5
Len:-0 5 10 2-
This array indicates that node 1 is the initial node. The shortest path for node 1 to reach Node 2 is 5 and node 3 is 10, the number 5 cannot be reached (the path can be expressed in a large number ).
Find one unlabeled and the Len is the shortest, and record it with another array.
For example:
Array Subscript: 0 1 2 3 4 5
MARK: 0 1 0 0 1 0
This array indicates that the shortest path from the initial node to node 4 has been found

From the above two arrays, we can conclude that the point found in this loop is node 2, and the next step will be taken.

3. Mark the found vertex and use it as the intermediate vertex to recalculate the shortest path of all unlabeled vertices (update the Shortest Path table)
4. Loop Step 1 to n-1 (n is the number of vertices. If there are still unmarked vertices, it means they cannot reach this point)


The following is the core code:
[Cpp]
While (count <n)
{
Tempmin = INFINITE;
For (I = 1; I <= n; I ++)
{
If (in [I] = 0 & Len [I] <tempmin) // find the smallest one
{
Tempmin = Len [I];
Si = I;
}
}
In [si] = 1;
Count ++;
For (I = 1; I <= n; I ++) // updata the length
{
If (in [I] = 0 & (tempmin + mGraph. matrix [si] [I]) <Len [I])
{
Len [I] = tempmin + mGraph. matrix [si] [I];
}
}
}

The core part is the two for loops above. The first for loop traverses all nodes and finds the unmarked and shortest path; the second for loop is to traverse the Shortest Path node found above all nodes and update the Shortest Path table for the intermediate point; note: Mark the point to be found after the first for loop (the shortest path to this point is found)

Here is an example:

If there is such a directed graph (this figure is Yan Weimin p188 page of "Data Structure", which is not described in detail in the book, this is an example)
The V0-V5 has 6 nodes in total, the path between nodes has been marked, the shortest path from V0 to other nodes is now required;

The preceding algorithm flow shows that the Dijkstra algorithm requires several structures to save the desired information:
1. Save the structure of this image
2. Record the Shortest Path array from V0 to other nodes (set as Len [n + 1])
3. record whether a node has found the Shortest Path array (in [n + 1])

The following is the Algorithm Implementation part:
1. mark V0 -- in [1] = 1 initialize Len [] = {INFINITE, 0, INFINITE, 10, INFINITE, 30,100} here the first element of the array is not used, array subscript starting from 1 indicates V0 and so on
2. in the first cycle, V2, V4, and V5 are adjacent to V0. The shortest distance between V2 and V0 is 10, V2 is marked, and V2 is the center point (Path: V0-> V2-> Vx) update the Shortest Path table. In this case, V3 is updated to 10 + 50 = 60 ,.
3. enter the second cycle. V1, V3, V4, and V5 are not marked at this time. The temporary shortest paths from V0 to these are INFINITE, 60, 30, and 100, respectively, find the smallest vertex V4, Mark V4 as 1, and update the shortest table with V4 as the intermediate vertex (path V0-> V4-> Vx). In this case, V3 is updated to 50, v5 is updated to 90.
4. enter the third cycle. V1, V3, and V5 are not marked at this time. The temporary shortest paths are INFINITE, 50, and 90 respectively. Find the smallest one, V3, and Mark V3 as 1, update the shortest table with V3 as the center point (Path: V0-> V4-> V3-> Vx). Then, V5 is updated to 60.
5. enter the fourth cycle. V1 and V5 are not marked at this time. The temporary shortest paths are INFINITE and 60, V5 is found, and 1 is marked, update the shortest table with V5 as the intermediate point. No elements are updated at this time.
6. Enter the fifth cycle. Nothing was found in this cycle.
7. Exit the loop. The Len table is the shortest path from V0 to other nodes.


The above is the most basic idea of Dijkstra algorithm. Of course, when looking for the shortest path, we often need to find the shortest path to reach a certain point. below, let's take a look at how to record the shortest path to a certain point:

Adding the shortest path to the Dijkstra algorithm is actually very simple. You only need to save the parent node serial number of the vertex when the shortest path is updated. At last, you can roll back the intermediate node and use the stack output.
Finally, the complete code is provided directly (the code is very general and I hope you can advise ):
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Deprecision MAX_LEN 100
# Define INFINITE 1000
Typedef struct graph
{
Int nodenum;
Int edgenum;
Int matrix [MAX_LEN] [MAX_LEN];
} Graph;
 
Typedef struct stack
{
Int bottom;
Int top;
Int printout [MAX_LEN];
} Mstack;
 
Int in [MAX_LEN];
Int Len [MAX_LEN];
Int path [MAX_LEN];
 
Void InitStack (mstack * s)
{
S-> bottom = 0;
S-> top = 0;
Memset (s-> printout, 0, sizeof (int) * MAX_LEN );
}
 
Void push (mstack * s, int m)
{
S-> printout [s-> top ++] = m;
}
 
Int pop (mstack * s)
{
Return s-> printout [-- s-> top];
}
 
Void InitGraph (Graph * g, int n)
{
Int I, j;
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
{Www.2cto.com
If (I = j) g-> matrix [I] [j] = 0;
Else g-> matrix [I] [j] = INFINITE;
}

For (I = 1; I <= n; I ++)
{
In [I] = 0;
Len [I] = INFINITE;
Path [I] = 0;
}
}
 
Int main ()
{
 
Int n, m, I, A, B, templen, count, min, tempmin, si, temp;
While (scanf ("% d", & n, & m ))
{
Count = 0;
Graph mGraph;
MGraph. edgenum = m;
MGraph. nodenum = n;
InitGraph (& mGraph, n );
For (I = 0; I <m; I ++)
{
Scanf ("% d", & A, & B, & templen );
MGraph. matrix [A] [B] = templen;
}

 
In [1] = 1;
Path [1] = 1; // sava path
Len [1] = 0;

For (I = 2; I <= n; I ++)
{
Len [I] = mGraph. matrix [1] [I]; // Init the len
If (Len [I]! = INFINITE) path [I] = 1;
}

Min = 0;
Si = 1;

While (count <n-1)
{
Tempmin = INFINITE;
For (I = 1; I <= n; I ++)
{
If (in [I] = 0 & Len [I] <tempmin) // find the smallest one
{
Tempmin = Len [I];
Si = I;
}
}
In [si] = 1;

For (I = 1; I <= n; I ++) // updata the length
{
If (in [I] = 0 & (tempmin + mGraph. matrix [si] [I]) <Len [I])
{
Len [I] = tempmin + mGraph. matrix [si] [I];
Path [I] = si;
}
}
Count ++;
}

Mstack s;
For (I = 1; I <= n; I ++)
{
Temp = I;
InitStack (& s );
If (path [temp] = 0)
{
Printf ("no path \ n ");
Continue;
}
While (path [temp]! = 1)
{
Push (& s, path [temp]);
Temp = path [temp];
}
Printf ("1 --> ");
While (s. bottom! = S. top)
{
Printf ("% d -->", pop (& s ));
}
Printf ("% d min length is % d \ n", I, Len [I]);

}

}
Return 0;
}

Attach the result of the figure above (V0 node is 1, V1 node is 2 .... And so on)


Related Article

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.