-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
195 lines (167 loc) · 4.97 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
import { BinaryHeap } from "./heap.js";
onload = function () {
// create a network
let curr_data;
// const container = document.getElementById("mynetwork");
const container2 = document.getElementById("mynetwork2");
const graphcontainer = document.getElementById("container2");
// const genNew = document.getElementById("generate-graph");
const solve = document.getElementById("solve");
const temptext = document.getElementById("temptext");
// getting friend name and amt paid
const nof = document.getElementById("nof");
const addfriendbtn = document.getElementById("addfriend");
const addfrienddiv = document.getElementById("friendslist");
// initialise graph options
const options = {
edges: {
arrows: { to: { enabled: true } },
color: "orange",
labelHighlightBold: true,
font: {
size: 20,
},
},
nodes: {
font: "12px arial red",
scaling: {
label: true,
},
shape: "icon",
icon: {
face: "FontAwesome",
code: "\uf183",
size: 40,
color: "#991133",
},
},
};
let network2 = new vis.Network(container2);
network2.setOptions(options);
addfriendbtn.onclick = function () {
addfrienddiv.style.display = "block";
graphcontainer.style.display = "flex";
solve.style.display = "block";
addfriendbtn.disabled = true;
let n = parseInt(nof.value);
for (let i = 0; i < n; i++) {
console.log("hello");
dynamicdivadd(i);
}
};
function dynamicdivadd(i) {
var div = document.createElement("div");
div.className = "col-auto";
var div2 = document.createElement("div");
div2.className = "row";
div.appendChild(div2);
var div3 = document.createElement("div");
div3.className = "col";
var inputname = document.createElement("input");
inputname.className = "form-control inputname";
inputname.autocomplete = "off";
inputname.id = `fname${i}`;
inputname.placeholder = `Friend ${i + 1} `;
div3.appendChild(inputname);
var div4 = document.createElement("div");
div4.className = "col";
var inputamt = document.createElement("input");
inputamt.className = "form-control inputamt";
inputamt.autocomplete = "off";
inputamt.id = `famt${i}`;
inputamt.placeholder = "amt paid";
div4.appendChild(inputamt);
div2.appendChild(div3);
div2.appendChild(div4);
addfrienddiv.appendChild(div);
var br = document.createElement("br");
addfrienddiv.appendChild(br);
}
solve.onclick = function () {
const data = createData();
curr_data = data;
temptext.style.display = "none";
container2.style.display = "inline";
const solvedData = solveData();
network2.setData(solvedData);
};
function createData() {
// const sz = Math.floor(Math.random() * 8) + 2;
// Adding people to nodes array
let nodes = [];
let n = parseInt(nof.value);
for (let i = 0; i < n; i++) {
var idname = `fname${i}`;
var fname = document.getElementById(idname).value;
nodes.push({ id: i + 1, label: fname });
}
nodes = new vis.DataSet(nodes);
var totalamt = 0;
for (let i = 0; i < n; i++) {
var idamt = `famt${i}`;
var famt = document.getElementById(idamt).value;
totalamt += parseInt(famt);
}
console.log(typeof totalamt);
console.log(typeof nof.value);
const nofv = parseInt(nof.value);
console.log(typeof nofv);
const amtperhead = Math.floor(totalamt / nofv);
console.log(amtperhead);
var netvaluearr = [];
for (let i = 0; i < n; i++) {
var idamt = `famt${i}`;
var famt = document.getElementById(idamt).value;
netvaluearr[i] = parseInt(famt) - amtperhead;
}
console.log(netvaluearr);
const data = {
nodes: nodes,
// edges: edges,
netvaluearr: netvaluearr,
};
return data;
}
function solveData() {
// console.log(f1name.value + " " + f1amt.value);
let data = curr_data;
const sz = data["nodes"].length;
// const vals = Array(sz).fill(0);
const vals = data.netvaluearr;
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;
}
};