Dijkstra Algorithm for single-source shortest path (1)

Source: Internet
Author: User

Dijkstra implemented in this articleAlgorithmThe simplest method is brute-force search. The time complexity is O (V ^ 2). The priority queue method will be used later to modify the breadth-first traversal to implement the Dijkstra algorithm, the time complexity of this algorithm will be improved.

CodeInstance:

/* <Br/> reference: http://baike.baidu.com/view/7839.htm <br/> algorithm flow: <br/> In the following description, S is the source, W [U, v] is the length of the edge between the vertex u and v, and the result is stored in DIST [] <br/> 1. initialization: the distance from the source Dist [s] is set to 0, the distance from other points is set to infinite, and the status of all points is set to not extended. <Br/> 2. Loop n-1 times: <br/> In a point that has not been extended, obtain the U point with the smallest distance and set its status to extended. <Br/> execute relax (u, v) for each vertex v adjacent to U, that is, if Dist [u] + W [U, v] <Dist [v], <br/> change Dist [v] to a shorter distance from Dist [u] + W [U, V]. In this case, the first node is u in the shortest path of point v. <Br/> 3. End. In this case, for any U, DIST [u] is the distance from S to u. <Br/> direct implementation: (the method used in this instance) <br/> the simplest implementation method is to find the nearest point in each cycle, then, any method is used to update the adjacent edge. The time complexity is obviously O (n2) <br/> */<br/> # include <fstream> <br/> # include <cstring> <br/> # include <iostream> <br/> # include <stdlib. h> <br/> using namespace STD; <br/> const int maxnum = 1000000; // maximum edge weight <br/> int N; // number of nodes <br/> int Dist [501]; // the shortest path to node 1 <br/> bool State [501]; // indicates the status of the node that has been searched <br/> int data [501] [501]; // adjacent matrix <br/> // find the node with the smallest weight <br/> in T findmin () <br/>{< br/> int minnode = 0, min = maxnum, I; <br/> for (I = 1; I <= N; I ++) <br/> If (Dist [I] <min )&&(! State [I]) <br/>{< br/> min = DIST [I]; <br/> minnode = I; <br/>}< br/> return minnode; // If minnode is set to 0, no minimum node is found. <br/>}< br/> void Dijkstra () <br/>{< br/> cout <"Number of Input Points:" <Endl; <br/> CIN> N; // number of input vertices <br/> // construct an adjacent matrix. The distance between the vertex and itself is infinite. <Br/> cout <"input right of the adjacent matrix:" <Endl; <br/> for (INT p = 1; P <= N; P ++) <br/> for (INT q = 1; q <= N; q ++) <br/>{< br/> // In> data [p] [Q]; <br/> CIN> data [p] [Q]; <br/> If (data [p] [Q] = 0) <br/> data [p] [Q] = maxnum; <br/>}< br/> // initialization <br/> for (INT I = 1; I <= N; I ++) <br/> {<br/> State [I] = 0; // initialization, indicates that all vertices have not been extended <br/> Dist [I] = data [1] [I]; // data [1] [I] indicates that 1 is the source point <br/>}< br/> State [1] = true; <br/> int done = 1; <br/> while (done <n) // loop n-1 times <br/> {<Br/> int node = findmin (); <br/> If (node! = 0) <br/>{< br/> done ++; // Add 1 to the number of points found <br/> State [node] = true; // indicates that the shortest path from node 1 to node has been found. <br/> for (INT I = 1; I <= N; I ++) // update the PATH value of a vertex not found <br/> If (Dist [I]> Dist [node] + data [node] [I]) & (! State [I]) <br/> Dist [I] = DIST [node] + data [node] [I]; <br/>}< br/> else <br/> break; <br/>}< br/> int main () <br/> {<br/> Dijkstra (); <br/> for (int K = 1; k <= N; k ++) <br/> {<br/> If (Dist [k] = maxnum) <br/> cout <-1; <br/> else <br/> cout <Dist [k]; <br/> If (k = N) <br/> cout <Endl; // final output line feed <br/> else <br/> cout <""; // if it does not reach the end, it is separated by null characters <br/>}< br/> system ("pause"); <br/> return 0; <br/>}< br/>/* <br/> Number of Input Points: <br/> 5 <br/> enter the right of the adjacent matrix: <br/> 0 4 2 0 0 <br/> 0 0 3 2 3 <br/> 0 1 0 4 5 <br/> 0 0 0 0 0 <br/> 0 0 0 1 0 <br/> */

PS: 2011-6-15

Modified:

Output THE Dist [I] Changes in the process

