Dijkstra Algorithm for Shortest Path

Source: Internet
Author: User

1. Shortest Path Algorithm

In our daily life, if we need to travel between Area A and Area B, what we want to know most is the path from Area A to area B, the path has the shortest path. The shortest path is a classic algorithm in graph theory research. It aims to find the shortest path between two nodes in a graph (composed of nodes and paths. The specific algorithm formats include:

(1) determining the Shortest Path of the starting point: that is, finding the shortest path when the starting node is known.

(2) The problem of Shortest Path for determining the end point: opposite to the problem of determining the start point, this problem is the problem of finding the shortest path by knowing the end node. In an undirected graph, this problem is exactly the same as the problem of determining the starting point. In a directed graph, this problem is equivalent to the problem of determining the starting point when all paths are reversed.

(3) the problem of determining the Shortest Path of the start and end points: that is, finding the shortest path between the two nodes is known.

(4) Global shortest path problem: Find all shortest paths in the figure.

The algorithm used to solve the shortest path problem is called the shortest path algorithm ". The most common path algorithms include Dijkstra algorithm, a * algorithm, Bellman-Ford algorithm, Floyd-warshall algorithm, and Johnson algorithm.

This article mainly studies the single-source algorithm of Dijkstra algorithm.

2 Dijkstra Algorithm

2.1 Dijkstra Algorithm

Dijkstra is a typical Shortest Path 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.

2.2 Dijkstra algorithm ideas

The Dijkstra algorithm thought: Set G = (V, E) to a directed graph with weights, and divide the vertex set V in the graph into two groups, the first group is the vertex set with the obtained Shortest Path (represented by S. At first, there is only one source point in S. Every time a shortest path is obtained, it will be added to the set S, until all vertices are added to S, the algorithm ends.) The second group is the vertex set of the other uncertain Shortest Paths (as shown in the utable ), add the second vertex to the second vertex in the ascending order of the shortest path length. The length of the shortest path from the Source Vertex V to the vertex S is not greater than the shortest path length of any vertex from The Source Vertex V to the U. In addition, each vertex corresponds to a distance. The distance between vertices in S is the shortest path length from V to this vertex, and the distance between vertices in U, from V to this vertex, only the vertex in S is the current shortest path length of the intermediate vertex.

2.3 steps of Dijkstra Algorithm

(1) Initially, s only contains the source point, that is, the distance between S = and V is 0. U contains other vertices except v. The distance between U vertices is the weight of the edge (if V and u have edges) or (if u is not the outdegree neighbor of V ).

(2) Select a vertex K with the smallest distance from U and add K to S (the selected distance is the shortest path length from V to K ).

(3) change the distance between vertices in U based on K. If the distance from the Source Vertex V to the vertex u (u) goes through the vertex K) shorter than the original distance (without passing through vertex K), the distance value of vertex u is modified, and the distance of vertex K of the modified distance value plus the edge weight.

(4) Repeat steps (2) and (3) until all vertices are included in S.

2.4 Dijkstra algorithm example

For example, if a is set as the source point, find the shortest path from A to other vertices (B, c, d, e, f. The distance between adjacent line segments, that is, the weight value. (Note: The distance between adjacent vertices and the visual length in the graph cannot be one-to-one)

Figure 1: Dijkstra undirected graph

 

The Algorithm Execution steps are as follows:


References

[1] Huang guoyu, ye najing, data structure, Tsinghua University Press, August 2001, 1st

[2] Shortest Path, http://baike.baidu.com/view/349189.htm? Func = retitle

[3] Li chunbao, data structure tutorial, Tsinghua University Press, January 2005, 1st

Dijkstra algorithm, http://baike.baidu.com/view/7839.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.