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

Source: Internet
Author: User

ClassicAlgorithmStudy Series: continued and thorough understanding of Dijkstra Algorithm 

Author: July February 13, 2011.
ReferenceCode: Introduction to algorithms, Second Edition.
---------------------------------------

For more information about the Dijkstra algorithm, see:
Classical Algorithm Research Series: II. Dijkstra Algorithm
Http://blog.csdn.net/v_JULY_v/archive/2010/12/24/6096981.aspx

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), the relaxation technique O (e) is used, 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 it does not contain a negative weight Loop

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 ← V [g] // insert, O (1)
4 While Q =ø
5 do u EXTRACT-MIN (q) // simple O (V * V); binary cross/item heap, and FIB-heap are both 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 isO (V ^ 2 + E ).

2,If the binary/item heap achieves the smallest priority queue, the EXTRACT-MIN (q) runs for 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 it is a sparse graph, E = O (V ^ 2/LGV), the running time of this Dijkstra algorithm isO (V ^ 2 ).

3,UseFibonacci heapTo achieve the minimum priority queue, the EXTRACT-MIN (q) Run Time is O (V * LGV ),
Therefore, the running time of this Dijkstra algorithm isO (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 ).

the Dijkstra algorithm + FIB-HEAP-EXTRACT-MIN (h) algorithm is given first:
Dijkstra (G, W, S)
1 initialize-single-source (G, S)
2 s faster ø
3 Q faster V [g]
// 3rd rows, insert operation , O (1)
4 While Q =ø
5 do u solid EXTRACT-MIN (q) // 5th rows, EXTRACT-MIN operation , V * LGV
6 s ← s limit {u}
7 for each vertex v ε adj [u]
8 do relax (u, v, W) // 8th rows, relax operation , E * O (1)

FIB-HEAP-EXTRACT-MIN) // The price is O (LGV)
1 Z Min [H]
2 If Z =nil
3 then for each child X of Z
4 do add X to the root list of H
5 p [x] running Nil
6 remove Z from the root list of H
7 if z = right [Z]
8 then Min [H] running Nil
9 else Min [H] Unlock right [Z]
10 bytes lidate (h)
11 N [H] ← N [H]-1
12 Return Z

OK. Next, detailed steps are described.

Insert of Row 3:
Fib-heap-insert (H, X)// Spread the price, O (1 ).
1 degree [x] limit 0
2 P [x] running Nil
3 child [x] ← Nil
4 left [x] returns x
5 right [x] limit x
6 mark [x] limit false
7 concatenate the root list containing X with root list H
8 if Min [H] = nil or key [x] <key [Min [H]
9 then Min [H] limit x
10 N [H] ← N [H] + 1

5th rows of EXTRACT-MIN operations:
FIB-HEAP-EXTRACT-MIN) // The price is O (LGV)
1 Z Min [H]
2 If Z =nil
3 then for each child X of Z
4 do add X to the root list of H
5 p [x] running Nil
6 remove Z from the root list of H
7 if z = right [Z]
8 then Min [H] running Nil
9 else Min [H] Unlock right [Z]
10 bytes lidate (h)// The consolidate algorithm is provided below.
11 N [H] ← N [H]-1
12 Return Z

YesThe FIB-HEAP-EXTRACT-MIN Process:


Consolidate (h)
1 For I limit 0 to D (N [H])
2 do a [I] commit Nil
3 For each node W in the root list of H
4 do X W
5 d Required degree [x] // Number of children
6 While a [d] =nil
7 Do y prepare a [d]
8 if key [x]> key [y]
9 then exchange x <-> Y
10 fib-heap-Link (H, Y, X) // Below.
11 A [d] running Nil
12 d usdd + 1
13 A [d] running X
14 min [H] running Nil
15 For I limit 0 to D (N [H])
16 do if a [I] =nil
17 then add a [I] to the root list of H
18 if Min [H] = nil or key [A [I] <key [Min [H]
19 then Min [H] When a [I]

Fib-heap-Link (H, Y, x) // link to X.
1 remove y from the root list of H
2 make y a child of X, incrementing degree [x]
3 mark [y] limit false

The Relax operation of row 8th has been provided:
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)

In general, in Dijkstra algorithm, decrease-key calls much more than the call of the EXTRACT-MIN,
Therefore, without increasing the spread time of the EXTRACT-MIN operation, try to reduce the spread time of the decrease-key operation, can achieve faster than the binary heap.

The following is a comparison of the time complexity of operations on a binary heap, a binary heap, and a Fibonacci heap:

Operate a binary heap (worst) Fibonacci heap (equal pool)

__________________________________
Make-heap Merge (1) Merge (1) Merge (1)
Insert into (LG n) o (lg n) Then (1)
Minimum random (1) O (lg n) random (1)
EXTRACT-MIN Branch (lg n) Branch (LG n) o (lg n)
Union terminate (n) o (lg n) terminate (1)
Decrease-key pair (lg n) percent (1)
Delete multiple (lg n) random (LG n) o (lg n)

Fibonacci heap,In the future, we will go further into depth and detail in this blog.
At the same time, this article will continue to deepen and expand.
.

 

I am July and have allArticleContent and materials are copyrighted.
You must indicate the author and the source, and notify me of the reprinting. Thank you.

July and July February 13, 2011.

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.