-
Notifications
You must be signed in to change notification settings - Fork 0
/
bford.js
162 lines (143 loc) · 4.48 KB
/
bford.js
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
var colors = require('colors');
var fs = require('fs');
var Table = require('cli-table');
var args = process.argv.slice(2);
var path = `${args[0]}.bf`;
try {
if (!fs.existsSync(path)) {
console.log('Graph file not found!'.red);
process.exit(1);
}
var dataArr = fs.readFileSync(path).toString().split('\n');
const SIZE = dataArr.length;
var neighbours = [];
var dataSteps = [];
// PRE INITIALIZATION
var data = make3DArray(SIZE, SIZE, SIZE, null);
var copy = JSON.parse(JSON.stringify(data));
dataSteps.push(copy);
// INITIALIZATION
for (var i = 0; i < SIZE; i++) {
data[i][i][i] = 0;
var rawNodeData = dataArr[i].split(' ');
neighbours.push([]);
for (var j = 0; j < rawNodeData.length; j++) {
var connectionData = rawNodeData[j].split('-');
var to = parseInt(connectionData[0]);
var cost = parseInt(connectionData[1]);
neighbours[i].push(to);
data[i][i][to] = cost;
}
}
var copy = JSON.parse(JSON.stringify(data));
dataSteps.push(copy);
console.log('\nInitialization'.bold.magenta);
copy.forEach((item, i) => {
console.table(item);
});
var thereIsChange = false;
var counter = 0;
// ITERATION
do {
counter++;
var propagateDataArr = [];
var currentShortest = [];
dataSteps[dataSteps.length - 1].forEach((matrix, i) => {
currentShortest.push([]);
for (var j = 0; j < SIZE; j++) {
var shortestYet = null;
var shorestK = null;
matrix.forEach((row, k) => {
if (row[j] != null) {
if (shortestYet == null) {
shortestYet = row[j];
shorestK = k;
} else if (row[j] < shortestYet) {
shortestYet = row[j];
shorestK = k;
}
}
});
currentShortest[i].push({
val: shortestYet,
row: j
});
}
});
var pastShortest = [];
dataSteps[dataSteps.length - 2].forEach((matrix, i) => {
pastShortest.push([]);
for (var j = 0; j < SIZE; j++) {
var shortestYet = null;
var shorestK = null;
matrix.forEach((row, k) => {
if (row[j] != null) {
if (shortestYet == null) {
shortestYet = row[j];
shorestK = k;
} else if (row[j] < shortestYet) {
shortestYet = row[j];
shorestK = k;
}
}
});
pastShortest[i].push({
val: shortestYet,
row: j
});
}
});
var propagatePlaces = [];
currentShortest.forEach((itemOuter, i) => {
itemOuter.forEach((item, j) => {
if (item.val != null) {
if (pastShortest[i][j].val == null) {
propagatePlaces.push(true);
propagateDataArr.push(new PropagateData(i, item.row, neighbours[i], item.val));
} else if (item.val < pastShortest[i][j].val) {
propagatePlaces.push(true);
propagateDataArr.push(new PropagateData(i, item.row, neighbours[i], item.val));
}
} else {
propagatePlaces.push(false);
}
});
});
if (propagatePlaces.includes(true)) {
thereIsChange = true;
while (propagateDataArr.length > 0) {
var curPropagateItem = propagateDataArr.shift();
curPropagateItem.receivers.forEach((rec, i) => {
var dist = data[rec][rec][curPropagateItem.from];
var num = curPropagateItem.newShortestValue + dist;
data[rec][curPropagateItem.from][curPropagateItem.to] = num;
});
}
var copy = JSON.parse(JSON.stringify(data));
dataSteps.push(copy);
console.log(`\nIteration-Step ${counter}`.bold.green);
copy.forEach((item, i) => {
console.table(item);
});
} else {
thereIsChange = false;
}
} while (thereIsChange);
} catch (e) {
console.log('Error:', e.stack);
}
function make3DArray(xmax, ymax, zmax, def) {
// Generates a 3D Array with given sizes and standard value def
var r, x, y, z;
for (r = [], x = 0; x < xmax; x++)
for (r[x] = [], y = 0; y < ymax; y++)
for (r[x][y] = [], z = 0; z < zmax; z++)
r[x][y][z] = def;
return r;
}
function PropagateData(from, to, receivers, newShortestValue) {
this.from = from;
this.to = to;
this.receivers = receivers;
this.newShortestValue = newShortestValue;
}