Skip to content

Commit

Permalink
Fixed A* and removed front() method from priority queue
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaKamikaze committed May 19, 2021
1 parent 000e075 commit f11ad75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
9 changes: 5 additions & 4 deletions node/PublicResources/js/aStar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PriorityQueue } from "../js/queue.js";
import { heuristicApprox } from "../js/pathModules.js";

export { aStar };

/**
* This functions changes the network elements. The nodes have their distanceOrigin
* property and their parent property assigned. This allows for the traceback
Expand All @@ -13,7 +15,7 @@ import { heuristicApprox } from "../js/pathModules.js";
*/
function aStar(cyGraph, startNode, endNode) {
let pending = new PriorityQueue(); // Open list
let fullyExpanded = new Set(); // Close list
let fullyExpanded = new Set(); // Closed list
let currentShortest = {}; // The minimum distance element from the priority queue.

// Initialization
Expand Down Expand Up @@ -53,6 +55,7 @@ function aStar(cyGraph, startNode, endNode) {
if (successor.data("distanceOrigin") <= possibleImprovedCost) {
return;
}
pending.nodes.splice(pending.nodes.indexOf(successor), 1);
}
// If the successor is in the closed list, but possibly needs reassessment:
else if (fullyExpanded.has(successor)) {
Expand All @@ -69,8 +72,8 @@ function aStar(cyGraph, startNode, endNode) {
possibleImprovedCost +
heuristicApprox(cyGraph, successor.id(), endNode.id())
);
pending.enqueue(successor);
successor.data("_parent", currentShortest.id());
pending.enqueue(successor);
}
}
});
Expand All @@ -85,5 +88,3 @@ function aStar(cyGraph, startNode, endNode) {
);
}
}

export { aStar };
7 changes: 3 additions & 4 deletions node/PublicResources/js/dijkstra.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PriorityQueue } from "../js/queue.js";
import { initializeSingleSource, relax } from "../js/pathModules.js";

export { dijkstra };

/**
* Dijkstra's algorithm will find the shortest path between all nodes in a weighted graph.
* @param {Class} cyGraph The graph nodes will be updated with new distances
Expand All @@ -12,14 +14,13 @@ function dijkstra(cyGraph, startNode) {
initializeSingleSource(graph, startNode);
let queue = new PriorityQueue();

// Initialization
queue.enqueue(startNode);

while (!queue.isEmpty()) {
let shortestDistance = queue.dequeue();
let nodes = shortestDistance.openNeighborhood((ele) => ele.isNode());

// For loop that checks if node can traverse each edge
// For loop that relaxes each successor of the shortestDistance node
for (let i = 0; i < nodes.length; i++) {
let edge = graph.getElementById(
`${shortestDistance.id()}${nodes[i].id()}`
Expand All @@ -32,5 +33,3 @@ function dijkstra(cyGraph, startNode) {
}
}
}

export { dijkstra };
10 changes: 0 additions & 10 deletions node/PublicResources/js/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ class PriorityQueue {
}
}

front() {
if (this.isEmpty()) {
throw new Error("There is no front. The queue is empty!");
}
// Return the lowest distance element, as this is a minimum priority queue.
else {
return this.nodes[0];
}
}

isEmpty() {
return this.nodes.length === 0;
}
Expand Down

0 comments on commit f11ad75

Please sign in to comment.