Image single-source shortest path algorithm

Source: Internet
Author: User

Overview
If you have a map that shows the distance between each adjacent city, from one location to another, how can you find the shortest path? The shortest-circuit algorithm solves these problems. Definition: Given a directed graph with (none), each edge has a weight of w, given a starting point S and ending point T, find the minimum path of the weight value from S to T, that is, the shortest path. The shortest path algorithm depends on one property: the shortest path between two vertices contains other shortest paths on the path. In short, the sub-path of the shortest path is the shortest path.

 


Relaxation Technology
Relaxation is essentially a greedy operation. Relaxation operation: For each vertex v, a property d [V] is set to describe the upper bound of the weight on the shortest path from the Source Vertex s to v, it is called shortest-path estimate, and parent [v] represents the frontend. Initialize pseudo code:


[Html]
INITIALIZE-SINGLE-SOURCE (G, s)
For each vertex v (-V [G]
Do
D [v] <-INF
Parent [v] <-NULL
Done
D [s] <-0

INITIALIZE-SINGLE-SOURCE (G, s)
For each vertex v (-V [G]
Do
D [v] <-INF
Parent [v] <-NULL
Done
D [s] <-0

After initialization, all v belongs to V, parent [v] = NULL, and v belongs to {V-{s }}, d [s] = 0 and d [v] = infinite. Loosen an edge (u, v). If this edge can improve the shortest path, update d [v] and parent [v]. A relaxation operation can reduce the estimated value of d [v] for the shortest path, and update the parent [v] in the forward direction of v. the following pseudo-code performs a one-step relaxation operation on the edge (u, v:


[Html]
RELAX (u, v, w)
If d [v]> d [u] + w (u, v)
Then
D [v] <-d [u] + w (u, v)
Parent [v] <-u
Fi

RELAX (u, v, w)
If d [v]> d [u] + w (u, v)
Then
D [v] <-d [u] + w (u, v)
Parent [v] <-u
Fi

 

In the example on the left, the estimated value of the shortest path is reduced. In the example on the right, the estimated value of the shortest path remains unchanged. Update d [v] and parent [v] When a closer path is found between v and u.


Dijkstra Algorithm
To solve the shortest path problem, the most typical Algorithm is Dijkstra, which is a single-source shortest path Algorithm. Its core idea is Greedy Algorithm ). The Dijkstra algorithm requires that the weights of all edges be non-negative.


Algorithm IDEA
Set G = (V, E) to a directed graph with weights. Divide vertex set V in the graph into two groups. The first group is the vertex set with the obtained Shortest Path (represented by S, initially, there is only one source point in S. After finding a shortest path, add it to the set S until all vertices are added to S, and the algorithm ends ); the second group is the vertex set of the Other unconfirmed Shortest Paths (as shown in the utable), and adds the second group of vertices to S in the ascending order of shortest path length. The length of the shortest path from the Source Vertex v to the vertex S is not greater than the shortest path length of any vertex from The Source Vertex v to the U. In addition, each vertex corresponds to a distance. The distance between vertices in S is the shortest path length from v to this vertex, and the distance between vertices in U, from v to this vertex, only the vertex in S is the current shortest path length of the intermediate vertex.


Pseudocode
[Html]
Dijkstra (G, w, s)
INITIALIZE-SINGLE-SOURCE (G, s)
S <-empty set
Q <-V [G]
 
While Q! = Empty set
Do
U EXTRACT-MIN (Q)
S <-S & {u}
For each vertex v (-Adj [u]
Do
RELAX (u, v, w)
Done
Done

Dijkstra (G, w, s)
INITIALIZE-SINGLE-SOURCE (G, s)
S <-empty set
Q <-V [G]

While Q! = Empty set
Do
U EXTRACT-MIN (Q)
S <-S & {u}
For each vertex v (-Adj [u]
Do
RELAX (u, v, w)
Done
Done

Dijkstra algorithm time is mainly consumed in finding the smallest weight of the edge, and relax all the remaining sides, so EXTRACT-MIN (Q) this step, a better way is to use the priority queue, the priority queue can use a binary heap.

 


Algorithm Description
Assume that arcs is used to represent a directed graph with weights. arcs [I] [j] represents the weights on the arc <vi, vj>. If <vi, vj> does not exist, set arcs [I] [j] To infinity (the maximum allowed value can be used on the computer ). S is the set of the end points of the shortest path from v. Its initial state is empty. Then, starting from v, the initial value of the shortest path length that other vertices (endpoints) vi can reach is D [I] = arcs [src] [I] vi (-V ).
Select vj so that D [j] = Min {D [I] | vi (-V-S}, and vj is the end point of a obtained Shortest Path starting from v. Make S = S & {j}
Modify the shortest path length from v to any vertex of V-S. If D [j] + arcs [j] [k] <D [k], modify D [k] = D [j] + arcs [j] [k]
Repeat operations 2 and 3 for n-1 times. The shortest path length from v to the other vertices in the graph is a sequence that increases progressively according to the path length.


Implementation Code (C language)
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
 
// The maximum value of the graph Vertex
# Define NODE 102.
 
// Maximum Edge Weight
# Deprecision MAX 10002
 
// Storage map of the adjacent matrix
Int map [NODE] [NODE];
 
// Record the shortest path length from src to other vertices
Int dis [NODE];
 
// Mark the array to determine which src to which vertex has the shortest path obtained
Int mark [NODE];
 
/**
* Find the shortest path from src to other vertices in the graph map using Dijkstra Algorithm
* Src is a single-source vertex, and n is the number of vertices in the graph.
*/
Void dijkstra (int src, int n)
{

Int I, j, min, k, tmp;
 
// Initialization
For (I = 0; I <n; I ++ ){
Dis [I] = map [src] [I];
Mark [I] = 0;
}
 
Dis [src] = 0;
Mark [src] = 1;
 
// N n-1 main cycle. Each cycle obtains the shortest path from src to a vertex v.
For (I = 1; I <n; I ++ ){
Min = MAX;
K = src;
For (j = 0; j <n; j ++ ){
If (! Mark [j] & dis [j] <min ){
K = j;
Min = dis [j];
}
}
Mark [k] = 1;
 
// Update the shortest path of src to other vertices
For (j = 0; j <n; j ++ ){
Tmp = map [k] [j] + dis [k];
If (tmp <dis [j] & mark [j] = 0 ){
Dis [j] = tmp;
}
}
}
}

# Include <stdio. h>
# Include <stdlib. h>

// The maximum value of the graph Vertex
# Define NODE 102.

// Maximum Edge Weight
# Deprecision MAX 10002

// Storage map of the adjacent matrix
Int map [NODE] [NODE];

// Record the shortest path length from src to other vertices
Int dis [NODE];

// Mark the array to determine which src to which vertex has the shortest path obtained
Int mark [NODE];

/**
* Find the shortest path from src to other vertices in the graph map using Dijkstra Algorithm
* Src is a single-source vertex, and n is the number of vertices in the graph.
*/
Void dijkstra (int src, int n)
{

Int I, j, min, k, tmp;

// Initialization
For (I = 0; I <n; I ++ ){
Dis [I] = map [src] [I];
Mark [I] = 0;
}

Dis [src] = 0;
Mark [src] = 1;

// N n-1 main cycle. Each cycle obtains the shortest path from src to a vertex v.
For (I = 1; I <n; I ++ ){
Min = MAX;
K = src;
For (j = 0; j <n; j ++ ){
If (! Mark [j] & dis [j] <min ){
K = j;
Min = dis [j];
}
}
Mark [k] = 1;

// Update the shortest path of src to other vertices
For (j = 0; j <n; j ++ ){
Tmp = map [k] [j] + dis [k];
If (tmp <dis [j] & mark [j] = 0 ){
Dis [j] = tmp;
}
}
}
}

 

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.