Skip to content

Commit 0297ed1

Browse files
Merge pull request #206 from DjamelEd/patch-3
Create Dijkstra.c
2 parents 5be4597 + a641435 commit 0297ed1

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

Dijkstra/Dijkstra.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include<stdio.h>
2+
#define INFINITY 9999
3+
#define MAX 10
4+
5+
void dijkstra(int G[MAX][MAX],int n,int startnode);
6+
7+
int main()
8+
{
9+
int G[MAX][MAX],i,j,n,u;
10+
printf("Enter no. of vertices:");
11+
scanf("%d",&n);
12+
printf("\nEnter the adjacency matrix:\n");
13+
14+
for(i=0;i<n;i++)
15+
for(j=0;j<n;j++)
16+
scanf("%d",&G[i][j]);
17+
18+
printf("\nEnter the starting node:");
19+
scanf("%d",&u);
20+
dijkstra(G,n,u);
21+
22+
return 0;
23+
}
24+
25+
void dijkstra(int G[MAX][MAX],int n,int startnode)
26+
{
27+
28+
int cost[MAX][MAX],distance[MAX],pred[MAX];
29+
int visited[MAX],count,mindistance,nextnode,i,j;
30+
31+
//pred[] stores the predecessor of each node
32+
//count gives the number of nodes seen so far
33+
//create the cost matrix
34+
for(i=0;i<n;i++)
35+
for(j=0;j<n;j++)
36+
if(G[i][j]==0)
37+
cost[i][j]=INFINITY;
38+
else
39+
cost[i][j]=G[i][j];
40+
41+
//initialize pred[],distance[] and visited[]
42+
for(i=0;i<n;i++)
43+
{
44+
distance[i]=cost[startnode][i];
45+
pred[i]=startnode;
46+
visited[i]=0;
47+
}
48+
49+
distance[startnode]=0;
50+
visited[startnode]=1;#include<stdio.h>
51+
#define INFINITY 9999
52+
#define MAX 10
53+
54+
void dijkstra(int G[MAX][MAX],int n,int startnode);
55+
56+
int main()
57+
{
58+
int G[MAX][MAX],i,j,n,u;
59+
printf("Enter no. of vertices:");
60+
scanf("%d",&n);
61+
printf("\nEnter the adjacency matrix:\n");
62+
63+
for(i=0;i<n;i++)
64+
for(j=0;j<n;j++)
65+
scanf("%d",&G[i][j]);
66+
67+
printf("\nEnter the starting node:");
68+
scanf("%d",&u);
69+
dijkstra(G,n,u);
70+
71+
return 0;
72+
}
73+
74+
void dijkstra(int G[MAX][MAX],int n,int startnode)
75+
{
76+
77+
int cost[MAX][MAX],distance[MAX],pred[MAX];
78+
int visited[MAX],count,mindistance,nextnode,i,j;
79+
80+
//pred[] stores the predecessor of each node
81+
//count gives the number of nodes seen so far
82+
//create the cost matrix
83+
for(i=0;i<n;i++)
84+
for(j=0;j<n;j++)
85+
if(G[i][j]==0)
86+
cost[i][j]=INFINITY;
87+
else
88+
cost[i][j]=G[i][j];
89+
90+
//initialize pred[],distance[] and visited[]
91+
for(i=0;i<n;i++)
92+
{
93+
distance[i]=cost[startnode][i];
94+
pred[i]=startnode;
95+
visited[i]=0;
96+
}
97+
98+
distance[startnode]=0;
99+
visited[startnode]=1;
100+
count=1;
101+
102+
while(count<n-1)
103+
{
104+
mindistance=INFINITY;
105+
106+
//nextnode gives the node at minimum distance
107+
for(i=0;i<n;i++)
108+
if(distance[i]<mindistance&&!visited[i])
109+
{
110+
mindistance=distance[i];
111+
nextnode=i;
112+
}
113+
114+
//check if a better path exists through nextnode
115+
visited[nextnode]=1;
116+
for(i=0;i<n;i++)
117+
if(!visited[i])
118+
if(mindistance+cost[nextnode][i]<distance[i])
119+
{
120+
distance[i]=mindistance+cost[nextnode][i];
121+
pred[i]=nextnode;
122+
}
123+
count++;
124+
}
125+
126+
//print the path and distance of each node
127+
for(i=0;i<n;i++)
128+
if(i!=startnode)
129+
{
130+
printf("\nDistance of node%d=%d",i,distance[i]);
131+
printf("\nPath=%d",i);
132+
133+
j=i;
134+
do
135+
{
136+
j=pred[j];
137+
printf("<-%d",j);
138+
}while(j!=startnode);
139+
}
140+
}
141+
count=1;
142+
143+
while(count<n-1)
144+
{
145+
mindistance=INFINITY;
146+
147+
//nextnode gives the node at minimum distance
148+
for(i=0;i<n;i++)
149+
if(distance[i]<mindistance&&!visited[i])
150+
{
151+
mindistance=distance[i];
152+
nextnode=i;
153+
}
154+
155+
//check if a better path exists through nextnode
156+
visited[nextnode]=1;
157+
for(i=0;i<n;i++)
158+
if(!visited[i])
159+
if(mindistance+cost[nextnode][i]<distance[i])
160+
{
161+
distance[i]=mindistance+cost[nextnode][i];
162+
pred[i]=nextnode;
163+
}
164+
count++;
165+
}
166+
167+
//print the path and distance of each node
168+
for(i=0;i<n;i++)
169+
if(i!=startnode)
170+
{
171+
printf("\nDistance of node%d=%d",i,distance[i]);
172+
printf("\nPath=%d",i);
173+
174+
j=i;
175+
do
176+
{
177+
j=pred[j];
178+
printf("<-%d",j);
179+
}while(j!=startnode);
180+
}
181+
}

0 commit comments

Comments
 (0)