Shortest Path-Dijkstra algorithm and Floyd algorithm

Source: Internet
Author: User

Dijkstra Algorithm

1. Definition Overview

Dijkstra is a typical single-source shortest path algorithm used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm is a representative shortest path algorithm. It is described in detail as a basic content in many professional courses, such as data structure, graph theory, and operational research. Note that this algorithm requires that no negative weight edge exists in the graph.

Problem description: In an undirected graph G = (V, E), assume that the length of E [I] for each edge is w [I]. find the shortest path from vertex V0 to other points. (Single-source shortest path)

 

2. Algorithm Description

1) algorithm idea: Set G = (V, E) to a directed graph with weights. Divide the vertex set V in the graph into two groups, the first group is the vertex set with the obtained Shortest Path (represented by S. At first, there is only one source point in S. Every time a shortest path is obtained, it will be added to the set S, until all vertices are added to S, the algorithm ends.) The second group is the vertex set of the other uncertain Shortest Paths (as shown in the utable ), add the second vertex to the second vertex in the ascending order of the 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.

2) algorithm steps:

A. Initially, S only contains the source point, that is, the distance between S = {v} and v is 0. U contains other vertices except v, that is, U = {other vertices}. If v and U vertices u have edges, <u, v> the value is normal, if u is not the outdegree neighbor of v, the <u, v> weight is ∞.

B. Select a vertex k with the smallest distance from U and add k to S (the selected distance is the shortest path length from v to k ).

C. the distance between vertices in U is modified based on k. If the distance from the Source Vertex v to the vertex u (after the vertex k) is higher than the original distance (without the vertex k) short, then modify the distance value of vertex u, the distance of the modified distance value vertex k plus the edge weight.

D. Repeat steps B and c until all vertices are included in S.

 

The animation execution process is as follows:

 

3. algorithm code implementation:

 

Const int MAXINT = 32767; const int MAXNUM = 10; int dist [MAXNUM]; int prev [MAXNUM]; int A [MAXUNM] [MAXNUM]; void Dijkstra (int v0) {bool S [MAXNUM]; // determines whether the data has been saved to this point and int n = MAXNUM; for (int I = 1; I <= n; ++ I) {dist [I] = A [v0] [I]; S [I] = false; // if (dist [I] = MAXINT) prev [I] =-1; else prev [I] = v0;} dist [v0] = 0; S [v0] = true; for (int I = 2; I <= n; I ++) {int mindist = MAXINT; int u = v0; // find the currently unused Point j's dist [j] minimum value for (int j = 1; j <= n; ++ j) if ((! S [j]) & dist [j] <mindist) {u = j; // u stores the mindist = dist, which is the smallest distance from the current adjacent contact. [j];} S [u] = true; for (int j = 1; j <= n; j ++) if ((! S [j]) & A [u] [j] <MAXINT) {if (dist [u] + A [u] [j] <dist [j]) // find the shorter path from v0 through the new u point path {dist [j] = dist [u] + A [u] [j]; // update dist prev [j] = u; // record the precursor vertex }}}}

 

4. Algorithm Instances

First, an undirected graph is provided.

The steps for finding the single-source shortest path starting with A using Dijkstra algorithm are as follows:

 

Floyd algorithm

1. Definition Overview

Floyd-Warshall Algorithm(Floyd-Warshall algorithm) is an algorithm for solving the shortest path between any two points. It can correctly handle the shortest path problem of Directed Graphs or negative weights, it is also used to calculate the transfer closure of a directed graph. The time complexity of the Floyd-Warshall algorithm is O (N3), and the space complexity is O (N2 ).

 

2. Algorithm Description

1) algorithm principle:

The Floyd algorithm is a classic dynamic planning algorithm. To describe in a popular language, our first goal is to find the shortest path from point I to Point j. From the perspective of dynamic planning, we need to re-interpret this goal (this interpretation is the essence of the most creative dynamic planning)

The shortest path from any node I to any node j is similar to two possibilities: 1 is directly from I to j, 2 is from I through several nodes k to j. Therefore, we assume that Dis (I, j) is the distance from node u to node v. For each node k, we check Dis (I, k) + Dis (k, j) <Dis (I, j) is true. If yes, it indicates that the path from I to k to j is shorter than that from I to j, we set Dis (I, j) = Dis (I, k) + Dis (k, j). In this way, when we traverse all nodes k, Dis (I, j) records the shortest distance from I to j.

2). Algorithm Description:

A. Start with any single-sided path. The distance between all two points is the edge weight. If there is no EDGE connection between the two points, the right is infinite.

B. For each pair of vertices u and v, check whether a vertex w exists to make the path from u to w and then to v shorter than the path we know. Update it.

3). Calculation of the process matrix of the Floyd algorithm-cross method

Method: two lines are calculated from the upper left corner to the lower right corner, as shown below:

Given A matrix, where matrix A is an adjacent matrix, while the matrix Path records the vertices required for the shortest Path between the u and v points

The calculation method is as follows:

The final A3 is the result.

 

3. algorithm code implementation

Typedef struct {char vertex [VertexNum]; // vertex table int edges [VertexNum] [VertexNum]; // adjacent matrix, which can be seen as edge table int n, e; // Number of vertices and edges in the graph} MGraph;
Void Floyd (MGraph g) {int A [MAXV] [MAXV]; int path [MAXV] [MAXV]; int I, j, k, n = g. n; for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {A [I] [j] = g. edges [I] [j]; path [I] [j] =-1 ;}for (k = 0; k <n; k ++) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) if (A [I] [j]> (A [I] [k] + A [k] [j]) {
A [I] [j] = A [I] [k] + A [k] [j]; path [I] [j] = k ;}}
}

Algorithm time complexity: O (n3)

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.