Classical Algorithm Research Series: Second, continue Dijkstra algorithm + Step-by-Step c Implementation of the fibonacci heap

Source: Internet
Author: User

Author: JULY, March 18, 2011
Source: http://blog.csdn.net/v_JULY_v
----------------------------------

 

Introduction:
To consider a problem,
Six Points on the plane, A, B, C, D, E, F. It is assumed that the distance between some points is known,
Now, the shortest distance from A to the other five points, B, C, D, E, and F is required.

As shown in:

After that, we can easily get the shortest distance of A-> B, C, D, E, F:

Destination path shortest distance
A => A, A-> A 0
A => B, A-> C-> B 3 + 2 = 5
A => C, A-> C 3
A => D, A-> C-> D 3 + 3 = 6
A => E, A-> C-> E 3 + 4 = 7
A => F, A-> C-> D-> F 3 + 3 + 3 = 9

I think, if you have just entered A blank question, you need to answer A-> B, C, D, E, F the shortest distance of each point,
A primary school student can fill out the information within a few minutes.

Of course our problem is not that simple. The above is just a concrete example.
In fact, many problems, such as the problem of finding the shortest path of a graph, need to use the above method to constantly compare and find the shortest path, it is the application of Dijkstra algorithm. Of course, there are BFS algorithms and more efficient A * search algorithms.

A * the search algorithm has been described in detail in this BLOG. In this article, we use the fibonacci heap to implement the Dijkstra algorithm.
That is, Dijkstra + fibonacci heap c implementation.

After I thought about an algorithm thoroughly, I had to write code to implement it so that I could really master it. In this BLOG, I have written 18 articles and 11 algorithms in the classic Algorithm Research Series. Therefore, there are more than 10 algorithms to be implemented.


Code style
To implement an algorithm, you must first understand the principle of this algorithm. After understanding the principle of this algorithm, you must write code for implementation.
Before opening the compiler, I first searched the internet for "Dijkstra algorithm + fibonacci heap implementation ".

I found that no c code has been implemented by the Dijkstra + fibonacci heap on the internet, and I skipped the following types of code:

1. No comments (not readable ).
2. No typographical layout (uncomfortable ).
3. Complicated redundancy (look at it with annoyance ).

 

Implement Dijkstra algorithm in the fibonacci heap

OK. Let's get started with the subject. Next, let's use the fibonacci heap to implement the Dijkstra algorithm step by step.
As mentioned above, to implement an algorithm, we must first clarify its principles and ideas, understand the principles of an algorithm, and know what the purpose of the algorithm is, that is, what is this algorithm used?

From the preceding example, we can conclude that the Dijkstra algorithm is used to solve the shortest path from a point to another point.
We always need to find the shortest distance from the source point to each target point. If a new point is found during the path searching process, it is found that the path can be shortened when the source point reaches the path of the Previous destination point through the newly discovered point, so we must update this Shortest Path in time.

OK. For example, if we first find A path, A-> B, the shortest distance of this path is 6, and then find the C point, if the path A-> C-> B is located, the shortest distance of A-> B is 5, which is smaller than the shortest distance 6 previously found, the shortest distance from A to B is 5, and the shortest path is A-> C-> B.

Okay. Now that we understand what this algorithm is for, let's try to write it with pseudo code (some people may say, no, I haven't understood anything yet, I have to write code. Well, don't you have materials at hand? If you have to do all your work on your own, it's a huge project. : D .).

First, let's look at the pseudo code of the Dijkstra algorithm from the introduction to algorithms as follows:

DIJKSTRA (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s) // 1. INITIALIZE the node.
2 S Ø
3 Q 1_v [G] // 2. Insert a node
4 while Q =ø
5 do u distinct EXTRACT-MIN (Q) // 3. Extract the minimum point from the smallest queue
6 S ← S limit {u}
7 for each vertex v ε Adj [u]
8 do RELAX (u, v, w) // 4. Relaxation operation.

After all, there is a lot of work to do with the code that can be compiled and run on the machine.
First, let's take a look at the above pseudo code. We can see that basically, this Dijkstra algorithm is mainly divided into the following four steps:

1. initialize node work
2. Insert a node
3. Extract the smallest vertex from the smallest queue
4. Relaxation operation.

OK. Because 2nd operations involve the Fibonacci heap, it is a little more complicated. Let's analyze 1st, 2, and 4 Operations first:

1. The O (V) time is used 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

Based on the above pseudo code, it is not difficult to write the following code:

Void init_single_source (Graph * G, int s)
{
For (int I = 0; I <G-> n; I ++ ){
D [I] = INF;
Pre [I] =-1;
}
D [s] = 0;
}

2. Insert a node to the queue

2 S Ø
3 Q 1_v [G] // 2. Insert a node

Code:
For (I = 0; I <G-> n; I ++)
S [I] = 0;

4. Relaxation operation.
First, you must understand what is a relaxation operation:
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.
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)

Similarly, it is not difficult to write the following code:
Void relax (int u, int v, Graph * G)
{
If (d [v]> d [u] + G-> w [u] [v])
{
D [v] = d [u] + G-> w [u] [v]; // update the shortest path.
Pre [v] = u; // The parent node whose u is v
}
}

Let's explain the above relax code, where u is the parent node of v. When we find that its parent node d [u] is added with the Path Distance G-> w [u] [v], the shortest distance from the child node to the source node d [v] must be updated.
Note that the original path from A to B is A-> B. Now, when A reaches B after C, this path is shorter than A-> B. Of course, you must update the shortest path from A to B, that is, A-> C-> B, C is the parent node of B (so I believe you are clear. : D .).
That is, A => B <= A-> C-> B.

OK, 1st, 2, and 4 operation steps. We have all written code for implementation. Then, let's write the code for 3rd operations: 3. From the smallest queue, extract the smallest vertex.

As you can see, we need to construct a minimum priority queue. What is used to construct the minimum priority queue column? By the way, heap. Which of the following is the best heap and the most efficient.

Why? OK. See the comparison of the three implementation methods of the minimum priority queue:

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 ),

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.