-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
273 lines (234 loc) · 8.76 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
onload = function () {
let curr_data,V,source,destination;
const container1 = document.getElementById('mynetwork');
const container2 = document.getElementById('mynetwork2');
const genNew = document.getElementById('generate-graph');
const solve = document.getElementById('solve');
const temptext1 = document.getElementById('temptext1');
const temptext2 = document.getElementById('temptext2');
const cities = ['Delhi', 'Mumbai', 'Gujarat', 'Goa', 'Kanpur', 'Chandigarh','Jammu', 'Jabalpur','Indore', 'Hyderabad', 'Bangalore', 'Chennai', 'Gangtok', 'Meghalaya'];
// initialising our graph options
const options = {
edges: {
labelHighlightBold: true,
font: {
size: 30
}
},
nodes: {
font: '12px arial red',
scaling: {
label: true
},
shape: 'icon',
icon: {
face: 'FontAwesome',
code: '\uf015',
size: 35,
color: '#03076e',
}
}
};
// Initializing our network!
// Network for the question graph
const network = new vis.Network(container1);
network.setOptions(options);
// Network for the result graph
const network2 = new vis.Network(container2);
network2.setOptions(options);
function createData(){
V = Math.floor(Math.random() * 8) + 5; // 5 and 12
let nodes = [];
for(let i=1;i<=V;i++) nodes.push({id:i, label: cities[i-1]})
// Prepareing the vis.js style nodes for our data
nodes = new vis.DataSet(nodes);
// Creating a tree like graph structure
let edges = [];
for(let i=2;i<=V;i++){
let neighbour = i - Math.floor(Math.random()*Math.min(i-1,3)+1); // Picks a neighbour from i-3 to i-1
edges.push({type: 0, from: i, to: neighbour, color: 'orange',label: String(Math.floor(Math.random()*70)+31)});
}
// Randomly adding new edges to graph
// 0->bus
// 1->plane
for(let i=1;i<=V/2;){
let m1 = Math.floor(Math.random()*V)+1;
let m2 = Math.floor(Math.random()*V)+1;
if(m1!==m2){
if(m1<m2){
let temp = m1;
m1 = m2;
m2 = temp;
}
// if an edge between these two vertices already exits And if it does then of which kind
let works = 0;
for(let j=0;j<edges.length;j++){
if(edges[j]['from']===m1 && edges[j]['to']===m2) {
if(edges[j]['type']===0)
works = 1;
else
works = 2;
}
}
// Adding edges to the graph
// If works == 0, we can add bus as well as plane between vertices
// If works == 1, we can only add plane between them
if(works <= 1) {
if (works === 0 && i < V / 4) {
// Adding a bus
edges.push({
type: 0,
from: m1,
to: m2,
color: 'orange',
label: String(Math.floor(Math.random() * 70) + 31)
});
} else {
// Adding a plane
edges.push({
type: 1,
from: m1,
to: m2,
color: 'green',
label: String(Math.floor(Math.random() * 50) + 1)
});
}
i++;
}
}
}
// Setting the new values of global variables
source = 1;
// source = Math.floor(Math.random() * 8) + 2
destination = V;
curr_data = {
nodes: nodes,
edges: edges
};
}
genNew.onclick = function () {
// Create new data and display the data
createData();
network.setData(curr_data);
temptext2.innerText = 'Find least time path from '+cities[source-1]+' to '+cities[destination-1];
temptext1.style.display = "inline";
temptext2.style.display = "inline";
container2.style.display = "none";
};
solve.onclick = function () {
// Creating graph from data and set to display
temptext1.style.display = "none";
temptext2.style.display = "none";
container2.style.display = "inline";
network2.setData(solveData());
};
// creating graph
function createGraph(data){
let graph = [];
for(let i=1;i<=V;i++){
graph.push([]);
}
for(let i=0;i<data['edges'].length;i++) {
let edge = data['edges'][i];
if(edge['type']===1)
continue;
graph[edge['to']-1].push([edge['from']-1,parseInt(edge['label'])]);
graph[edge['from']-1].push([edge['to']-1,parseInt(edge['label'])]);
}
return graph;
}
// djikstra algorithm function
function djikstra(graph, size, source) {
let vis = Array(size).fill(0);
let dist = [];
for(let i=1;i<=size;i++)
dist.push([10000,-1]);
dist[source][0] = 0;
for(let i=0;i<size-1;i++){
let mn = -1;
for(let j=0;j<size;j++){
if(vis[j]===0){
if(mn===-1 || dist[j][0]<dist[mn][0])
mn = j;
}
}
vis[mn] = 1;
for(let j in graph[mn]){
let edge = graph[mn][j];
if(vis[edge[0]]===0 && dist[edge[0]][0]>dist[mn][0]+edge[1]){
dist[edge[0]][0] = dist[mn][0]+edge[1];
dist[edge[0]][1] = mn;
}
}
}
return dist;
}
function shouldTakePlane(edges, dist1, dist2, mn_dist) {
let plane = 0;
let p1=-1, p2=-1;
for(let pos in edges){
let edge = edges[pos];
if(edge['type']===1){
let to = edge['to']-1;
let from = edge['from']-1;
let wght = parseInt(edge['label']);
if(dist1[to][0]+wght+dist2[from][0] < mn_dist){
plane = wght;
p1 = to;
p2 = from;
mn_dist = dist1[to][0]+wght+dist2[from][0];
}
if(dist2[to][0]+wght+dist1[from][0] < mn_dist){
plane = wght;
p2 = to;
p1 = from;
mn_dist = dist2[to][0]+wght+dist1[from][0];
}
}
}
return {plane, p1, p2};
}
function solveData() {
const data = curr_data;
// Creating adjacency list
const graph = createGraph(data);
// Applying djikstra from source and destination
let dist1 = djikstra(graph,V,source-1);
let dist2 = djikstra(graph,V,destination-1);
// Initialise min_dist to min distance via bus from source to destination
let mn_dist = dist1[destination-1][0];
// See if plane should be used
let {plane, p1, p2} = shouldTakePlane(data['edges'], dist1, dist2, mn_dist);
let new_edges = [];
if(plane!==0){
new_edges.push({arrows: { to: { enabled: true}}, from: p1+1, to: p2+1, color: 'green',label: String(plane)});
// Using spread operator to push elements of result of pushEdges to new_edges
new_edges.push(...pushEdges(dist1, p1, false));
new_edges.push(...pushEdges(dist2, p2, true));
} else{
new_edges.push(...pushEdges(dist1, destination-1, false));
}
const ans_data = {
nodes: data['nodes'],
edges: new_edges
};
return ans_data;
}
//??
function pushEdges(dist, curr, reverse) {
let tmp_edges = [];
while(dist[curr][0]!==0){
let fm = dist[curr][1];
if(reverse)
tmp_edges.push({arrows: { to: { enabled: true}},from: curr+1, to: fm+1, color: 'orange', label: String(dist[curr][0] - dist[fm][0])});
else
tmp_edges.push({arrows: { to: { enabled: true}},from: fm+1, to: curr+1, color: 'orange', label: String(dist[curr][0] - dist[fm][0])});
curr = fm;
}
return tmp_edges;
}
genNew.click();
};
// <!-- Mahir Neema -->
// <!-- GitHub profile https://github.com/Mahir-Neema -->
// <!-- https://www.linkedin.com/in/mahir-neema-55b66319a/ -->