-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkstate.c
More file actions
99 lines (95 loc) · 2.05 KB
/
linkstate.c
File metadata and controls
99 lines (95 loc) · 2.05 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
#include <stdio.h>
int top = -1;
int ps[9999];
int d[9999];
void push(int n)
{
ps[++top] = n;
}
int pop()
{
int i, j, k;
for (i = 0; i <= top; i++)
{
for (j = i + 1; j <= top; j++)
{
if (d[ps[i]] < d[ps[j]])
{
k = ps[i];
ps[i] = ps[j];
ps[j] = k;
}
}
}
top--;
return ps[top + 1];
}
int main()
{
int N, n, i, j;
char v[3];
printf("Enter no. of nodes :\n");
scanf("%d", &n);
int c[n][n], f[n];
char s[n];
printf("Enter -1 if router does not exist or enter cost\n");
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
printf("%c -- %c\n", i + 'A', j + 'A');
scanf("%d", &c[i][j]);
c[j][i] = c[i][j];
}
f[i] = 0;
d[i] = 99999;
}
for (j = 0; j < n; j++)
{
for (i = 0; i < n; i++)
d[i] = 99999;
printf("Enter the node :\n");
scanf("%s", v);
N = v[0] - 'A';
for (i = 0; i < n; i++)
{
if (i == N)
s[i] = ' ';
else if (c[N][i] == -1)
s[i] = ' ';
else
s[i] = i + 'A';
}
d[N] = 0;
push(N);
while (1)
{
if (top == -1)
break;
N = pop();
for (i = 0; i < n; i++)
{
if (N != i && c[N][i] > -1)
{
if (d[N] + c[N][i] < d[i])
{
d[i] = d[N] + c[N][i];
if (s[i] - 'A' != i)
s[i] = s[N];
push(i);
}
}
}
}
printf("Table for %s :\n", v);
for (i = 0; i < n; i++)
{
printf("%c %d ", i + 'A', d[i]);
if (s[i] - 'A' != i)
printf("%c", s[i]);
printf("\n");
}
printf("\n");
}
return 0;
}