# Include <fstream> <br/> # include <cstring> <br/> # include <iostream> <br/> # include <stdlib. h> <br/> using namespace STD; <br/> const int maxnum = 1000000; // maximum edge weight <br/> int N; // number of nodes <br/> int Dist [501]; // the shortest path to node 1 <br/> bool State [501]; // indicates the status of the node that has been searched <br/> int data [501] [501]; // adjacent matrix <br/> // find the node with the smallest weight <br/> int findmin () <br/> {<br/> int minnode = 0, min = maxnum, I; <br/> for (I = 1; I <= N; I ++) <br/> If (Dist [I] <min) &&(! State [I]) <br/>{< br/> min = DIST [I]; <br/> minnode = I; <br/>}< br/> return minnode; // If minnode is set to 0, no minimum node is found. <br/>}< br/> void Dijkstra () <br/>{< br/> cout <"Number of Input Points:" <Endl; <br/> CIN> N; // number of input vertices <br/> // construct an adjacent matrix. The distance between the vertex and itself is infinite. <Br/> cout <"input right of the adjacent matrix:" <Endl; <br/> for (INT p = 1; P <= N; P ++) <br/> for (INT q = 1; q <= N; q ++) <br/>{< br/> // In> data [p] [Q]; <br/> CIN> data [p] [Q]; <br/> If (data [p] [Q] = 0) <br/> data [p] [Q] = maxnum; <br/>}< br/> // initialization <br/> for (INT I = 1; I <= N; I ++) <br/> {<br/> State [I] = 0; // initialization, indicates that all vertices have not been extended <br/> Dist [I] = data [1] [I]; // data [1] [I] indicates that 1 is the source point <br/>}< br/> cout <"Dist [I] in the output process" <Endl; <br/> for (INT I = 1; I <= N; I ++) <br/> {<B R/> cout <Dist [I] <""; <br/>}< br/> cout <Endl; <br/> State [1] = true; <br/> int done = 1; <br/> while (done <n) // loop n-1 times <br/>{< br/> int node = findmin (); <br/> If (node! = 0) <br/>{< br/> done ++; // Add 1 to the number of points found <br/> State [node] = true; // indicates that the shortest path from node 1 to node has been found. <br/> for (INT I = 1; I <= N; I ++) // update the PATH value of a vertex not found <br/> If (Dist [I]> Dist [node] + data [node] [I]) & (! State [I]) <br/> Dist [I] = DIST [node] + data [node] [I]; <br/> for (Int J = 1; j <= N; j ++) <br/> cout <Dist [J] <""; <br/> cout <Endl; <br/>}< br/> else <br/> break; <br/>}< br/> int main () <br/>{< br/> Dijkstra (); <br/> cout <"output the final Dist [I] value.-1 indicates the Source Vertex" <Endl; <br/> for (int K = 1; k <= N; k ++) <br/> {<br/> If (Dist [k] = maxnum) <br/> cout <-1; <br/> else <br/> cout <Dist [k]; <br/> If (k = N) <br/> cout <Endl; // final output line feed <br/> else <br/> cout <""; // if it does not reach the end, it is separated by null characters <br/>}< br/> system ("pause"); <br/> return 0; <br/>}< br/>/* <br/> Number of Input Points: <br/> 5 <br/> enter the right of the adjacent matrix: <br/> 0 4 2 0 0 <br/> 0 0 3 2 3 <br/> 0 1 0 4 5 <br/> 0 0 0 0 0 <br/> 0 0 0 1 0 <br/> Dist [I] During output <br/> 1000000 4 2 1000000 1000000 <br/> 1000000 3 2 6 7 <br/> 1000000 3 2 5 6 <br/> 1000000 3 2 5 6 <br/> 1000000 3 2 5 6 <br/> output the final Dist [I] value, -1 indicates the source point <br/>-1 3 2 5 6 <br/> press any key to continue... <br/> */

The Dijkstra algorithm calculates the single-source shortest path, which is equivalent to finding the shortest path of all other points that can be obtained from S.

Therefore, perform the following steps:

1. Dist [I] is used to mark the shortest path from S to I. The Initialization is only Dist [s] = 0, and other Dist [I] = maxweitht, indicating that the weight is infinite.

2. Because the source point has been found, you also need to traverse the remaining V-1 points to find the shortest path.

3. From Dist [I], find that the I point is not accessed and the minimum value of DIST is used as the next vertex minnode.

4. if the value of DIST [minnode] plus the value on the edge of the reachable vertex J is smaller than the value of DIST [J], the DIST [J] is updated, indicating that the pre-order node of J is minnode.

 

 

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.