Skip to main content

Write a C Program for Single Source Shortest Paths using Dijkstra’s Algorithm

 

#include<stdio.h>

 

#include<conio.h>

 

#define INFINITY 9999

 

#define MAX 10

 

void dijkstra(int G[MAX][MAX],int n,int startnode);

 

int main()

 

{

 

 

 

    int G[MAX][MAX],i,j,n,u;

 

 

    printf("Enter no. of vertices:");

 

    scanf("%d",&n);

 

    printf("\nEnter the adjacency matrix:\n");

 

    for(i=0;i<n;i++)

 

    for(j=0;j<n;j++)

 

    scanf("%d",&G[i][j]);

 

    printf("\nEnter the starting node:");

 

    scanf("%d",&u);

 

    dijkstra(G,n,u);

 

    getch();

 

    return 0;

 

}

 

void dijkstra(int G[MAX][MAX],int n,int startnode)

 

{

 

    int cost[MAX][MAX],distance[MAX],pred[MAX];

 

    int visited[MAX],count,mindistance,nextnode,i,j;

 

    //pred[] stores the predecessor of each node

 

    //count gives the number of nodes seen so far

 

    //create the cost matrix

 

    for(i=0;i<n;i++)

 

    for(j=0;j<n;j++)

 

    { if(G[i][j]==0)

 

    cost[i][j]=INFINITY;

 

    else

 

    cost[i][j]=G[i][j];

 

    }

 

    //initialize pred[],distance[] and visited[]

 

    for(i=0;i<n;i++)

 

    {

 

    distance[i]=cost[startnode][i];

 

    pred[i]=startnode;

 

    visited[i]=0;

 

    }

 

    distance[startnode]=0;

 

    visited[startnode]=1;

 

    count=1;

 

    while(count<n-1)

 

    {

 

     mindistance=INFINITY;

 

    //nextnode gives the node at minimum distance

 

    for(i=0;i<n;i++)

 

    if(distance[i]<mindistance&&!visited[i])

 

    {

 

    mindistance=distance[i];

 

    nextnode=i;

 

    }

 

    //check if a better path exists through nextnode

 

    visited[nextnode]=1;

 

    for(i=0;i<n;i++)

 

    if(!visited[i])

 

    if(mindistance+cost[nextnode][i]<distance[i])

 

    {

 

     distance[i]=mindistance+cost[nextnode][i];

 

     pred[i]=nextnode;

 

    }

 

   count++;

 

    }

 

    //print the path and distance of each node

 

    for(i=0;i<n;i++)

 

    if(i!=startnode)

 

{

 

    printf("\nDistance of node%d=%d",i,distance[i]);

 

    printf("\nPath=%d",i);

 

    j=i;

 

    do

 

    {

 

      j=pred[j];

 

      printf("<-%d",j);

 

    }while(j!=startnode);

 

      }

}


Comments

Popular posts from this blog

Windows 11 Pro Activation Key

 Windows 11 Pro key: W269N-WFGWX-YVC9B-4J6C9-T83GX Windows 11 Pro N key: MH37W-N47XK-V7XM9-C7227-GCQG9 Windows 11 Pro Workstations key: NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J Windows 11 Pro Workstations N key: 9FNHH-K3HBT-3W4TD-6383H-6XYWF Windows 11 Pro Education key: 6TP4R-GNPTD-KYYHQ-7B7DP-J447Y Windows 11 Home key: TX9XD-98N7V-6WMQ6-BX7FG-H8Q99 Windows 11 Home N key: 3KHY7-WNT83-DGQKR-F7HPR-844BM Windows 11 Home Home Single Language key: 7HNRX-D7KGG-3K4RQ-4WPJ4-YTDFH Windows 11 Home Country Specific: PVMJN-6DFY6-9CCP6-7BKTT-D3WVR Windows 11 Education key: NW6C2-QMPVW-D7KKK-3GKT6-VCFB2 Windows 11 Education N: 2WH4N-8QGBV-H22JP-CT43Q-MDWWJ Windows 11 Enterprise key: NPPR9-FWDCX-D2C8J-H872K-2YT43 Windows 11 Enterprise N key: DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4 Windows 11 Enterprise G: YYVX9-NTFWV-6MDM3-9PT4T-4M68B Windows 11 Enterprise G N: 44RPN-FTY23-9VTTB-MP9BX-T84FV Windows 11 Enterprise LTSC 2019 key: M7XTQ-FN8P6-TTKYV-9D4CC-J462D Windows 11 Enterprise N LTSC 2019 key: 92NFX-8DJQP-P6BBQ-THF...

Colon Classification by(Vikram kumar)

Colon Classification is one of the most systematic schemes of Library Classifications used in many libraries in India and a few libraries abroad as well. This was devised by the late Dr. S.R. Ranganathan. He found the existing scheme of library classification unable to cope with the multidimensional dynamic growth of universe of subjects. Colon Classification proceeds in a different manner in spite of enumerating all possible subjects and their subdivisions, it analyses the subject in its various components and places them under five fundamental categories known as personality, matter, energy, space and time. To connect or to synthesize the various components of a subject, different connection symbols have been provided. Readymade class numbers are also available, but to build a class number, one has to analyze and pick up the possible isolates belonging to different fundamental categories which are then put together with the help appropriate connecting symbols. Colon Classifi...