Classical Algorithm Research Series: II. Dijkstra Algorithm

Source: Internet
Author: User

July November January 2011

======================================

This article mainly references: Introduction to algorithm version 2 and Wikipedia.

Sorry for the poor writing.
This article is a series of articles on classical algorithms, which are permanently inspected, updated, and maintained.
July, updated on April 9, February 10, 2011.
1. Dijkstra algorithm Introduction

Dijkstra algorithm, also known as Dijkstra ),
The algorithm solves the shortest path from a single source point to another vertex in the directed graph.
For example,
If the vertex in the figure represents the city, and the weight on the edge represents the distance between cities,
Dijkstra algorithm can be used to find the shortest path between two cities.

 

Ii. Dijkstra Algorithm Implementation

The input of the Dijkstra algorithm contains a weighted directed graph G and A Source Vertex S in G.
We use V to represent the set of all vertices in G and E to represent the set of all edges in G.
(U, v) indicates that a path is connected from vertex u to vertex v, And the edge weight is defined by the weight function w: E → [0, ∞.

Therefore, w (u, v) is the non-negative cost value (cost) from vertex u to vertex v, And the edge cost can be imagined as the distance between two vertices.
The cost value of the path between any two points is the total cost value of all edges in the path.

It is known that vertices s and t exist in V. Dijkstra algorithm can find the lowest cost path (for example, the shortest path) from s to t ).
This algorithm can also find the shortest path from vertex s to any other vertex in a graph.

Okay. Let's take a look at the specific implementation of this algorithm:

Dijkstra Algorithm Implementation 1 (Wikipedia ):

U: = Extract_Min (Q) searches for vertex u with the smallest d [u] value in vertex set Q. This vertex is deleted from set Q and returned to the user.

1 function Dijkstra (G, w, s)
2 for each vertex v in V [G] // Initialization
3 d [v]: = infinity
4 previous [v]: = undefined
5 d [s]: = 0
6 S: = empty set
7 Q: = set of all vertices
8 while Q is not an empty set // Dijkstra algorithm master
9 u: = Extract_Min (Q)
10 S: = S union {u}
11 for each edge (u, v) outgoing from u
12 if d [v]> d [u] + w (u, v) // expanded edge (u, v)
13 d [v]: = d [u] + w (u, v)
14 previous [v]: = u

If we only search for a shortest path between s and t, we can add the condition in row 9th to terminate the program if u = t is met.
Now we can trace the shortest path from s to t Through iteration.

1 s: = empty sequence
2 u: = t
3 while defined u
4 insert u to the beginning of S
5 u: = previous [u]
Now the sequence S is the vertex set of the shortest path from s to t.

 

Dijkstra Algorithm Implementation 2 (Introduction to algorithms ):

DIJKSTRA (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s)
2 S Ø
3 Q ← V [G] // V * O (1)
4 while Q =ø
5 do u done EXTRACT-MIN (Q) // EXTRACT-MIN, V * O (V), V * O (lgV)
6 S ← S limit {u}
7 for each vertex v ε Adj [u]
8 do RELAX (u, v, w) // relaxation technology, E * O (1), E * O (lgV ).

 

Because the Dijkstra algorithm always selects the "lightest" or "nearest" vertex in the V-S and inserts it into the set S, we say it uses a greedy policy.

(Greedy algorithms will be described in detail in future blog posts ).

========================================================== ===

Updated on April 9, February 9, 2011:
The initial time complexity of this Dijkstra algorithm is O (V * V + E). If the source point is reachable, O (V * lgV + E * lgV) => O (E * lgV)
When it is a sparse graph, E = V * V/lgV, the time complexity of the algorithm can be O (V ^ 2 ).

However, we know that if the Fibonacci heap implements a priority queue, the algorithm time complexity is O (V * lgV + E ).

 

 

Iii. Dijkstra Algorithm Execution speed

We can use a large O symbol to express the running time of Dijkstra algorithm as a function of the number of edges m and the number of vertices n. The simplest way to implement the Dijkstra algorithm is to use a linked list or array to store the Q set of all vertices,
Therefore, the Extract-Min (Q) operation requires linear search of all elements in Q.
In this case, the algorithm runs at O (E ^ 2 ).

For a sparse graph with fewer edge numbers than E ^ 2, we can use an adjacent table to implement the decocort algorithm more effectively.
At the same time, a binary heap or Fibonacci heap needs to be used as a priority queue to find the smallest vertex (Extract-Min ).

When a binary heap is used, the algorithm takes O (V + E) logE ),
The Fibonacci heap can slightly improve the performance and make the algorithm run at O (V + ElogE ). (This was amended on April 9, January 16 .)

 
The Open Shortest Path First (OSPF) algorithm is a specific implementation of the Dickus Algorithm in network routing.
Unlike the Dijkstra algorithm, the Bellman-Ford algorithm can be used for graphs with negative weight edges, this algorithm can be used as long as the figure does not have a loop with a negative total cost and s reachable from the source point (if such a loop exists, the shortest path does not exist, because the total cost can be reduced without limit after multiple loops ).

One of the most famous problems related to the shortest path problem is the Traveling salesman problem (Traveling salesman problem), which requires finding the shortest path that exactly goes through all the punctuation points and eventually returns to the origin.
However, this problem is NP-complete. In other words, unlike the shortest path problem, the traveling salesman problem is unlikely to have a polynomial time solution.
If the known information can be used to estimate the distance from A certain point to the target point, you can use the * search algorithm to reduce the search range of the shortest path.

========================================================== =

Updated on April 9, February 9, 2011:
Comparison of time complexity of BFS, DFS, Kruskal, Prim, and Dijkstra algorithms:
Generally, we know that the time complexity of BFS and DFS algorithms is O (V + E ),
The time complexity of the Minimum Spanning Tree Algorithm Kruskal and Prim is O (E * lgV ).

If the Prim algorithm is implemented using the Fibonacci heap, the time complexity of the algorithm is O (E + V * lgV). When | V |<<| E |, E + V * lgV is a major improvement.
// | V | <| E |, => O (E + V * lgV) <O (E * lgV), right. : D

Dijkstra algorithm. When the Fibonacci heap is used as the priority queue, the algorithm time complexity is O (V * lgV + E ).
// You can see that the time complexity of the algorithm is the same as that of the Prim algorithm when the Fibonacci heap is used.

So we can say that BFS, Prime, and Dijkstra algorithms have similarities. From the time complexity comparison of each algorithm, we can look at one of them.

 

Iv. Graphic parsing Dijkstra Algorithm

OK. After a bit of complicated information, you are not fully aware of this algorithm.
It doesn't matter. Let's make a picture. Allow me to elaborate on the concept of this algorithm,

Dijkstra is a typical shortest path algorithm used to calculate the shortest path from one node to all other nodes.
However, this parameter is applicable to non-negative weight edges.

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.]

OK, please refer:

For example, if A is set as the source point, find the shortest path from A to all other one-to-one 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)

Dijkstra undirected graph

 

 

The Algorithm Execution steps are as follows:

Related Article

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.