0% found this document useful (0 votes)
45 views3 pages

C Dijakstras

This C program implements Dijkstra's algorithm to find the shortest path between nodes in a graph. The user inputs the number of nodes, a cost matrix containing the weights of edges between nodes, and a source node. The dij() function then calculates the shortest path from the source node to all other nodes using Dijkstra's algorithm and stores the results in a distance array. The distances are finally printed from the source to each other node.

Uploaded by

Khushi Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

C Dijakstras

This C program implements Dijkstra's algorithm to find the shortest path between nodes in a graph. The user inputs the number of nodes, a cost matrix containing the weights of edges between nodes, and a source node. The dij() function then calculates the shortest path from the source node to all other nodes using Dijkstra's algorithm and stores the results in a distance array. The distances are finally printed from the source to each other node.

Uploaded by

Khushi Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>

//#include<conio.h>

#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[100])

int i,u,count,w,flag[10],min;

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

flag[i]=0,dist[i]=cost[v][i];

count=2;

while(count<=n)

min=99;

for(w=1;w<=n;w++)

if(dist[w]<min && !flag[w])

min=dist[w],u=w;

flag[u]=1;

count++;

for(w=1;w<=n;w++)

if((dist[u]+cost[u][w]<dist[w]) && !flag[w])

dist[w]=dist[u]+cost[u][w];

void main()

int v,n,i,j,cost[10][10],dist[10];
//clrscr();

printf("\nEnter the number of Nodes : \n");

scanf("%d",&n);

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

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

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

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

if(cost[i][j]==0)

cost[i][j]=infinity;

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

scanf("%d",&v);

dij(n,v,cost,dist);

printf("\nShortest path : \n");

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

if(i!=v)

printf("%d->%d, cost = %d\n",v,i,dist[i]);

// getch();

}
OUTPUT

Enter the number of Nodes :

Enter the cost matrix :

0 3 999 7 999

3 0 4 2 999

04056

72504

999 999 6 4 0

Enter the source matrix :

Shortest path :

1->2, cost = 3

1->3, cost = 7

1->4, cost = 5

1->5, cost = 9

...Program finished with exit code 0

Press ENTER to exit console.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy