Skip to content

Commit

Permalink
Rename isInHeap to isInFringe to reduce coupling between Prim's algor…
Browse files Browse the repository at this point in the history
…ithm and fringe data structure
  • Loading branch information
andrii-venher committed Nov 19, 2023
1 parent 0181c24 commit b1d056d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions dijkstra-prim-visualization/src/algorithms/prim.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ const prim = (adjacencyList, createFringe) => {
const nodesCount = adjacencyList.length;

const fringe = new createFringe(nodesCount);
const isInHeap = new Array(nodesCount);
const isInFringe = new Array(nodesCount);
const steps = new Array(nodesCount);
const keys = new Array(nodesCount);

// Insert node 0 with value 0
fringe.insert(0, 0);
isInHeap[0] = true;
isInFringe[0] = true;
keys[0] = Infinity;
steps[0] = {
parent: -1,
weight: null,
};

for (let index = 1; index < nodesCount; index++) {
isInHeap[index] = true;
isInFringe[index] = true;
keys[index] = Infinity;
fringe.insert(index, Infinity);
}

while (!fringe.isEmpty()) {
const extractedNode = fringe.extractMin();
isInHeap[extractedNode.key] = false;
isInFringe[extractedNode.key] = false;

const neightbours = adjacencyList[extractedNode.key];
neightbours.forEach((n) => {
if (isInHeap[n.node]) {
if (isInFringe[n.node]) {
if (keys[n.node] > n.weight) {
fringe.decreaseKey(n.node, n.weight);
keys[n.node] = n.weight;
Expand Down

0 comments on commit b1d056d

Please sign in to comment.