Skip to content

Commit

Permalink
Remove function declaration from loop
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Aug 3, 2014
1 parent 9e305ab commit 4b0499f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
21 changes: 10 additions & 11 deletions algorithms/graph/breadth_first_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,18 @@ var breadthFirstSearch = function (graph, startVertex, callbacks) {
vertexQueue.push(startVertex);
callbacks = normalizeCallbacks(callbacks, [startVertex]);

/* jshint loopfunc: true */
while (!vertexQueue.isEmpty()) {
var vertex = vertexQueue.pop();
var vertex;
var enqueue = function (neighbor) {
if (callbacks.allowTraversal(vertex, neighbor)) {
callbacks.onTraversal(vertex, neighbor);
vertexQueue.push(neighbor);
}
};

while (!vertexQueue.isEmpty()) {
vertex = vertexQueue.pop();
callbacks.enterVertex(vertex);

graph.neighbors(vertex).forEach(function (neighbor) {
if (callbacks.allowTraversal(vertex, neighbor)) {
callbacks.onTraversal(vertex, neighbor);
vertexQueue.push(neighbor);
}
});

graph.neighbors(vertex).forEach(enqueue);
callbacks.leaveVertex(vertex);
}
};
Expand Down
18 changes: 9 additions & 9 deletions algorithms/graph/prim.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ var prim = function (graph) {
q.insert(vertex, Infinity);
});

/* jshint loopfunc: true */
var relax = function (neighbor) {
var weight = graph.edge(vertex, neighbor);
if (weight < q.priority(neighbor)) {
q.changePriority(neighbor, weight);
parent[neighbor] = vertex;
}
};

while (!q.isEmpty()) {
var top = q.extract(true);
var vertex = top.item,
Expand All @@ -38,14 +45,7 @@ var prim = function (graph) {
mst.addVertex(vertex);
}

// Relax.
graph.neighbors(vertex).forEach(function (neighbor) {
var weight = graph.edge(vertex, neighbor);
if (weight < q.priority(neighbor)) {
q.changePriority(neighbor, weight);
parent[neighbor] = vertex;
}
});
graph.neighbors(vertex).forEach(relax);
}

return mst;
Expand Down

0 comments on commit 4b0499f

Please sign in to comment.