Skip to content

changed to deep comparison for sanity #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion algorithm/cryptography/affine_cipher/basic/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function decrypt (cypherText) {
var plainText = '';
var aInverse = (function () {
for (var i = 1; i < N; i++) {
if ( ((keys.a * i).mod (N)) == 1 ) {
if ( ((keys.a * i).mod (N)) === 1 ) {
return i;
}
}
Expand Down
6 changes: 3 additions & 3 deletions algorithm/dp/max_sum_path/basic/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ function update(i, j, value) {
}
for (var i = 0; i < N; i++) {
for (var j = 0; j < M; j++) {
if (i == 0 && j == 0) {
if (i === 0 && j === 0) {
update(i, j, D[i][j]);
} else if (i == 0) {
} else if (i === 0) {
tracer._select(i, j - 1);
update(i, j, DP[i][j - 1] + D[i][j]);
tracer._deselect(i, j - 1);
} else if (j == 0) {
} else if (j === 0) {
tracer._select(i - 1, j);
update(i, j, DP[i - 1][j] + D[i][j]);
tracer._deselect(i - 1, j);
Expand Down
4 changes: 2 additions & 2 deletions algorithm/graph_search/bfs/shortest_path/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ var s = Integer.random(0, G.length - 1); // s = start node
var e; // e = start node
do {
e = Integer.random(0, G.length - 1);
} while (s == e);
} while (s === e);
var MAX_VALUE = Infinity;
logger._print('finding the shortest path from ' + s + ' to ' + e);
var minWeight = BFS(s);
if (minWeight == MAX_VALUE) {
if (minWeight === MAX_VALUE) {
logger._print('there is no path from ' + s + ' to ' + e);
} else {
logger._print('the shortest path from ' + s + ' to ' + e + ' is ' + minWeight);
Expand Down
16 changes: 8 additions & 8 deletions algorithm/graph_search/bridges/efficient/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var util = function (u, disc, low, parent) {
//u is the node that is currently being processed in the DFS (depth-first search)
//disc is the numbering of the vertices in the DFS, starting at 0
//low[v] is the lowest numbered vertex that can be reached from vertex v along the DFS
//parent is the node that u came from
//parent is the node that u came from
logger._print ('');
logger._print ('Visiting node ' + u);
graphTracer._visit (u)._wait ();
Expand All @@ -28,10 +28,10 @@ var util = function (u, disc, low, parent) {
}

adj [u].forEach (function (v) {
if (disc [v] > -1 && v == parent) {
if (disc [v] > -1 && v === parent) {
trace(v);
logger._print (u + '\'s neighbor ' + v + ' is u\'s parent. Not visiting it.');

}
else if (disc[v] > -1 && v != parent) {
trace(v);
Expand All @@ -42,7 +42,7 @@ var util = function (u, disc, low, parent) {
}
}

if (disc[v] == -1) {
if (disc[v] === -1) {
trace(v);
logger._print (u + '\'s neighbor ' + v + ' has not been visited yet');

Expand All @@ -54,18 +54,18 @@ var util = function (u, disc, low, parent) {
logger._print ('Setting low [' + u + '] to ' + Math.min (low [u], low [v]));
low [u] = Math.min (low [u], low [v]);

if (low [v] == disc [v]) {
if (low [v] === disc [v]) {
logger._print ('low [' + v + '] == disc [' + v + '], low[' + v + ']=' + low[v] + ', disc[' + v + ']=' + disc[v]);
logger._print (u + ' -> ' + v + ' is a bridge. Adding ' + u + '->' + v + 'to the set of bridges found');
bridges.push ([u, v]);
}
}

});
};

(function findBridges (graph) {

var disc = filledArray (graph.length, -1);
var low = filledArray (graph.length, -1);

Expand All @@ -92,7 +92,7 @@ var util = function (u, disc, low, parent) {

logger._print ('Starting the main for loop (for each node)');
for (var v = 0; v < graph.length; v++) {
if (disc[v] == -1) {
if (disc[v] === -1) {
logger._print (v + ' has not been visited yet. Calling util (' + v + ', [' + disc + '], [' + low + '],' + v + ') from the for loop');
util (v, disc, low, v);
logger._print ('Returned in for loop after util (' + v + ', [' + disc + '], [' + low + '], [' + v + '])');
Expand Down
6 changes: 3 additions & 3 deletions algorithm/graph_search/dfs/shortest_path/code.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function DFS(node, parent, weight) { // node = current node, parent = previous node
if (minWeight < weight) return;
if (node == e) {
if (node === e) {
tracer._visit(node, parent, weight)._wait();
if (minWeight > weight) {
minWeight = weight;
Expand All @@ -24,14 +24,14 @@ var s = Integer.random(0, G.length - 1); // s = start node
var e; // e = end node
do {
e = Integer.random(0, G.length - 1);
} while (s == e);
} while (s === e);
var MAX_VALUE = Infinity;
var minWeight = MAX_VALUE;
logger._print('finding the shortest path from ' + s + ' to ' + e);
var D = []; // D[i] indicates whether the i-th node is discovered or not
for (var i = 0; i < G.length; i++) D.push(false);
DFS(s, undefined, 0);
if (minWeight == MAX_VALUE) {
if (minWeight === MAX_VALUE) {
logger._print('there is no path from ' + s + ' to ' + e);
} else {
logger._print('the shortest path from ' + s + ' to ' + e + ' is ' + minWeight);
Expand Down
6 changes: 3 additions & 3 deletions algorithm/graph_search/dijkstra/shortest_path/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Dijkstra(start, end) {
minIndex = i;
}
}
if (minDistance == MAX_VALUE) break; // If there is no edge from current node, jump out of loop
if (minDistance === MAX_VALUE) break; // If there is no edge from current node, jump out of loop
D[minIndex] = true;
tracerS._select(minIndex);
tracer._visit(minIndex)._wait();
Expand All @@ -32,7 +32,7 @@ function Dijkstra(start, end) {
}
tracer._leave(minIndex)._wait();
}
if (S[end] == MAX_VALUE) {
if (S[end] === MAX_VALUE) {
logger._print('there is no path from ' + start + ' to ' + end);
} else {
logger._print('the shortest path from ' + start + ' to ' + end + ' is ' + S[end]);
Expand All @@ -43,6 +43,6 @@ var s = Integer.random(0, G.length - 1); // s = start node
var e; // e = end node
do {
e = Integer.random(0, G.length - 1);
} while (s == e);
} while (s === e);
logger._print('finding the shortest path from ' + s + ' to ' + e)._wait();
Dijkstra(s, e);
6 changes: 3 additions & 3 deletions algorithm/graph_search/floyd_warshall/shortest_paths/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function FloydWarshall() {
// If there is a shorter path using k, use it instead
for (var k = 0; k < G.length; k++) {
for (i = 0; i < G.length; i++) {
if (k == i) continue;
if (k === i) continue;
tracer._visit(k, i)._wait();
for (j = 0; j < G.length; j++) {
if (i == j || j == k) continue;
if (i === j || j === k) continue;
tracer._visit(j, k)._wait();
if (S[i][j] > S[i][k] + S[k][j]) {
tracer._visit(j, i, S[i][j])._wait();
Expand All @@ -33,7 +33,7 @@ function FloydWarshall() {
}
for (i = 0; i < G.length; i++)
for (j = 0; j < G.length; j++)
if (S[i][j] == MAX_VALUE) logger._print('there is no path from ' + i + ' to ' + j);
if (S[i][j] === MAX_VALUE) logger._print('there is no path from ' + i + ' to ' + j);
else logger._print('the shortest path from ' + i + ' to ' + j + ' is ' + S[i][j]);
}
var MAX_VALUE = Infinity;
Expand Down
8 changes: 4 additions & 4 deletions algorithm/sorting/cycle/basic/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ for (var cycleStart = 0; cycleStart <= N - 2; cycleStart++) {
}

// if the item is already there, this is not a circle
if (pos == cycleStart) {
if (pos === cycleStart) {
tracer._deselect(cycleStart);
continue;
}

// otherwise put the item there or right after any duplicates
while (item == D[pos]) {
while (item === D[pos]) {
pos++;
}

Expand All @@ -46,7 +46,7 @@ for (var cycleStart = 0; cycleStart <= N - 2; cycleStart++) {
tracer._denotify(pos)._denotify(cycleStart);

// rotate the rest of the cycle
while (pos != cycleStart) {
while (pos !== cycleStart) {
pos = cycleStart;

for (i = cycleStart + 1; i <= N - 1; i++) {
Expand All @@ -56,7 +56,7 @@ for (var cycleStart = 0; cycleStart <= N - 2; cycleStart++) {
}
}

while (item == D[pos]) {
while (item === D[pos]) {
pos++;
}

Expand Down