Shortest path-dijkstra algorithm and Floyd algorithm

Source: Internet
Author: User

Dijkstra algorithm

1. Definition Overview

The Dijkstra (Dijkstra) algorithm is a typical single-source shortest path algorithm that calculates the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. Dijkstra algorithm is a very representative shortest path algorithm, in many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on. Note that the algorithm requires no negative weight edges in the graph.

Problem Description: In the g= graph (v,e), assume that each edge e[i] length is w[i], find the shortest path from the vertex V0 to the remaining points. (Single source Shortest path)

2. Algorithm description

1) Algorithm idea: Set g= (V,e) is a weighted to the graph, the figure of the vertex set V into two groups, the first set of the shortest path of the vertex set (s), the initial s only one source point, each subsequent to a shortest path, will be added to the set S, until all the vertices are added to the S, The algorithm ends, and the second set is the vertex set (denoted by u) of the remaining indeterminate shortest paths, and the second set of vertices is added to s in order of ascending shortest path length. During the join process, the shortest path length that is always maintained from the source Point V to the vertices in S is not longer than the shortest path length from the source point V to any vertex in U. In addition, each vertex corresponds to a distance, the distance from the vertex in S is the shortest path length from V to the vertex, and the distance from the vertex in U is the current shortest path length from V to the vertex that includes only the vertex in S as the middle vertex.

2) Algorithm steps:

A. Initially, S contains only the source point, that is, the distance of S={v},v is 0. U contains other vertices other than V, that is: u={remaining vertices}, if V and u in vertex u have edges, then <u,v> normal right value, if U is not V's out edge adjacency point, then <u,v> weight is ∞.

B. Select the smallest vertex k from U, and add K to S (the selected distance is the shortest path length of V to K).

C. Change the distance of each vertex in the U with K as the new consideration; If the distance from the source point v to the vertex u (through vertex K) is shorter than the original distance (without the vertex K), the distance value of the vertex U is modified, the distance of the vertex k of the modified distance value and the right of the edge are added.

D. Repeat steps b and c until all vertices are contained in S.

Perform an animation process such as

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 point has been deposited into the s set of int n=maxnum;        for (int i=1; i<=n; ++i) {dist[i] = A[v0][i];                                S[i] = false;        No initial use of the point 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 out the current unused point J Dist[j] minimum for (int j=1; j<=n; ++j) if (!                             S[J]) && dist[j]<mindist) {u = j;            U Save the number of the point with the smallest distance in the current adjacency point mindist = Dist[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])//In the new U-point path to find a shorter path from the v0 point {dist[j] = Dist[u] + a[u][j    ];                    Update dist Prev[j] = u; Record precursor Vertex}}}}

4. Algorithm examples

First, give an image without a direction.

Using the Dijkstra algorithm to find the single-source shortest path with a as the starting point is as follows

Floyd algorithm

1. Definition Overview

The floyd-warshall algorithm (Floyd-warshall algorithm) is an algorithm to solve the shortest path between any two points, which can correctly handle the shortest path problem with the direction graph or the negative weight, and is also used to calculate the transitive closure of the graph. The time complexity of the floyd-warshall algorithm is O (n3) and the spatial complexity is O (n2).

2. Algorithm description

1) algorithm thought principle:

The Floyd algorithm is a classical dynamic programming algorithm. In plain language, the first goal is to find the shortest path from point I to J. From the point of view of dynamic planning, we need to make a reinterpretation of this goal (this interpretation is the essence of dynamic planning the most creative)

The shortest path from any node I to any node J is 2 possible, and 1 is straight from I to j,2 from I through several nodes K to J. Therefore, we assume that dis (i,j) is the distance from the shortest path of node u to node V, for each node K, we check whether dis (i,k) + dis (k,j) < dis (I,J) is established, if it is established, prove that the path from I to K to J is shorter than I direct to J Path, We set up dis (i,j) = Dis (i,k) + dis (k,j) so that when we traverse through all nodes K,dis (i,j) The distance from the shortest path of I to J is recorded.

2). Algorithm Description:

A. Start with any one-sided path. The distance between all two points is the right of the edge, and if no edge is connected between the two points, then the right is infinity.

B. For each pair of vertices u and V, see if there is a vertex w so that the path from u to W to V is shorter than known. If it is updated.

3). calculation----Cross-crossing method for process matrix of Floyd algorithm

Method: Two lines, starting from the upper left corner to the bottom right corner as shown below

Gives the matrix, where matrix A is the adjacency matrix, and the matrix Path records the point at which the shortest path between the U,v two points must pass

The corresponding calculation method is as follows:

Finally a3 is the result of the request.

3. Algorithm code implementation

typedef struct          {            char vertex[vertexnum];                                Vertex table             int edges[vertexnum][vertexnum];                       Adjacency matrix, can be regarded as the side table             int n,e;                                               Current number of vertices and sides in the graph         
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])) c17/> {
A[I][J]=A[I][K]+A[K][J]; path[i][j]=k; }

Shortest path-dijkstra algorithm and Floyd algorithm

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.