-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstras.C
More file actions
122 lines (122 loc) · 3.09 KB
/
Dijkstras.C
File metadata and controls
122 lines (122 loc) · 3.09 KB
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
#include <stdio.h>
#include <stdlib.h>
#define V 10
static int g = 0;
int graph[V][V] = {0};
static int vertices[10];
static int lengths[10];
static int checkarray[10];
static int num_ver;
int check(int f)
{
int i;
for (i = 0; i < g; i++)
{
if (checkarray[i] == 1)
{
return 1;
}
}
return 0;
}
int min_dist()
{
int min = INT_MAX;
int min_index, i;
for (i = 0; i < num_ver; i++)
{
if (check(i) == 0 && lengths[i] < min)
{
min = lengths[i];
min_index = i;
}
}
return min_index;
}
void dijkstras(int s)
{
int i, j, minval = INT_MAX;
int marked = 0, minidx;
for (i = 0; i < num_ver; i++)
{
lengths[i] = INT_MAX;
}
lengths[s] = 0;
for (i = 0; i < num_ver - 1; i++)
{
int u = min_dist();
checkarray[u] = 1;
for (j = 0; j < num_ver; j++)
{
if (check(j) == 0 && graph[u][j] != 0)
{
if (lengths[u] != INT_MAX && graph[u][j] + lengths[u] < lengths[j])
{
lengths[j] = lengths[u] + graph[u][j];
}
}
}
}
printf("\nShortest distances are as follows:\n");
for (i = 0; i < num_ver; i++)
{
printf("\n");
printf("%d-%d\t%d", vertices[s], vertices[i], lengths[i]);
}
}
int find_index(int k, int a)
{
int i;
for (i = 0; i < k; i++)
{
if (vertices[i] == a)
{
return i;
}
}
}
int main()
{
int mm, i, j, weight, source, destination, num_edges;
printf("----------Dijkstra's Algortihm----------");
printf("\nEnter number of vertices:\t");
scanf("%d", &num_ver);
if (num_ver > 10)
{
exit(0);
}
else
{
printf("\nEnter the values of the vertices:\n");
for (i = 0; i < num_ver; i++)
{
scanf("%d", &vertices[i]);
}
printf("\nEnter number of edges:\t");
scanf("%d", &num_edges);
for (i = 0; i < num_edges; i++)
{
printf("Enter the source vertex of %d edge:\t", i + 1);
scanf("%d", &source);
printf("Enter destination vertex of %d edge:\t", i + 1);
scanf("%d", &destination);
printf("Enter weight of %d edge:\t", i + 1);
scanf("%d", &weight);
graph[find_index(num_ver, source)][find_index(num_ver, destination)] = weight;
graph[find_index(num_ver, destination)][find_index(num_ver, source)] = weight;
}
printf("\nThe adjecency matrix represetation is as follows:");
for (i = 0; i < num_ver; i++)
{
printf("\n");
for (j = 0; j < num_ver; j++)
{
printf("%d ", graph[i][j]);
}
}
printf("\nEnter the source vertex from whom the distance is to be calclulated:\t");
scanf("%d", &mm);
dijkstras(find_index(num_ver, mm));
}
return 0;
}