Note: The dijela Algorithm

Source: Internet
Author: User
Note: The dijela Algorithm
Core code
10 lines of code without curly braces. set [] represents the vertex set S. The core idea of the algorithm is to constantly add the vertex closest to the set S to the set according to the greedy algorithm. dis is the distance from the source point to each node.
for(i=1;i<N;i++){Min=65535;for(j=0;j<N;j++){if(!set[j]&&dis[j]<Min){k=j;Min=dis[j];}}set[k]=1;for(j=0;j<N;j++){if(!set[j]&&(Min+arc[k][j]<dis[j]))dis[j]=Min+arc[k][j];}}

After writing down the code above, all we need to do is initialize the vertex, and the other is initialize the graph.
Const int n = 9, INF = 65535; int vexs [N]; int arc [N] [N]; void Init () {// initialization Diagram for (INT I = 0; I <n; I ++) {for (Int J = I; j <n; j ++) {arc [I] [J] = inf ;}} arc [0] [1] = 1; ARC [0] [2] = 5; arc [1] [2] = 3; ARC [1] [3] = 7; ARC [1] [4] = 5; ARC [2] [4] = 1; arc [2] [5] = 7; ARC [3] [4] = 2; ARC [3] [6] = 3; ARC [4] [5] = 3; arc [4] [6] = 6; ARC [4] [7] = 9; ARC [5] [7] = 5; ARC [6] [7] = 2; arc [6] [8] = 7; ARC [7] [8] = 4; for (INT I = 0; I <n; I ++) {for (Int J = I; j <n; j ++) {arc [J] [I] = arc [I] [J] ;}} for (INT I = 0; I <n; I ++) vexs [I] = I ;}

Initialize the dijela algorithm, that is, select the source node.
// Initialization (starting from V0 here) for (I = 0; I <n; I ++) {set [I] = 0; dis [I] = arc [0] [I];} dis [0] = 0; set [0] = 1;

Complete code
# Include <stdio. h> const int n = 9, INF = 65535; int vexs [N]; int arc [N] [N]; void Init () {// initialization Diagram for (INT I = 0; I <n; I ++) {for (Int J = I; j <n; j ++) {arc [I] [J] = inf ;}} arc [0] [1] = 1; ARC [0] [2] = 5; arc [1] [2] = 3; ARC [1] [3] = 7; ARC [1] [4] = 5; ARC [2] [4] = 1; arc [2] [5] = 7; ARC [3] [4] = 2; ARC [3] [6] = 3; ARC [4] [5] = 3; arc [4] [6] = 6; ARC [4] [7] = 9; ARC [5] [7] = 5; ARC [6] [7] = 2; arc [6] [8] = 7; ARC [7] [8] = 4; for (INT I = 0; I <n; I ++) {for (Int J = I; J <n; j ++) {arc [J] [I] = arc [I] [J] ;}} for (INT I = 0; I <N; I ++) vexs [I] = I;} int main () {Init (); int I, J, K, min; int set [N], dis [N]; // initialization (starting from V0 here) for (I = 0; I <n; I ++) {set [I] = 0; dis [I] = arc [0] [I];} dis [0] = 0; set [0] = 1; // Main Loop for (I = 1; I <n; I ++) {min = 65535; // distance V1, V2, V3 ,.... find the nearest vertex for (j = 0; j <n; j ++) {If (! Set [J] & dis [J] <min) {k = J; min = dis [J] ;}} set [k] = 1; // update the shortest path from V0 to each node based on the latest Shortest Path. For (j = 0; j <n; j ++) {If (! Set [J] & (min + arc [k] [J] <dis [J]) dis [J] = min + arc [k] [J];} // output result for (INT I = 1; I <n; I ++) printf ("% d", DIS [I]);}

Result
1 4 7 5 8 10 12 16
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.