Dijkstra algorithm to find the shortest circuit

Source: Internet
Author: User

Ezra · W. Dicos (Edsger wybe dijkstra,1930 May 11 ~2002 Year August 6) Dutchman. Computer scientist, who graduated from the University of Leiden, Holland, studied physics and mathematics in the early years and then converted to computational science. He was awarded the Turing Award in 1972 for the Nobel Prize in the computer science community, and he was also awarded the Afips Harry Goode Memorial Award, 1989 ACM SIGCSE the outstanding Contribution to the teaching of computer sciences education, And the 2002 ACM PODC most influential Paper award.

Ezra · W. Dicos (Edsger wybe Dijkstra)

1 proposed the "Goto Harmful Theory"; 2 proposed the semaphore and the PV primitive; 3 solved the problem of "the Philosopher's dinner"; 4 Shortest Path Algorithm (SPF) and the creator of the Banker's algorithm; 5 the first ALGOL 60 compiler designer and implementation; 6 the designer and developer of the operating system; E. Knuth, who is known as the greatest computer scientist of our time. Battling cancer for many years, he died at the age of 72 in the Dutch Nuenen's home on August 6, 2002.

Dijkstra algorithm C code

#include <stdio.h>#include<stdlib.h>#include<string.h>#defineMAXINT 32767#defineMaxnum 6Char*str ="ABCDEF";voidDijkstraint*dist,int*prev,int(*a) [Maxnum],intv0);voidFindpath (int*dist,int*prev,intStartintend);intMain () {intA[maxnum][maxnum] = {{0,6,3, MAXINT, MAXINT, MAXINT}, {6,0,2,5, MAXINT, MAXINT}, {3,2,0,3,4, MAXINT}, {MAXINT,5,3,0,2,3}, {MAXINT, MAXINT,4,2,0,5}, {MAXINT, MAXINT, MAXINT,3,5,0},                            }; intDist[maxnum] = {0}; intPrev[maxnum] = {0}; intV0 =2, i =0;        Dijkstra (Dist, prev, A, v0); Findpath (Dist, prev, V0,5); //For (i = 0; i < maxnum; i++)    {    //printf ("%d", Prev[i]);} system ("Pause"); return 0;}voidFindpath (int*dist,int*prev,intStartintend) {    intTMP =Prev[end]; int*rst = (int*)calloc(Maxnum,sizeof(int)); intCNT =0, i =0; RST[CNT]=end; CNT++; if(TMP = =start) {rst[cnt]=tmp; }    Else    {         while(TMP! =start) {rst[cnt]=tmp; TMP=Prev[tmp]; CNT++; } rst[cnt]=tmp; }        //printf ("%d\n", CNT);     for(i = CNT; I >=0; i--) {printf ("%c", Str[rst[i]]); if(I! =0) {printf (" -"); }} printf ("%d\n", Dist[end]);  Free(RST); RST=NULL; }voidDijkstraint*dist,int*prev,int(*a) [Maxnum],intv0) {    intS[maxnum]; intn = maxnum, i =0, j =0;  for(i =0; I < n; i++) {Dist[i]=A[v0][i]; S[i]=0; if(Dist[i] = =MAXINT) {Prev[i]= -1; }        Else{Prev[i]=V0; //printf ("%c precursor%c\n", Str[i], str[v0]);}} Dist[v0]=0; S[v0]=1;  for(i =1; I < n; i++)    {        intMindist =MAXINT; intU =V0;  for(j =0; J < N; J + +)        {            if((! S[J]) && Dist[j] <mindist) {u=J; Mindist=Dist[j]; }} S[u]=1;  for(j =0; J < N; J + +)        {            if((! S[J]) && A[u][j] <MAXINT) {                if(Dist[u] + a[u][j] <Dist[j]) {Dist[j]= Dist[u] +A[u][j]; PREV[J]=u; //printf ("%c precursor%c\n", Str[j], str[u]);                }            }        }    }}
View Code

Code for reference only

Resources

    1. Baidu Encyclopedia http://baike.baidu.com/link?url=LWr-IQcqdJoG9qAz_kmQ6kIybBDqEqj0bo3dk-t3A_vtd0P_ Ee1evcwm3iqokrwmregr_vlst7zgb_wsvqvcaq
    2. Shortest Path-dijkstra algorithm and Floyd algorithm http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html

Dijkstra algorithm to find the shortest circuit

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.