Classical Algorithm Research Series: Part 2: A thorough understanding of Dijkstra Algorithm

Source: Internet
Author: User

Author: July February 13, 2011.
Reference code: introduction to algorithms, Second Edition.
---------------------------------------

To learn what Dijkstra algorithm is, see: html "> http://www.bkjia.com/kf/201104/87374.html


This article begins with the single-source shortest path problem, and then describes the Bellman-Ford algorithm,
Describe every step of the Dijkstra Algorithm in detail, and teach you to thoroughly understand this Dijkstra algorithm.

I. Single-Source Shortest Path
We know that the single-source shortest path is a known graph G = (V, E). We need to find the shortest path from a certain Source Vertex s <-V to each v <-V.
To put it simply, find a fixed point s in graph G, and start with s. It is required to find the shortest distance or path between s and the other points in graph G.

The single-source shortest path has the following variants:
I. Single-end shortest path problem:
The shortest path from each vertex v to the specified destination t. That is, the relative problem of the single-source shortest path.
II. Single-pair vertex shortest path problem:
Given the vertices u and v, find a shortest path from u to v.

III. shortest path problem between each pair of vertices:
Find the shortest path from u to v for any two vertices u and v.
The simplest idea is to use each vertex as the source point and run a single source algorithm to solve this problem.
Of course, there are still better ways to explain in this BOlG later.

 

2. Bellman-Ford Algorithm
1. Loop Problems
A shortest path cannot contain a negative weight loop or a positive weight loop.
Some shortest path algorithms, such as Dijkstra, require that the weights of all edges in the graph be non-negative, such as on Highway maps, find a shortest path from the specified point s to the target vertex v.

2. Bellman-Ford Algorithm
The Bellman-Ford algorithm allows the input graph to have a negative weight edge, as long as there is no negative weight loop reachable from the source point.
In short, the negative weight edge can exist in the figure. However, this negative weight edge does not constitute a negative weight loop and does not affect the formation of the loop.
Besides, the Bellman-Ford algorithm itself is used to determine whether a negative weight loop is reachable from the source point in the graph,
If a negative weight loop exists, the algorithm returns FALSE. If not, TRUE is returned.

Description of the Bellman-Ford Algorithm
BELLMAN-FORD (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s) // INITIALIZE each vertex, O (V)
2 for I limit 1 to | V [G] |-1
3 do for each edge (u, v) ε E [G]
4 do RELAX (u, v, w) // For each vertex (V-1), both use relaxation technique O (E), counted as O (v-1) * E ))
5 for each edge (u, v) ε E [G]
6 do if d [v]> d [u] + w (u, v)
7 then return FALSE // check each edge in the graph to determine whether it contains a negative weight loop,
// If d [v]> d [u] + w (u, v), it indicates that it contains, and FALSE is returned,
8 return TRUE // returns TRUE if the negative weight loop is not included

The time complexity of the Bellman-Ford algorithm, which can be O (V * E ).

3. about whether the graph contains a negative weight loop:
According to the theorem, we assume that u is the father or parent of v.
When G (V, E) is a directed graph or undirected graph (and does not contain any negative weight loop), s <-V, s is any vertex of G, then any side (u, v) <-V, has
D [s, v] <= d [s, u] + 1
For detailed proof of this theorem, refer to the proof of the theorem 22nd in Chapter 22.1 In the introduction to algorithms.
Or, the correctness of the Bellman-Ford algorithm can be demonstrated through triangular inequality in chapter 1, and the deformation of the above theorem can be obtained.

That is, if graph G does not contain a negative weight loop
D [v] = $ (s, u)
<= $ (S, u) + w (u, v) // according to the Triangle Inequality
= D [u] + w [u, v]
Therefore, in the diagram that does not contain a negative weight loop, we can obtain d [v] <= d [u] + w (u, v ).

As a result, it is hard to understand that in the aforementioned Bellman-Ford algorithm,
If d [v]> d [u] + w (u, v), => contains a negative weight loop, return FASLE
Else if => does not contain a negative weight loop. TRUE is returned.

Okay, let's get started with the Dijkstra algorithm.

 

Iii. In-depth analysis of Dijkstra Algorithm
I. Introduction to relaxation technology RELAX
The Dijkstra algorithm uses the relaxation technique and sets a property d [v] for each vertex V <-v to describe the upper bound of the weight on the shortest path from the Source Vertex s to v,
It is called the Shortest Path estimation.

