Shortest Path---dijkstra algorithm

Source: Internet
Author: User

Dijkstra algorithm Dijkstra algorithm

is the shortest path algorithm from one vertex to the rest, which solves the problem of the shortest path in the direction graph. The main feature of the Dijkstra algorithm is to extend from the center of the starting point to the outer layer until it expands to the end point.

Dijkstra algorithm Sample demo (from Ouyang_lianjun blog)

Here I beg, the shortest path from vertex v1 to each other vertex

First, first, we declare an array of dis, which initializes the values of:

Our vertex set T is initialized to: T={v1}

Since it is the shortest distance from the V1 vertex to the rest of the vertices, find a vertex closest to the vertex of number 1th. By using an array dis, it is known that the current V1 vertex is the v3 vertex. When the number 2nd vertex is selected, the value of dis[2] (subscript starting from 0) has been changed from "Estimated value" to "determined value", that is, the shortest distance from the V1 vertex to the V3 vertex is the current dis[2] value. Add the V3 to T.
Why is it? Because the V3 vertex is currently closest to the V1 vertex, and all sides of the graph are positive, it is certainly not possible to pass through the third vertex, making the V1 vertex to the V3 vertex shorter. Because the distance from the V1 vertex to the other vertices is certainly not v1 to the V3 vertex short.

OK, since the shortest path of a vertex is determined, below we will be based on the new vertex V3 would have a degree, found to V3 for arc Tail: < V3,V4, then we look at the path: v1–v3–v4 length is shorter than v1–v4, in fact, this is already very obvious, Because Dis[3] represents the v1–v4 length of infinity, and the length of the V1–V3–V4 is: 10+50=60, so update dis[3] value, the following results are obtained:

So dis[3] to be updated to 60. This process has a professional term called "slack". That is, V1 vertex to v4 vertex distance is dis[3], through the < v3,v4> this edge relaxation success. This is the main idea of the Dijkstra algorithm: "Edge" to relax the V1 vertex to the rest of the vertices of the distance.

Then we look for the minimum value from other values except dis[2] and dis[0], and find that the value of dis[4] is the smallest, and by the way it is explained, we can know that the shortest distance from V1 to V5 is the value of dis[4], and then we add V5 to the set T, and then, Consider whether the V5 will affect the value of our array dis, V5 has two degrees:< v5,v4> and < V5,v6>, and then we find that: the length of the V1–V5–V4 is: 50, and the value of dis[3] is 60, so we want to update dis[3] In addition, the length of the V1-V5-V6 is: 90, and dis[5] is 100, so we need to update the value of Dis[5]. The updated DIS array is as follows:

Then, continue to select the value of the indeterminate vertex from the dis to select a minimum value, found that the value of dis[3] is minimal, so the V4 is added to the set T, at this time the collection T={v1,v3,v5,v4}, and then, consider whether the V4 of the degree of the impact of our array dis value, V4 has a:< v4,v6>, and then we find: the length of the V1–V5–V4–V6 is: 60, and the value of dis[5] is 90, so we want to update the value of Dis[5], the updated dis array such as:

We then use the same principle to determine the shortest path for the V6 and V2, and finally the values of the DIS array are as follows:

Examples:

Aizu-alds1_12_b single Source Shortest Path I

Example Links: Https://vjudge.net/problem/Aizu-ALDS1_12_B topic meaning:

For the cost of a single source shortest path given a weighted graph G = (v,e), start with the vertex 0 of G and the sum of the weights of each edge on the shortest path of 0 to each vertex V

Input: The first line enters the number of vertices of G N. The next n rows enter the adjacency table of each vertex u in the following format U k v1 C1 v2 C2 v3 c3 ...

G each vertex number is 0 to n-1 respectively. U stands for vertex number, K for U, VI (I=1,2,3...K) for vertex with u and u to V for weighted C

Output the shortest distance from each vertex number V to 0 sequentially

AC Code:
#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;Const intINF =0x3f3f3f3f;intMain () {//freopen ("In.txt", "R", stdin);    intn,road[ the][ the],vis[ the],u,k,e,d,dis[ the],minroad;  while(SCANF ("%d", &n)! =EOF) {memset (Vis,0,sizeof(VIS)); memset (Road,inf,sizeof(road));  for(intI=0; i<n;i++)//turn adjacency table into adjacency matrix to deposit{scanf ("%d%d",&u,&k); Road[u][u]=0;  for(intj=0; j<k;j++) {scanf ("%d%d",&e,&d); Road[u][e]=D; }        }         for(intI=0; i<n;i++)//initializing the DIS arrayDis[i] = road[0][i]; vis[0]=1; intflag=0; inti,j;  while(1) {Minroad=inf; Flag=0;  for(i=0, j=0; i<n;i++)//The current dis array is found to be not marked and the smallest            {                if(vis[i]==0&& dis[i]<minroad) {Flag=1; J=i; Minroad =dis[i];} }            if(Flag = =0) Break;//all marked out while loopVIS[J] =1;  for(intI=0; i<n;i++)//Compare the values of the DIS array update            {                if(vis[i]==0&& (Dis[j]+road[j][i]) <dis[i]) dis[i]= dis[j]+Road[j][i]; }        }         for(intI=0; i<n;i++) printf ("%d%d\n", I,dis[i]); }    return 0;}

Shortest Path---dijkstra algorithm

Related Article

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.