Simple Dijkstra algorithm template and dijkstra Algorithm

Source: Internet
Author: User

Simple Dijkstra algorithm template and dijkstra Algorithm

Simple Dijkstra does not need to use the heap. Every time you find the minimum value, you have to traverse it. The time complexity is high.

Vertices are n vertices ranging from 0 to n-1.

The following is a template:

Int Dijkstra (int a, int B) // a is the start point, and B is the end point {bool visited [maxn]; // records the Access Point int pos = a, min, dist [maxn]; // pos indicates the vertex corresponding to each smallest edge. dist records the shortest distance from the start point to each vertex. For (int I = 0; I <n; I ++) {dist [I] = map [a] [I]; visited [I] = 0 ;} dist [a] = 0; // It must be 0 to itself visited [a] = 1; // start point mark accessed for (int I = 0; I <n-1; I ++) // The n-1 points after traversal {min = INF; // for min, you must first define a large number for (int j = 0; j <n; j ++) if ((! Visited [j]) & dist [j] <min) // search for the smallest edge {pos = j; // search for the flag min = dist [pos];} if (min = INF) break; // if the minimum value is not updated, it indicates that there is no link to the end, and visited [pos] = 1 does not need to be run; // The tag point has been accessed for (int j = 0; j <n; j ++) if (! Visited [j] & map [pos] [j]> dist [pos] + map [pos] [j]) dist [j] = dist [pos] + map [pos] [j]; // update dist} return dist [B]; // return the shortest distance from a to B}





Dijkstra algorithm source code

Dijkstra algorithm -- c ++ source code --

# Include <iostream. h>
Void main ()
{
Int infinity = 100, j, I, n, k, t, ** w, * s, * p, * d;
Cout <"input the value of n :";
Cin> n;
Cout <endl;

D = new int [n];
S = new int [n];
P = new int [n];
W = new int * [n];
For (I = 0; I <n; I ++) {w [I] = new int [n];}
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Cin> w [I] [j];

For (s [0] = 1, I = 1; I <n; I ++)
{
S [I] = 0; d [I] = w [0] [I];
If (d [I] <infinity) p [I] = 0;
Else p [I] =-1;
}

For (I = 1; I <n; I ++)
{
T = infinity; k = 1;
For (j = 1; j <n; j ++)
If ((! S [j]) & (d [j] <t) {t = d [j]; k = j ;}
S [k] = 1; // point k join the S
For (j = 1; j <n; j ++)
If ((! S [j]) & (d [j]> d [k] + w [k] [j])
{D [j] = d [k] + w [k] [j]; p [j] = k ;}

}
Cout <"the shortest distance from the source point to other vertices is as follows :";
For (I = 1; I <n; I ++) cout <d [I] <"";

}
/*********
The number of vertices is represented by n. Here n = 6
100 1 12 100 100 100
100 100 9 3 100 100
100 100 100 100 5 100
100 100 4 100 13 15
100 100 100 100 100 4
100 100 100 100 100 100
For specific examples, see 148 page of algorithm design skills and analysis by e-Industry Publishing House
************/

Dijkstra algorithm problems

The time complexity of the dijkstra algorithm is O (n² ),
It may be set to kn 2, where items with less than 1 times are ignored
K (10 × 10) = 10 ms
Then k (40 × 40) = 16 [k × (10 × 10)] = 160 ms

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.