17. Problem of Shortest Path (Dijkstra), shortest path dijkstra

Source: Internet
Author: User

17. Problem of Shortest Path (Dijkstra), shortest path dijkstra

I. Basic concepts:

Find a path with the smallest Edge Weight and edge weight to reach the destination from the starting point, which is the most Short Circuit Problem of the unit.

In the short-circuit problem, a directed Weighted Graph G = (V, E) is given. The edge weight is a measurement standard of a physical object, not necessarily a distance, it can be in the form of time, money, fines, losses, or linear accumulation of any other paths.

The right of path p = (V0, V1,..., Vk) refers to the sum of the ownership values of the edge. The minimum path between u and v is defined:


Three types of Shortest Path Problems:

1. Single-source shortest path problem: Find a shortest path from each node v to a specified node u. If we reverse each edge in the image, we can turn this problem into a single-source shortest path problem.
2. Single-to-node shortest path problem: for a given node to u and v, find a path from u to v.
3. The shortest path between each pair of nodes: for each pair of nodes u and v, find the shortest path from u to v. The Floyed-Warshall algorithm can be used to solve the problem, but there are harsh conditions that do not allow negative weight loops under low time efficiency. Run the single-source algorithm once at each node as the source node to improve the timeliness.

Relaxation technology is the core of a single-source Algorithm

The so-called relaxation technique is to reduce the upper limit of the actual shortest path right of each node until the upper limit is equal to the shortest path right.

Theorem: Given a directed Weighted Graph G = (V, E), Set P = <V1, V2 ,......, Vk> is a path from node V1 to node Vk. For any I, j has I <= j <= k, set Pij = <Vi, Vi + 1 ,..., Vj> is the sub-path of P from Vi to Vj, and Pij is a shortest path from Vi to Vj.

Given a directed Weighted Graph G = (V, E) and the Source Vertex is s, all edges (u, v) in E have) <= & (s, u) + w (u, v ).


Relaxation technology:

For each node v, set an attribute d [V] to describe the upper bound of the shortest path from the Source Vertex s to v, which is called the Shortest Path Length Estimation, set f [v] to indicate the father of vertices.


Proc initiallze_single_source (G, s );
{
For each v ε V [G] do {d [v]: = ∞; f [v] = nil ;}
D [s]: = 0;
}

The process of relaxing an edge (u, v) includes testing whether the node u can be used to improve the shortest path to v, if possible, update d [v] and f [v]. A relaxation operation can reduce the Shortest Path Length Estimation d [v] and update v's father f [v].


Proc relax (u, v, w );
{
If d [v]> d [u] + w [u, v] then {d [v]: = d [u] + w [u, v]; f [v]: = u ;}
}


Ii. Dijkstra algorithm:

Dijkstra algorithm solves the shortest path problem of a directed weighted graph. The condition of this algorithm is that the weights of all edges in the graph are not negative, that is, for each edge (u, v) ε E, w (u, v)> = 0;
The Dijkstra algorithm sets a node set S. The right to the final shortest path from the source node r to the node in the Set S has been determined, that is, for all nodes v, S, with d [v] = & (r, v) and set the minimum priority queue Q, the queue contains all nodes that belong to the V-S (that is, those nodes have not yet determined the Shortest Path) and arrange each node with the D value as the keyword.
Initially, Q contains nodes other than r. The D value of these nodes is ∞. R enters the set S, d [r] = 0. The algorithm repeatedly extracts the node u, V-S with the smallest D value from Q, inserts u into the set S, and relaxes all outbound edges of u. This process continues until the Q queue is empty.



The Dijkstra algorithm can be successfully completed as long as there is no negative weight edge in the graph. If the weight of any edge is negative, the algorithm may give the wrong answer.


Procedure Dijstra (G, w, r );
{
Initiallze_single_source (G, r );
S: = #; Q: = V [G];
While Q <> # do
{
Extracts the node u with the smallest D value from the Q of the smallest priority queue;
S: = S bytes [u];
For v ε adj (u) do relax (u, v, w );
}
}


The execution speed of Dijkstra algorithm depends on the data structure of priority queue Q. Three data structures are available:
(1) Use a one-dimensional array to implement a priority queue. the time complexity is O (V * V ). It is used in dense graphs of small scale.
(2) uses a binary heap to implement priority queue Q. the time complexity is O (ElnV) and is used in sparse graphs.
(3) Use the Fibonacci heap to implement priority queues. the time complexity is optimized to O (VlnV + E ). However, the implementation of the Fibonacci heap program is quite cumbersome, and the actual running effect of the program is not ideal. It is not recommended.

The similarities and differences between Dijkstra algorithm and Prim algorithm:
Same: both are width-first searches, and priority queue Q is arranged by the distance d as the keyword. After the node leaves the queue, it must be relaxed.
Difference: distance d in the Dijkstra algorithm is the shortest path length between the node and the source point. distance d in the Prim algorithm is the shortest side length of the node connection spanning tree.


Deformation:

Find the Shortest Path
Number of solutions for the existence of multiple Shortest Paths
Request short path


Exercise:

1. codevs1041

2. poj3013

3. poj1135




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.