-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
141 lines (124 loc) · 4.07 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
import { BinaryHeap } from './heap.js';
onload = function () {
// create a network
let curr_data;
const container = document.getElementById('mynetwork');
const container2 = document.getElementById('mynetwork2');
const genNew = document.getElementById('generate-graph');
const solve = document.getElementById('solve');
const temptext = document.getElementById('temptext');
// initialise graph options
const options = {
edges: {
arrows: {
to: true
},
labelHighlightBold: true,
font: {
size: 20
}
},
nodes: {
font: '12px arial red',
scaling: {
label: true
},
shape: 'icon',
icon: {
face: 'FontAwesome',
code: '\uf183',
size: 50,
color: '#991133',
}
}
};
// initialize your network!
let network = new vis.Network(container);
network.setOptions(options);
let network2 = new vis.Network(container2);
network2.setOptions(options);
function createData(){
const sz = Math.floor(Math.random() * 8) + 2;
// Adding people to nodes array
let nodes = [];
for(let i=1;i<=sz;i++){
nodes.push({id:i, label:"Person "+i})
}
nodes = new vis.DataSet(nodes);
// Dynamically creating edges with random amount to be paid from one to another friend
const edges = [];
for(let i=1;i<=sz;i++){
for(let j=i+1;j<=sz;j++){
// Modifies the amount of edges added in the graph
if(Math.random() > 0.5){
// Controls the direction of cash flow on edge
if(Math.random() > 0.5)
edges.push({from: i, to: j, label: String(Math.floor(Math.random()*100)+1)});
else
edges.push({from: j, to: i, label: String(Math.floor(Math.random()*100)+1)});
}
}
}
const data = {
nodes: nodes,
edges: edges
};
return data;
}
genNew.onclick = function () {
const data = createData();
curr_data = data;
network.setData(data);
temptext.style.display = "inline";
container2.style.display = "none";
};
solve.onclick = function () {
temptext.style.display = "none";
container2.style.display = "inline";
const solvedData = solveData();
network2.setData(solvedData);
};
function solveData() {
let data = curr_data;
const sz = data['nodes'].length;
const vals = Array(sz).fill(0);
// Calculating net balance of each person
for(let i=0;i<data['edges'].length;i++) {
const edge = data['edges'][i];
vals[edge['to'] - 1] += parseInt(edge['label']);
vals[edge['from'] - 1] -= parseInt(edge['label']);
}
const pos_heap = new BinaryHeap();
const neg_heap = new BinaryHeap();
for(let i=0;i<sz;i++){
if(vals[i]>0){
pos_heap.insert([vals[i],i]);
} else{
neg_heap.insert(([-vals[i],i]));
vals[i] *= -1;
}
}
const new_edges = [];
while(!pos_heap.empty() && !neg_heap.empty()){
const mx = pos_heap.extractMax();
const mn = neg_heap.extractMax();
const amt = Math.min(mx[0],mn[0]);
const to = mx[1];
const from = mn[1];
new_edges.push({from: from+1, to: to+1, label: String(Math.abs(amt))});
vals[to] -= amt;
vals[from] -= amt;
if(mx[0] > mn[0]){
pos_heap.insert([vals[to],to]);
} else if(mx[0] < mn[0]){
neg_heap.insert([vals[from],from]);
}
}
data = {
nodes: data['nodes'],
edges: new_edges
};
return data;
}
genNew.click();
};