Skip to content

Commit

Permalink
Refactoring in PQ and Dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Aug 3, 2014
1 parent c193dd3 commit 1e73051
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
20 changes: 9 additions & 11 deletions algorithms/graph/dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@ function dijkstra(graph, s) {
});

var currNode;
var relax = function (v) {
var newDistance = distance[currNode] + graph.edge(currNode, v);
if (newDistance < distance[v]) {
distance[v] = newDistance;
previous[v] = currNode;
q.changePriority(v, distance[v]);
}
};
while (!q.isEmpty()) {
currNode = q.extract();
var neighbors = graph.neighbors(currNode);
for (var i = 0; i < neighbors.length; i++) {
var v = neighbors[i];
// relaxation
var newDistance = distance[currNode] + graph.edge(currNode, v);
if (newDistance < distance[v]) {
distance[v] = newDistance;
previous[v] = currNode;
q.changePriority(v, distance[v]);
}
}
graph.neighbors(currNode).forEach(relax);
}
return {
distance: distance,
Expand Down
27 changes: 14 additions & 13 deletions data_structures/priority_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ var MinHeap = require('./heap').MinHeap;
* and not on the element itself
*/
function PriorityQueue(initialItems) {

var self = this;
MinHeap.call(this, function (a, b) {
return a.priority < b.priority ? -1 : 1;
return self._priority[a] < self._priority[b] ? -1 : 1;
});

this._items = {};
this._priority = {};

initialItems = initialItems || {};
var self = this;
Object.keys(initialItems).forEach(function (item) {
self.insert(item, initialItems[item]);
});
Expand All @@ -24,26 +25,26 @@ function PriorityQueue(initialItems) {
PriorityQueue.prototype = new MinHeap();

PriorityQueue.prototype.insert = function (item, priority) {
var o = {
item: item,
priority: priority
};

this._items[item] = o;
MinHeap.prototype.insert.call(this, o);
if (this._priority[item] !== undefined) {
return this.changePriority(item, priority);
}
this._priority[item] = priority;
MinHeap.prototype.insert.call(this, item);
};

PriorityQueue.prototype.extract = function (withPriority) {
var min = MinHeap.prototype.extract.call(this);
return withPriority ? min : min && min.item;
return withPriority ?
min && {item: min, priority: this._priority[min]} :
min;
};

PriorityQueue.prototype.priority = function (item) {
return this._items[item].priority;
return this._priority[item];
};

PriorityQueue.prototype.changePriority = function (item, priority) {
this._items[item].priority = priority;
this._priority[item] = priority;
this.heapify();
};

Expand Down
11 changes: 8 additions & 3 deletions test/data_structures/priority_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ describe('Min Priority Queue', function () {
assert(!q.isEmpty());

q.changePriority('b', 0);
q.changePriority('a', 1);
q.changePriority('c', 50);
q.changePriority('d', 1000);
q.changePriority('e', 2);

assert.equal(q.extract(), 'b');
assert.equal(q.extract(), 'd');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'a');
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'd');

});
});
Expand Down

0 comments on commit 1e73051

Please sign in to comment.