-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
52 lines (45 loc) · 2.22 KB
/
main.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
(function () {
'use strict';
const MIN = -100000,
MAX = 100000;
function getDistance(pair) {
return Math.sqrt(Math.pow(pair[0].x - pair[1].x, 2) + Math.pow(pair[0].y - pair[1].y, 2));
}
function execute() {
for (let i = 0; i < closestPair.testCases.length; i++) {
var test = closestPair.testCases[i],
startedAt = new Date().getTime(),
solution = closestPair.divideAndConquer(test.points),
timeTaken = new Date().getTime() - startedAt;
console.log('---------------------------');
console.log('Experiment ' + (i + 1));
console.log('Input size: ' + test.points.length);
console.log('Solution found: ' + JSON.stringify(solution) + '; distance: ' + getDistance(solution));
console.log('Correct solution: ' + JSON.stringify(test.solution) + '; distance: ' + getDistance(test.solution));
console.log('Time taken: ' + timeTaken + 'ms');
}
}
function solveRandom(size, bf, printPoints) {
var pgStartedAt = new Date().getTime(),
points = closestPair.generateRandomArrayOfPoints({x: MIN, y: MIN}, {x: MAX, y: MAX}, size),
bfStartedAt = new Date().getTime(),
bfSolution = bf === false || closestPair.bruteForce(points),
bfEndedAt = new Date().getTime(),
dcSolution = closestPair.divideAndConquer(points),
dcEndedAt = new Date().getTime();
if (bf !== false) console.log('Brute force solution: ' + JSON.stringify(bfSolution) + '; distance: ' + getDistance(bfSolution));
console.log('D&C solution: ' + JSON.stringify(dcSolution) + '; distance: ' + getDistance(dcSolution));
console.log('Time taken for point generation: ' + (bfStartedAt - pgStartedAt) + 'ms');
if (bf !== false) console.log('Time taken for brute force: ' + (bfEndedAt - bfStartedAt) + 'ms');
console.log('Time taken for D&C: ' + (dcEndedAt - bfEndedAt) + 'ms');
console.log('-----------------------------------------');
if (printPoints) {
console.log('points:');
console.log(JSON.stringify(points));
console.log('-----------------------------------------');
}
}
window.closestPair = window.closestPair || {};
window.closestPair.execute = execute;
window.closestPair.solveRandom = solveRandom;
}());