forked from psrth/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFloyd_Warshall_Algorithm.dart
132 lines (119 loc) · 2.73 KB
/
Floyd_Warshall_Algorithm.dart
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
import 'dart:io';
var pred = [];
int minimum (var a, var b)
{
if (a <= b)
{
return a;
}
else
{
return b;
}
}
void floydwarshall (int n, var weight)
{
var dist = [];
for(var i = 0; i < n; i++)
{
var distlist = [];
var predlist = [];
for(var j = 0; j < n; j++)
{
distlist.insert(j, weight[i][j]);
predlist.insert(j, -999);
}
dist.insert(i, distlist);
pred.insert(i, predlist);
}
for(int k = 0; k < n; k++)
{
for(int i = 0; i < n; i++)
{
var distlist = [];
var predlist = [];
for(int j = 0; j < n; j++)
{
distlist.insert(j, minimum(dist[i][j], dist[i][k] + dist[k][j]));
predlist.insert(j, k);
}
dist.removeAt(i);
dist.insert(i, distlist);
pred.removeAt(i);
pred.insert(i, predlist);
}
}
print("The matrix shows the shortest distance between each of the vertices:");
for (var i = 0; i < n; i++)
print(dist[i]);
}
main()
{
print("Enter number of vertices in the graph:");
var n = int.parse(stdin.readLineSync());
var weight = [];
print("Enter initial weights of the paths connecting each vertex(Enter 9999 for the pair of vertices with no path):");
for(int i = 0; i < n; i++)
{
var weightlist = [];
for(int j = 0; j < n; j++)
{
print("Enter distance between $i to $j: ");
var input = int.parse(stdin.readLineSync());
weightlist.insert(j, input);
}
weight.insert(i, weightlist);
}
print("The initial distances between vertices:");
for (var i = 0; i < n; i++)
{
print(weight[i]);
}
floydwarshall(n, weight);
}
/*
Enter number of vertices in the graph:
4
Enter initial weights of the paths connecting each vertex(Enter 9999 for the pair of vertices with no path):
Enter distance between 0 to 0:
0
Enter distance between 0 to 1:
8
Enter distance between 0 to 2:
9999
Enter distance between 0 to 3:
1
Enter distance between 1 to 0:
9999
Enter distance between 1 to 1:
0
Enter distance between 1 to 2:
1
Enter distance between 1 to 3:
9999
Enter distance between 2 to 0:
4
Enter distance between 2 to 1:
12
Enter distance between 2 to 2:
0
Enter distance between 2 to 3:
5
Enter distance between 3 to 0:
9999
Enter distance between 3 to 1:
2
Enter distance between 3 to 2:
9
Enter distance between 3 to 3:
0
The initial distances between vertices:
[0, 8, 9999, 1]
[9999, 0, 1, 9999]
[4, 12, 0, 5]
[9999, 2, 9, 0]
The matrix shows the shortest distance between each of the vertices:
[0, 3, 4, 1]
[5, 0, 1, 6]
[4, 7, 0, 5]
[7, 2, 3, 0]*/