-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloyd-warshall.js
144 lines (123 loc) · 3.32 KB
/
floyd-warshall.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
const undefinedNumberField = Number.MAX_SAFE_INTEGER;
// Example matrix processing function
export default function processMatrix(matrix) {
matrix = create_d0(matrix);
let html = convertToHtmlTable(matrix);
html += d_of_k(matrix, 0);
return html;
}
// Creates the distanc matrix of matrix with distanc 0
function create_d0 (d0_Matrix) {
const matrixDimensions = d0_Matrix.length;
for (let i = 0; i < matrixDimensions; i++) {
for (let j = 0; j < matrixDimensions; j++) {
// Example operation: set each element to 0
if (i === j) {
d0_Matrix[i][j] = 0;
} else if (d0_Matrix[i][j] > 0) {
// Do nothing
} else {
d0_Matrix[i][j] = undefinedNumberField;
}
}
}
return d0_Matrix;
}
function convertToHtmlTable(matrix) {
const n = matrix.length;
let html = "<br><table>";
html += "<tr><td>D(0)</td><td><table>";
for (let i = 0; i < n; i++) {
html += "<tr>";
for (let j = 0; j < n; j++) {
const value = matrix[i][j];
if (value === undefinedNumberField) {
html += "<td>#</td>";
} else {
html += `<td>${value}</td>`;
}
}
html += "</tr>";
}
html += "</table></td></tr></table>";
return html;
}
function td(inValue, cssClass) {
let html = "";
if (cssClass.length === 0) {
if (inValue === undefinedNumberField) {
html += "<td>#</td>";
} else {
html += `<td>${inValue}</td>`;
}
} else {
if (inValue === undefinedNumberField) {
html += `<td class="${cssClass}">#</td>`;
} else {
html += `<td class="${cssClass}">${inValue}</td>`;
}
}
return html;
}
function tdstr(inValue) {
return `<td>${inValue}</td>`;
}
// Creates the distanc matrix of matrix with distanc `K`
// It is a recursiv function
export function d_of_k(list, k_step) {
let html = "";
const n = list.length;
let i_k_value, k_j_value;
if (k_step < n) {
html += "<br><table>";
html += "<tr>";
html += tdstr(`D(${k_step})`);
html += "<td>";
html += "<table>";
for (let i = 0; i < n; i++) {
i_k_value = list[i][k_step];
if (i !== k_step && i_k_value !== undefinedNumberField) { //wenn (nicht) Zeilen Nr. die gewählte n von d(n) spricht
html += "<tr>";
for (let j = 0; j < n; j++) {
k_j_value = list[k_step][j];
let distance_of_i_to_j = list[i][j];
const sum_of_both_fields = k_j_value + i_k_value;
if (k_step !== j && k_j_value !== undefinedNumberField && (sum_of_both_fields < distance_of_i_to_j || distance_of_i_to_j === undefinedNumberField)) {
list[i][j] = sum_of_both_fields;
distance_of_i_to_j = sum_of_both_fields;
html += td(distance_of_i_to_j, "corange");
} else if (k_step === j) {
html += td(distance_of_i_to_j, "cgreen");
} else {
html += td(distance_of_i_to_j, "");
}
}
html += "</tr>";
} else if (i === k_step) {
html += '<tr class="highlight">';
for (let row_index = 0; row_index < n; row_index++) {
const value = list[i][row_index];
html += td(value, "");
}
html += "</tr>";
} else {
html += "<tr>";
for (let column_index = 0; column_index < n; column_index++) {
const value = list[i][column_index];
if (column_index === k_step) {
html += td(value, "cgreen");
} else {
html += td(value, "");
}
}
html += "</tr>";
}
}
html += "</table>";
html += "</td>";
html += "</tr>";
html += "</table>";
html += d_of_k(list, ++k_step);
}
return html;
}