First, we must use the O (V) Time to estimate the shortest path and initialize the precursor.
INITIALIZE-SINGLE-SOURCE (G, s)
1 for each vertex v ε V [G]
2 do d [v] ← ∞
3 π [v] rjnil // O (V)
4 d [s] 0

RELAX (u, v, w)
1 if d [v]> d [u] + w (u, v)
2 then d [v] mongod [u] + w (u, v)
3 π [v] ← u // O (E)
Graph.

II. Dijkstra Algorithm
This Dijkstra algorithm consists of three steps,
INSERT (3rd rows), EXTRACT-MIN (5th rows), and DECREASE-KEY (8th rows of RELAX, the operation that calls this reduce keyword ).

DIJKSTRA (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s) // INITIALIZE each vertex, O (V)
2 S Ø
3 Q 1_v [G] // INSERT, O (1)
4 while Q =ø
5 do u swap EXTRACT-MIN (Q) // simple O (V * V); binary fork/item HEAP, and FIB-HEAP, then both are O (V * lgV ).
6 S ← S limit {u}
7 for each vertex v ε Adj [u]
8 do RELAX (u, v, w) // simple method: O (E), binary cross/item HEAP, E * O (lgV), FIB-HEAP, E * O (1 ).

 

Iv. Run Time of Dijkstra Algorithm
Before continuing to elaborate, we must first declare a problem, DIJKSTRA (G, w, s) algorithm in the 5th rows, EXTRACT-MIN (Q), the specific implementation of the Minimum priority queue.
The running time of Dijkstra algorithm depends on the specific implementation of the smallest priority queue.

Three implementation methods of the minimum priority queue are as follows:
1. Use the vertex from 1 to | V | to compile the number, and simply store each d [v] into the corresponding v entry in an array,
As shown in DIJKSTRA (G, w, s), the running time of the Dijkstra algorithm is O (V ^ 2 + E ).

2. If it is a binary/item heap to achieve the minimum priority queue, the EXTRACT-MIN (Q) Run Time is O (V * lgV ),
Therefore, the running time of the Dijkstra algorithm is O (V * lgV + E * lgV ),
If all vertices are reachable from the source, O (V + E) * lgV) = O (E * lgV ).
When a sparse graph is used, E = O (V ^ 2/lgV). The running time of this Dijkstra algorithm is O (V ^ 2 ).

3, using the Fibonacci heap to achieve the minimum priority queue, the EXTRACT-MIN (Q) Run Time is O (V * lgV ),
Therefore, the running time of this Dijkstra algorithm is O (V * lgV + E ).

To sum up, the three implementation methods of this minimum priority queue are compared as follows:
EXTRACT-MIN + RELAX
I. Simple Method: O (V * V + E * 1)
II. Binary cross/item heap: O (V * lgV + | E | * lgV)
Source point reachable: O (E * lgV)
Sparse graph with E = o (V ^ 2/lgV ),
=> O (V ^ 2)
III. Fibonacci heap: O (V * lgV + E)

When | V | <| E |, DIJKSTRA (G, w, s) + FIB-HEAP-EXTRACT-MIN (Q), that is, the Fibonacci heap to achieve the minimum priority queue,
The advantage is shown.


V. Dijkstra algorithm + FIB-HEAP-EXTRACT-MIN (H), Fibonacci heap to achieve minimum priority queue
From the above content, we know that using the Fibonacci heap to implement the Minimum priority queue can increase the running time to O (VlgV + E ).
| V | A EXTRACT-MIN operation, the cost of each split is O (lgV), | E | a DECREASE-KEY operation for each split time is O (1 ).

Next, we will focus on the minimal priority queue operation in DIJKSTRA (G, w, s.

As we know, the DIJKSTRA algorithm includes the following three steps:
INSERT (3rd rows), EXTRACT-MIN (5th rows), and DECREASE-KEY (8th rows of RELAX ).

First, the Dijkstra algorithm + FIB-HEAP-EXTRACT-MIN (H) algorithm is given directly:
DIJKSTRA (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s)
2 S Ø
3 Q 1_v [G] // 3rd rows, INSERT operation, O (1)
4 while Q =ø
5 do u done EXTRACT-MIN (Q) // 5th rows, EXTRACT-MIN operation, V * lgV
6 S ← S limit {u}
7 for each vertex v ε Adj [u]
8 &

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.