-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.js
224 lines (203 loc) · 6.99 KB
/
practice.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
onload = function () {
// create a network
var curr_data;
var sz;
var container = document.getElementById('mynetwork');
var container2 = document.getElementById('mynetwork2');
var genNew = document.getElementById('generate-graph');
var solve = document.getElementById('solve');
var temptext = document.getElementById('temptext');
var temptext2 = document.getElementById('temptext2');
// initialise graph options
var options = {
edges: {
labelHighlightBold: true,
font: {
size: 20
}
},
nodes: {
font: '12px arial red',
scaling: {
label: true
},
shape: 'icon',
icon: {
face: 'FontAwesome',
code: '\uf015',
size: 40,
color: '#991133',
}
}
};
// initialize your network!
var network = new vis.Network(container);
network.setOptions(options);
var network2 = new vis.Network(container2);
network2.setOptions(options);
function createData(){
sz = Math.floor(Math.random() * 8) + 3;
const cities = ['Delhi', 'Mumbai', 'Gujarat', 'Goa', 'Kanpur', 'Jammu', 'Hyderabad', 'Bangalore', 'Gangtok', 'Meghalaya'];
let nodes = [];
for(let i=1;i<=sz;i++){
nodes.push({id:i, label: cities[i-1]})
}
nodes = new vis.DataSet(nodes);
let edges = [];
for(let i=2;i<=sz;i++){
let neigh = i - Math.floor(Math.random()*Math.min(i-1,3)+1);
edges.push({type: 0, from: i, to: neigh, color: 'orange',label: String(Math.floor(Math.random()*70)+31)});
}
src = 1;
dst = sz;
for(let i=1;i<=sz/2;){
let n1 = Math.floor(Math.random()*sz)+1;
let n2 = Math.floor(Math.random()*sz)+1;
if(n1!=n2){
if(n1<n2){
let tmp = n1;
n1 = n2;
n2 = tmp;
}
let works = 0;
for(let j=0;j<edges.length;j++){
if(edges[j]['from']===n1 && edges[j]['to']===n2) {
if(edges[j]['type']===0)
works = 1;
else
works = 2;
}
}
if(works <= 1) {
if (works === 0 && i < sz / 4) {
edges.push({
type: 0,
from: n1,
to: n2,
color: 'orange',
label: String(Math.floor(Math.random() * 70) + 31)
});
} else {
edges.push({
type: 1,
from: n1,
to: n2,
color: 'green',
label: String(Math.floor(Math.random() * 50) + 1)
});
}
i++;
}
}
}
let data = {
nodes: nodes,
edges: edges
};
curr_data = data;
}
genNew.onclick = function () {
createData();
network.setData(curr_data);
temptext2.innerText = 'Find least time path from '+cities[src-1]+' to '+cities[dst-1];
temptext.style.display = "inline";
temptext2.style.display = "inline";
container2.style.display = "none";
};
solve.onclick = function () {
temptext.style.display = "none";
temptext2.style.display = "none";
container2.style.display = "inline";
network2.setData(solveData(sz));
};
function dijkstra(graph, sz, src) {
let vis = Array(sz).fill(0);
let dist = [];
for(let i=1;i<=sz;i++)
dist.push([10000,-1]);
dist[src][0] = 0;
for(let i=0;i<sz-1;i++){
let mn = -1;
for(let j=0;j<sz;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 solveData(sz) {
let data = curr_data;
let graph = [];
for(let i=1;i<=sz;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'])]);
}
let dist1 = dijkstra(graph,sz,src-1);
let dist2 = dijkstra(graph,sz,dst-1);
let mn_dist = dist1[dst-1][0];
let plane = 0;
let p1=-1, p2=-1;
for(let pos in data['edges']){
let edge = data['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];
}
}
}
new_edges = [];
if(plane!==0){
new_edges.push({arrows: { to: { enabled: true}}, from: p1+1, to: p2+1, color: 'green',label: String(plane)});
new_edges.concat(pushEdges(dist1, p1, false));
new_edges.concat(pushEdges(dist2, p2, true));
} else{
new_edges.concat(pushEdges(dist1, dst-1, false));
}
data = {
nodes: data['nodes'],
edges: new_edges
};
return data;
}
function pushEdges(dist, curr, reverse) {
tmp_edges = [];
while(dist[curr][0]!=0){
let fm = dist[curr][1];
if(reverse)
new_edges.push({arrows: { to: { enabled: true}},from: curr+1, to: fm+1, color: 'orange', label: String(dist[curr][0] - dist[fm][0])});
else
new_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();
};