[Introduction to algorithms] Dijkstra Algorithm

Source: Internet
Author: User

Algorithm idea:

Dijkstra is a typical shortest path routing algorithm used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses Many computing nodes.

Dijkstra algorithm is a representative short-circuit algorithm. It has been described in detail in many professional courses, such as data structure, graph theory, and operational research.

The basic idea is to set the vertex set S and constantly make greedy choices to expand the set. A vertex belongs to the set S and is known only when the shortest path length from the source to the vertex is known.

Initially, S contains only the source. If u is a vertex of G, the path from source to U and passing through only the vertex in S is called a special path from source to U, use the DIST array to record the shortest path length corresponding to each vertex. Dijkstra algorithm extracts vertex u with the shortest special path length from the V-S each time, add u to S, and make necessary modifications to the array Dist. Once S contains all vertices in V, DIST records the shortest path length between the source and all other vertices.

For example, for Directed Graphs in the following table, the Dijkstra algorithm is used to calculate the shortest path between Source Vertex 1 and other vertices.

Dijkstra algorithm iteration process:

Pseudocode:

Sample Code:

# Include <iostream> # include <fstream> using namespace STD; const int maxn = 100; const int infinite = 100000; int adj [maxn] [maxn]; int N; int extract_min (int * D, bool * visited, int v) // obtain the smallest D from the adjacent contacts {int Len = infinite; int M =-1; for (INT I = 1; I <= N; I ++) {If (! Visited [I] & D [I] <Len) {Len = d [I]; M = I ;}} return M ;} void paint_path (int * Pre, int root) {If (pre [root] = 0) {printf ("% d", root);} else {paint_path (PRE, pre [root]); printf ("--> % d", root) ;}} void relax (int * D, int * Pre, int U, int V, int W) {If (d [v]> d [u] + W) {d [v] = d [u] + W; Pre [v] = u ;}} void Dijkstra (INT s) {int I; int U; int * D; int * pre; bool * visited; D = new int [N]; pre = new int [N]; visited = new B OOl [N]; // initializefor (INT I = 1; I <= N; I ++) {d [I] = infinite; Pre [I] = 0 ;} d [s] = 0; for (I = 1; I <= N; I ++) {u = extract_min (D, visited, N ); visited [u] = true; For (Int J = 1; j <= N; j ++) {If (! Visited [J]) {relax (D, pre, U, J, adj [u] [J]) ;}}for (I = 1; I <= N; I ++) {paint_path (PRE, I); printf ("\ n") ;}// Delete [] D; // Delete [] pre; // Delete [] visited;} int main (INT argc, char * argv []) {int I, j; ifstream in ("data.txt"); In> N; for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {In> adj [I] [J] ;}} Dijkstra (1); Return 0 ;}

Test results:

References:

Http://www.wutianqi.com /? P = 1890

Http://net.pku.edu.cn /~ Course/cs101/2007/resource/intro2algorithm/book6/chap25.htm

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.