-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
282 lines (245 loc) · 7.99 KB
/
script.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
let blocks = document.getElementsByClassName("drawing-area")[0];
let addEdge = false;
let cnt = 0;
let dist;
let alerted = localStorage.getItem("alerted") || "";
if (alerted !== "yes") {
alert(
"Read instructions before proceeding by clicking i-icon in the top-right corner"
);
localStorage.setItem("alerted", "yes");
}
// It is called when user starts adding edges by clicking on button given
const addEdges = () => {
if (cnt < 2) {
alert("Create atleast two nodes to add an edge");
return;
}
addEdge = true;
document.getElementById("add-edge-enable").disabled = true;
document.getElementsByClassName("run-btn")[0].disabled = false;
// Initializing array for adjacency matrix representation
dist = new Array(cnt + 1)
.fill(Infinity)
.map(() => new Array(cnt + 1).fill(Infinity));
};
// Temporary array to store clicked elements to make an edge between the(max size =2)
let arr = [];
const appendBlock = (x, y) => {
document.querySelector(".reset-btn").disabled = false;
document.querySelector(".click-instruction").style.display = "none";
// Creating a node
const block = document.createElement("div");
block.classList.add("block");
block.style.top = `${y}px`;
block.style.left = `${x}px`;
block.style.transform = `translate(-50%,-50%)`;
block.id = cnt;
block.innerText = cnt++;
// Click event for node
block.addEventListener("click", (e) => {
// Prevent node upon node
e.stopPropagation() || (window.event.cancelBubble = "true");
// If state variable addEdge is false, can't start adding edges
if (!addEdge) return;
block.style.backgroundColor = "coral";
arr.push(block.id);
// When two elements are push, draw a edge and empty the array
if (arr.length === 2) {
drawUsingId(arr);
arr = [];
}
});
blocks.appendChild(block);
};
// Allow creating nodes on screen by clicking
blocks.addEventListener("click", (e) => {
if (addEdge) return;
if (cnt > 12) {
alert("cannot add more than 12 vertices");
return;
}
console.log(e.x, e.y);
appendBlock(e.x, e.y);
});
// Function to draw a line between nodes
const drawLine = (x1, y1, x2, y2, ar) => {
// prevent multiple edges for same couple of nodes
if (dist[Number(ar[0])][Number(ar[1])] !== Infinity) {
document.getElementById(arr[0]).style.backgroundColor = "#333";
document.getElementById(arr[1]).style.backgroundColor = "#333";
return;
}
console.log(ar);
// Length of line
const len = Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);
const slope = x2 - x1 ? (y2 - y1) / (x2 - x1) : y2 > y1 ? 90 : -90;
// Adding length to distance array
dist[Number(ar[0])][Number(ar[1])] = Math.round(len / 10);
dist[Number(ar[1])][Number(ar[0])] = Math.round(len / 10);
// Drawing line
const line = document.createElement("div");
line.id =
Number(ar[0]) < Number(ar[1])
? `line-${ar[0]}-${ar[1]}`
: `line-${ar[1]}-${ar[0]}`;
line.classList.add("line");
line.style.width = `${len}px`;
line.style.left = `${x1}px`;
line.style.top = `${y1}px`;
// Edge weight
let p = document.createElement("p");
p.classList.add("edge-weight");
p.innerText = Math.round(len / 10);
p.contentEditable = "true";
p.inputMode = "numeric";
p.addEventListener("blur", (e) => {
if (isNaN(Number(e.target.innerText))) {
alert("Enter valid edge weight");
return;
}
n1 = Number(p.closest(".line").id.split("-")[1]);
n2 = Number(p.closest(".line").id.split("-")[2]);
// console.log(p.closest('.line'), e.target.innerText, n1, n2);
dist[n1][n2] = Number(e.target.innerText);
dist[n2][n1] = Number(e.target.innerText);
});
line.style.transform = `rotate(${
x1 > x2 ? Math.PI + Math.atan(slope) : Math.atan(slope)
}rad)`;
p.style.transform = `rotate(${
x1 > x2 ? (Math.PI + Math.atan(slope)) * -1 : Math.atan(slope) * -1
}rad)`;
line.append(p);
blocks.appendChild(line);
document.getElementById(arr[0]).style.backgroundColor = "#333";
document.getElementById(arr[1]).style.backgroundColor = "#333";
};
// Function to get (x, y) coordinates of clicked node
const drawUsingId = (ar) => {
if (ar[0] === ar[1]) {
document.getElementById(arr[0]).style.backgroundColor = "#333";
arr = [];
return;
}
x1 = Number(document.getElementById(ar[0]).style.left.slice(0, -2));
y1 = Number(document.getElementById(ar[0]).style.top.slice(0, -2));
x2 = Number(document.getElementById(ar[1]).style.left.slice(0, -2));
y2 = Number(document.getElementById(ar[1]).style.top.slice(0, -2));
drawLine(x1, y1, x2, y2, ar);
};
// Function to find shortest path from given source to all other nodes
const findShortestPath = (el) => {
let visited = [];
let unvisited = [];
clearScreen();
let source = Number(el.previousElementSibling.value);
if (source >= cnt || isNaN(source)) {
alert("Invalid source");
return;
}
document.getElementById(source).style.backgroundColor = "grey";
// console.log(source);
let parent = [];
parent[source] = -1;
visited = [];
for (i = 0; i < cnt; i++) unvisited.push(i);
// Array containing cost of reaching i(th) node from source
let cost = [];
for (i = 0; i < cnt; i++) {
i === source
? null
: dist[source][i]
? (cost[i] = dist[source][i])
: (cost[i] = Infinity);
}
cost[source] = 0;
// Array which will contain final minimum cost
let minCost = [];
minCost[source] = 0;
// Repeating until all edges are visited
while (unvisited.length) {
let mini = cost.indexOf(Math.min(...cost));
// console.log("draw", visited[visited.length-1],mini);
visited.push(mini);
unvisited.splice(unvisited.indexOf(mini), 1);
// Relaxation of unvisited edges
for (j of unvisited) {
if (j === mini) continue;
// console.log(mini, j);
if (cost[j] > dist[mini][j] + cost[mini]) {
minCost[j] = dist[mini][j] + cost[mini];
cost[j] = dist[mini][j] + cost[mini];
parent[j] = mini;
} else {
minCost[j] = cost[j];
// parent[j] = source;
}
}
cost[mini] = Infinity;
}
console.log("Minimum Cost", minCost);
for (i = 0; i < cnt; i++)
parent[i] === undefined ? (parent[i] = source) : null;
// console.log(parent);
indicatePath(parent, source);
};
const indicatePath = async (parentArr, src) => {
document.getElementsByClassName("path")[0].innerHTML = "";
for (i = 0; i < cnt; i++) {
let p = document.createElement("p");
p.innerText = "Node " + i + " --> " + src;
await printPath(parentArr, i, p);
}
};
const printPath = async (parent, j, el_p) => {
if (parent[j] === -1) return;
await printPath(parent, parent[j], el_p);
el_p.innerText = el_p.innerText + " " + j;
document.getElementsByClassName("path")[0].style.padding = "1rem";
document.getElementsByClassName("path")[0].appendChild(el_p);
// console.log(j,parent[j]);
if (j < parent[j]) {
let tmp = document.getElementById(`line-${j}-${parent[j]}`);
await colorEdge(tmp);
} else {
let tmp = document.getElementById(`line-${parent[j]}-${j}`);
await colorEdge(tmp);
}
};
const colorEdge = async (el) => {
if (el.style.backgroundColor !== "aqua") {
await wait(1000);
el.style.backgroundColor = "aqua";
el.style.height = "8px";
}
};
const clearScreen = () => {
document.getElementsByClassName("path")[0].innerHTML = "";
let lines = document.getElementsByClassName("line");
for (line of lines) {
line.style.backgroundColor = "#EEE";
line.style.height = "5px";
}
};
const resetDrawingArea = () => {
blocks.innerHTML = "";
const p = document.createElement("p");
p.classList.add("click-instruction");
p.innerHTML = "Click to create node";
blocks.appendChild(p);
document.getElementById("add-edge-enable").disabled = false;
document.querySelector(".reset-btn").disabled = true;
document.getElementsByClassName("path")[0].innerHTML = "";
cnt = 0;
dist = [];
addEdge = false;
};
const wait = async (t) => {
let pr = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("done!");
}, t);
});
res = await pr;
};