|
| 1 | + |
| 2 | +// Min Heap Implementation |
| 3 | + |
| 4 | +class MinHeap { |
| 5 | + constructor(nums) { |
| 6 | + this.data = []; |
| 7 | + for (let i = 0; i < nums.length; i++) { |
| 8 | + this.add(nums[i]); |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + getParentIndex = (i) => Math.floor((i - 1) / 2); |
| 13 | + |
| 14 | + getLeftChildIndex = (i) => (i * 2) + 1; |
| 15 | + |
| 16 | + getRightChildIndex = (i) => (i * 2) + 2; |
| 17 | + |
| 18 | + swap = (i1, i2) => { |
| 19 | + const tmp = this.data[i1]; |
| 20 | + this.data[i1] = this.data[i2]; |
| 21 | + this.data[i2] = tmp; |
| 22 | + }; |
| 23 | + |
| 24 | + add = (e) => { |
| 25 | + this.data[this.data.length] = e; |
| 26 | + this.heapify(this.data.length - 1); |
| 27 | + return this.data[0]; |
| 28 | + }; |
| 29 | + |
| 30 | + heapify = (curIndex) => { |
| 31 | + if (this.data[this.getParentIndex(curIndex)] !== undefined |
| 32 | + && this.data[curIndex][0] < this.data[this.getParentIndex(curIndex)][0]) { |
| 33 | + this.swap(curIndex, this.getParentIndex(curIndex)); |
| 34 | + this.heapify(this.getParentIndex(curIndex)); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + pop = () => { |
| 39 | + const firstElement = this.data[0]; |
| 40 | + if (this.data.length > 1) { |
| 41 | + // replace it with the last element in heap |
| 42 | + this.data[0] = this.data[this.data.length - 1]; |
| 43 | + // remove last elem |
| 44 | + this.data.pop(); |
| 45 | + this.heapifyDown(); |
| 46 | + } |
| 47 | + |
| 48 | + return firstElement; |
| 49 | + }; |
| 50 | + |
| 51 | + heapifyDown = () => { |
| 52 | + let cur = 0; |
| 53 | + |
| 54 | + while (this.data[this.getLeftChildIndex(cur)] !== undefined) { |
| 55 | + // get the smallest child (right or left) |
| 56 | + let smallChildInd = this.getLeftChildIndex(cur); |
| 57 | + if ( |
| 58 | + this.data[this.getRightChildIndex(cur)] !== undefined |
| 59 | + && this.data[this.getRightChildIndex(cur)][0] < this.data[this.getLeftChildIndex(cur)][0] |
| 60 | + ) { |
| 61 | + smallChildInd = this.getRightChildIndex(cur); |
| 62 | + } |
| 63 | + // if one child (r or l) is less than curr we swap |
| 64 | + if (this.data[smallChildInd][0] < this.data[cur][0]) { |
| 65 | + this.swap(cur, smallChildInd); |
| 66 | + } |
| 67 | + cur = smallChildInd; |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +const minCostConnectPoints = function (points) { |
| 73 | + const n = points.length; |
| 74 | + let finalCost = 0; |
| 75 | + |
| 76 | + if (n > 1 && n <= 1000) { |
| 77 | + let x1, x2; |
| 78 | + let y1, y2; |
| 79 | + let dist; |
| 80 | + |
| 81 | + const adjList = new Map(); |
| 82 | + // prepare adjacent list (each node has cost to every other node) |
| 83 | + for (let i = 0; i < n - 1; i++) { |
| 84 | + [x1, y1] = points[i]; |
| 85 | + |
| 86 | + for (let j = i + 1; j < n; j++) { |
| 87 | + [x2, y2] = points[j]; |
| 88 | + dist = Math.abs(x1 - x2) + Math.abs(y1 - y2); |
| 89 | + adjList.get(i) ? adjList.get(i).push([dist, j]) : adjList.set(i, [[dist, j]]); |
| 90 | + adjList.get(j) ? adjList.get(j).push([dist, i]) : adjList.set(j, [[dist, i]]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // prim's algorithm |
| 95 | + const visited = new Set(); |
| 96 | + const minHeap = new MinHeap([[0, 0]]); // [cost,point] |
| 97 | + // we gonna visit each node |
| 98 | + while (visited.size < n) { |
| 99 | + let partialCost = 0, i = 0; |
| 100 | + |
| 101 | + // get the least cost & its correspondent node |
| 102 | + [partialCost, i] = minHeap.pop(); |
| 103 | + |
| 104 | + // if the node hasn't been visited |
| 105 | + if (!visited.has(i)) { |
| 106 | + finalCost += partialCost; |
| 107 | + visited.add(i); |
| 108 | + for (const neighbourWithCost of adjList.get(i)) { |
| 109 | + if (!visited.has(neighbourWithCost[1])) { |
| 110 | + minHeap.add(neighbourWithCost); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return finalCost; |
| 117 | +}; |
0 commit comments