0% found this document useful (0 votes)
2 views6 pages

Program 1&3

The document provides implementations of Dijkstra's and Kruskal's algorithms in C for finding the shortest path in a graph and the minimum spanning tree, respectively. It includes code snippets for both algorithms along with sample input and output demonstrating their functionality. The Dijkstra algorithm calculates the shortest distances from a source node, while the Kruskal algorithm constructs a minimum spanning tree and calculates its cost.

Uploaded by

premalabhande
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)
2 views6 pages

Program 1&3

The document provides implementations of Dijkstra's and Kruskal's algorithms in C for finding the shortest path in a graph and the minimum spanning tree, respectively. It includes code snippets for both algorithms along with sample input and output demonstrating their functionality. The Dijkstra algorithm calculates the shortest distances from a source node, while the Kruskal algorithm constructs a minimum spanning tree and calculates its cost.

Uploaded by

premalabhande
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/ 6

Dijkstra Algorithm

#include<stdio.h>

#define INF 999

void dijkstra(int c[10][10],int n,int s,int d[10])

int v[10],min,u,i,j;

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

d[i]=c[s][i];

v[i]=0;

v[s]=1;

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

min=INF;

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

if(v[j]==0 && d[j]<min)

min=d[j];

u=j;

v[u]=1;

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

if(v[j]==0 && (d[u]+c[u][j])<d[j])

d[j]=d[u]+c[u][j];

int main()

{
int c[10][10],d[10],i,j,s,sum,n;

printf("\nEnter n value:");

scanf("%d",&n);

printf("\nEnter the graph data:\n");

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

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

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

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

scanf("%d",&s);

dijkstra(c,n,s,d);

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

printf("\nShortest distance from %d to %d is %d",s,i,d[i]);

return 0;

output

Enter n value:4

Enter the graph data:

444 767 987 12

999 87 56 45

1 0 999 678

444 678 235 0

Enter the souce node:1

Shortest distance from 1 to 1 is 444

Shortest distance from 1 to 2 is 247

Shortest distance from 1 to 3 is 247


Shortest distance from 1 to 4 is 12

Kruskal Algorithm

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

void union1(int

i,int j,int s[ ])

s[i]=j; }

int find(int v,int s[]) {

while(s[v]!=v)

v=s[v];

return v; }

void kruskal(int n,int c[10][10

int count,

i,s[10],min,j,

u,v,k,t[10][2],sum;

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

s[i]=

count=0;

sum=0;

k=0;

while(count<n
-1)

min=999;

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

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

if(c[i][j]!=0 && c[i][j]<min) {

min=c[i][j];

u=i,v=j; }}}

if(min==999)

break;

i=find(u,s);

j=find(v,s)

if(i!=j) {

t[k][0]=u;

t[k][1]=v;

k++;

count++;

sum=sum+min;

union1(

i,j,s);

c[u][v]=c[v][u]=999; }

if(count==n

-1)

printf(“cost of spanning tree =%d

\n”,sum);

printf(“spanning tree is shown below


\n”);

for(k=0;k<n

-1;k++)

printf(“%d

-> %d = %d

\n”,t[k][0],t[k][1], c[u][v]);

return();

printf(“spanning tree doesn’t exist

\n”);

void main() {

clock_t st,end;

int n,c[10][10],

i,j;

printf(“enter no. of nodes

\n”);

scanf(“%d”,&n);

printf(“enter the cost adjacency matrix

\n”);

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

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

scanf(“%d”,&c[i][j]); }}

st=clock();

kruskal(n,c);

end=clock();

printf("
\ntime taken in sec is %d

\n",(end

-st));

OUTPUT

Enter the number of nodes

Enter the cost adjacency matrix

999 3 5 4

3 999 999 7

5 999 999 6

4 7 6 999

Cost of spanning tree = 12

Spanning tree is shown below

01

03

02

time taken in sec is

You might also like