Dijkstra algorithm of Shortest path (Dijkstra)

Source: Internet
Author: User

1. Dijkstra (Dijkstra) algorithm Introduction

Dijkstra algorithm is proposed by E.w.dijkstra in 1959, also called Dijkstra algorithm, it applies the greedy algorithm pattern,

is currently recognized as the best way to solve the shortest path. The algorithm solves the shortest possible direction from a single source point to another vertex in a graph.

The main feature of a path problem is that the next vertex selected at each iteration is the closest vertex to the source point outside the marker point. But

Because the Dijkstra algorithm mainly calculates the shortest path from the source point to all other points, the algorithm is less efficient.

Basic process of 2.dijkstra algorithm

Assume that each node in the road network has a label that is the shortest path length from the starting point S to the point T; the previous point of the T in the shortest path from S to T. The basic process for solving the shortest path algorithm from the starting point S to T is:

1. Initialize. The starting point is set to:

Mark the origin point S, remember k = s, all other points are set to unmarked.

2. Verify the distance from all marked points K to the unmarked point J of the other direct connections, and set:


3. Select the next point. Select the smallest point I from all unmarked points, and point I is selected as a point in the shortest path and is set to marked.

4. Find the previous point of point I. Find the point directly connected to point I from the set of points that have been marked, and mark it as.

5. Mark point I. If all points are marked, the algorithm ends. Otherwise, remember k = i, go to 2 to continue.

As can be seen from the steps of the above algorithm: The key part of the Dijkstra algorithm is to constantly find the point of distance from the point of origin, and add the pity dorado to the point collection of the marker, and update the shortest estimated distance from the remaining points in the unmarked point set to the starting point [Z1].

Taking an Dijkstra graph with weighted value as an example, the shortest path from source point A to target F is analyzed by using the algorithm.


1. A matrix w with a power value represents a weighted undirected graph containing n nodes, representing the weight of the arc segment, if the node is not connected to the node, then the adjacency matrix with the weighted graph is shown. Set A as the source point, and G as the destination, representing the shortest path length from Node A to the other nodes in the graph. Sets the initial value of the node collection that represents the tag.


2. It is a terminating node on the shortest path from point A, so;


3. Modify the length value of the shortest path between the starting node A and the set, if D (j) +w (J,k) < D (k), then D (k) = d (j) + W (j,k);

4. Repeat steps 2, 3 for N-1 times, and finally get the shortest path from start node A to other nodes, and arrange the length of the path in ascending order.




The flowchart of the 3.dijkstra algorithm is as follows:



4. The code snippet is as follows:

Dijkstra (Dijkstra) algorithm//Xin Yang # define Maxvex 9#define INFINITY 65535typedef int patharc[maxvex];//used to store array typedef int in Shortest path shortpathtable[maxvex];//is used to store the weights and/*dijkstra algorithms for the shortest path to each point, the V0 vertex of the graph G to the remaining vertex v shortest path p[v] and the value of the weighted length d[v] *p[v] is the precursor vertex subscript, d[v] Represents the shortest path length of V0 to V and * */void Dijkstra (mgraph G, int V0, Patharc *p, shortpathtable *d) {int V, W, K, Min;int final[maxvex];//for marking Record/* Initialize */for (v = 0; v < g.numvertexts; v++) {final[v] = 0; (*d) [v] = 0; (*p) [v] = 0;} (*d) [V0] = 0;final[v0] = 1;//no longer searches for V0 after V0 is marked 1, the next line loops from V1 start//loop, each time v0 to a v vertex of the shortest path */for (v = 1; v < g.numvertexts; v++) {min = in Finity;for (w = 0; w < g.numvertexes; v++) {if (!final[w] && (*D) [W] < min) {k = W;min = (*d) [w];//w vertices are closer to v0 vertices}}f  Inal[k] = 1;//finds a vertex, marks it as one, and does not search for the vertex for (w = 0; w < g.numvertexes; w++) {if (!final[w] && (min + g.arc[k][w) < (*d) [W])) {(*D) [W] = min + g.arc[k][w];(*p) [W] = k;}}}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dijkstra algorithm of Shortest path (Dijkstra)

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.