-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra's.c
178 lines (148 loc) · 4.4 KB
/
Dijkstra's.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define MAX
typedef struct
{
char label [6];
int distance;
int previous;
int visited;
}
Vertex;
int CreateAdjacencyMatrix(Vertex Vertexarray[MAX],int argc, char *argv[],int Vertexcount,int AdjMatrix [][MAX])
{
int start = 0, end = 0;
int i = 0, j = 0;
char buffer [MAX]= {};
int l = 0;
strcpy(buffer,argv[1]);
char *token = NULL;
FILE *FH;
FH = fopen(buffer,"r+");
for(i = 0; i <MAX;i++)
for(j= 0;j<MAX;j++)
AdjMatrix[i][j]= -1;
while (fgets(buffer,sizeof(buffer)-1,FH))
{
if (buffer[strlen(buffer)-1]== '\n')
buffer[strlen(buffer)-1]== '\0';
token = strtok(buffer,",");
strcpy(Vertexarray[l].label,token);
while (token != NULL)
{
AdjMatrix[start][end] = atoi(token);
token = strtok(buffer,",");
sscanf(buffer,"%d %d",&start,&end);
end ++;
AdjMatrix[start][end]= atoi(token);
token = strtok(NULL,",");
sscanf(buffer,"%d %d",&start,&end);
end++;
}
start ++;
end = 0;
l++;
}
fclose(FH);
return Vertexcount;
}
void Dijkstra (int VertexCount,int startvertex,int adjacencyMatrix[MAX][MAX],Vertex Vertexarray[MAX])
{
int CurrentVertex = startvertex;
Vertexarray[startvertex].distance = 0;
Vertexarray[startvertex].previous = -1;
Vertexarray[startvertex].visited= 1;
int SmallestVertexIndex;
int dofv , cofuv,dofu;
int i ,x;
for ( x = 0; i < VertexCount; x++)
{
for ( i = 0; i < VertexCount; i++)
{
if (adjacencyMatrix[CurrentVertex][i]!= -1 && !Vertexarray[i].visited )
{
dofu = Vertexarray [CurrentVertex].distance;
cofuv = adjacencyMatrix[CurrentVertex][i];
dofv = Vertexarray[i].distance;
if (dofu + cofuv < dofv)
{
dofv = dofu + cofuv;
Vertexarray[i].distance = dofv;
Vertexarray[i].previous = CurrentVertex;
}
}
}
SmallestVertexIndex = -1;
int SmallestVertex = INT_MAX;
for ( i = 0; i < VertexCount; i++)
{
if (!Vertexarray[i].visited)
{
if (Vertexarray[i].distance < SmallestVertex)
{
SmallestVertex = Vertexarray[i].distance;
SmallestVertexIndex = i;
}
}
}
CurrentVertex = SmallestVertexIndex;
Vertexarray[CurrentVertex].visited = 1;
}
}
int main (int argc , char *argv[])
{
int AdjacencyMatrix[MAX][MAX] = {};
char StartingVertex[MAX]= {};
char dest [MAX]= {};
int destindex = -1;
int VertexCount = 0;
Vertex Vertexarray[MAX] = {};
char path [MAX]= {};
int startindex = 0;
VertexCount = CreateAdjacencyMatrix(Vertexarray,argc,argv,VertexCount,AdjacencyMatrix);
int i ,j;
#ifdef PRINTIT
printf("\n");
for (i = 0; i < VertexCount; i++)
{
for ( j = 0; i < VertexCount; j++)
{
printf("%5d\t",AdjacencyMatrix[i][j]);
printf("\n");
}
}
#endif
printf("What is the starting vertex? ");
scanf("%s",&StartingVertex);
#ifdef PRINTIT
printf("\n\nI\tL\tD\tP\tV\n");
for ( i = 0; i <VertexCount; i++)
{
printf("%d\t%s\t%d\t%d\t%d\n",i,Vertexarray[i].label,Vertexarray[i].distance,Vertexarray[i].previous,Vertexarray[i].visited);
}
#endif
printf("What is the destination? ",Vertexarray[0].label);
scanf("%s",&dest);
Dijkstra(VertexCount,startindex,AdjacencyMatrix,Vertexarray);
destindex = 0;
while (destindex < VertexCount && dest != Vertexarray[destindex].label)
{
destindex++;
}
int pathindex = -1;
destindex = -1;
int previndex = -1;
pathindex= Vertexarray[destindex].distance;
previndex = Vertexarray[destindex].previous;
path[pathindex]= Vertexarray[destindex].label;
while (pathindex > 0)
{
pathindex --;
path[pathindex]= Vertexarray[previndex].label;
previndex = Vertexarray[previndex].previous;
}
printf("The path from %c to %c is ",&StartingVertex,&dest);
}