Detailed steps of Dijkstra Dijkstra algorithm and realization of __ algorithm

Source: Internet
Author: User
Tags prev
1, the Dijkstra algorithm introduces the Dijkstra algorithm is a typical shortest path algorithm for calculating the shortest path of a particular vertex to all other vertices in a graph or net. The main feature is that the starting point is centered outward, the layer extends until the extension overwrites all vertices. 2, the Dijkstra algorithm thought sets g= (V,e) as an all-direction graph, divides the vertex set V in the graph into two groups. The first group is the vertex set of the shortest path that has been found (in s), when there is only one source point in S, and after each shortest path is obtained, the vertex of the shortest path is added to the set S, until all vertices are added to s. The second group is the set of vertices for the remaining indeterminate shortest paths (with u, the vertices in the u=v-s,u are continuously added to S, until U is empty, s=v). In the process of adding s, the shortest path length that always keeps the source point to the vertices in S is less than or equal to the shortest path length of any vertex in the source point to U. 3, the Dijkstra algorithm performs steps to set N to the number of vertices in g= (v,e). Dist[n] Stores the length of the current shortest path from the source point to each endpoint, Path[n] holds the corresponding path, S is the set of the end point of the shortest path, and U is the v-s, initially as all vertices that do not contain the source point. (1) The set S of the shortest path to initialize has the element source point A,s={a}. (2) Select a distance from the source point v the smallest vertex K, the K, add in S (the selected distance is V to K of the shortest path length). (3) To modify the distance between the vertices of u by taking K as the middle point of the new consideration; If the distance from the source point v to the vertex u (U u) is shorter than the original distance (not through the vertex K), modify the distance value of the vertex u, and the modified distance value is the distance of vertex K plus the right of vertex k to the U edge. (4) Repeat steps (2) and (3) until all vertices are contained in S. 4, the Dijkstra algorithm illustrates (1) a direction graph as follows, taking a as the source point, finding the shortest path of source point A to other vertices.

(2) The detailed steps of the algorithm are as follows:

Steps In the S Collection In the collection of U
Class Select a, at this point s={a} at this time the shortest path has a->a=0 with a as the middle point, starting from a to find U={B,C,D,E,F} a->b=1 a->c=2 a->e=15 a-> other u vertices are infinite
1 Found the shortest path a->b=1 b,s={a,b} from u={b,c,d,e,f} at this time the shortest path has a->a=0,a->b=1 with B as the middle point, from the a->b=1 this shortest path to start looking for U={C,D,E,F} (a->b->d=7) < initial infinite overwrite a->b->d= infinity for current a->b->d=7 a-> b-> other u vertices are infinite
2 Found the shortest path a->c=2 c,s={a,b,c} from u={c,d,e,f} at this time the shortest path has a->a=0,a->b=1,a->c=2 with B as the middle point, from the a->c=2 this shortest path to start looking for U={D,E,F} (a->c->d=5) < existing 7 rewritten as a->c->d=5
3 Found the shortest path a->c->d=5 d,s={a,b,c,d} from u={d,e,f} at this time the shortest path has a->a=0,a->b=1,a->c=2,a->c->d=5 with D as the middle point, from a- >c->d=5 this shortest path to start looking U={E,F} (A->c->d->e=9) < 15 in step 1 is rewritten as a->c->d->e=9 (a->c->d->f=6) < initial infinite overwrite as a->c- >d->f=6
4 Found the shortest path a->c->d->f=6 f,s={a,b,c,d,f} from u={e,f} at this point the shortest path has a a->a=0,a->b=1,a->c=2,a->c->d=5 a >c->d->f=6 with F as the middle point, starting from the shortest path of a->c->d->f=6 U={e} (A->C->D->F->E=7) < 9 rewritten in step 3 rewrite as a->c->d->f->e=7
5 Found the shortest path a->c->d->f->e=7 f,s={a,b,c,d,f,e} from u={f} at this point the shortest path has a->a=0,a->b=1,a->c=2,a->c->d= 5 a->c->d->f=6,a->c->d->f->e=7 The U collection is empty and the lookup is complete.


For example, the process of using the Dijkstra algorithm to compute the shortest path between source vertex 1 and other vertices is listed in the following table for a direction graph in the following figure.



Iterative process of the Dijkstra algorithm:

The theme is well understood above.

The following are specific implementations (c/s):
#include < iostream >
using namespace Std;

const int maxnum = 100;
const int maxint = 999999;


void Dijkstra (int n, int v, int * dist, int * prev, int c[maxnum][maxnum])
{
BOOL S[maxnum]; To determine if the point has been deposited in the S collection
for (int i = 1; I <= N; + + i)
{
Dist[i] = C[v][i]; Initializes the shortest distance from other points to the source point
S[i] = 0; The point has not been used in the initial
if (dist[i] = = maxint)//Initialize the other point to the previous point of the shortest path from the source point
Prev[i] = 0; Other points to the source point where the path does not exist,
Else
Prev[i] = v;
}//Initialize the state of the source point
DIST[V] = 0;
S[V] = 1;

In sequence, the nodes that are not placed in the S set are taken to the dist[of the minimum value and put into the binding s.
Once s contains all of the V-middle vertices, Dist records the shortest path length from the source point to all other vertices
for (int i = 2; I <= n; + + i)
{
int tmp = Maxint;
int u = v;
Find the dist[j] minimum for the point J that is not currently in use
for (int j = 1; j <= N; + + j)
If

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.