Skip to content

Commit c972cdc

Browse files
committed
Fix dfs implementation
1 parent 4311f9d commit c972cdc

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/graphs/searching/bfs.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,37 @@
6464
* @param {number} destination The destination node
6565
* @returns {boolean} true/false depending whether the params are valid
6666
*/
67-
function invalidParams(graph, source, destination) {
67+
function validateParams(graph, source, destination) {
6868
if (!graph) {
69-
return true;
69+
throw 'The graph should be represented as a matrix';
70+
}
71+
if (!graph[0]) {
72+
throw 'The graph should be represented as ' +
73+
'a matrix, with size at least 1x1';
74+
}
75+
var width = graph[0].length;
76+
for (var i = 0; i < graph.length; i += 1) {
77+
if (graph[i] !== width) {
78+
throw 'The graph should be represented as a matrix';
79+
}
7080
}
71-
var invalidCoordinates =
72-
source.concat(destination).filter(function (c, i) {
81+
source.concat(destination).filter(function (c, i) {
7382
if (c < 0) {
74-
return true;
83+
throw 'The source and destination coordinates should be above zero';
7584
}
7685
if (i % 2 === 0) {
7786
if (c >= graph.length) {
78-
return true;
87+
throw 'The source and destination coordinates ' +
88+
'should not be above graph\'s size';
7989
}
8090
} else {
8191
if (c >= graph[0].length) {
82-
return true;
92+
throw 'The source and destination coordinates ' +
93+
'should not be above graph\'s size';
8394
}
8495
}
8596
});
86-
if (invalidCoordinates.length) {
87-
return true;
88-
}
89-
return false;
97+
return true;
9098
}
9199

92100
/**
@@ -101,9 +109,7 @@
101109
* a path between the nodes
102110
*/
103111
return function (graph, source, destination) {
104-
if (invalidParams(graph, source, destination)) {
105-
return false;
106-
}
112+
validateParams(graph, source, destination);
107113
init(graph, destination);
108114
var current;
109115
queue.push(source);

test/graphs/searching/bfs.spec.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ describe('BFS', function () {
1616
var graph;
1717

1818
it('should work with incorrect input', function () {
19-
expect(bfs(null, 1, 1)).toBe(false);
20-
expect(bfs(sampleGraph, [-1, -1], [0, 0])).toBe(false);
21-
expect(bfs(sampleGraph, [0, -1], [-1, 0])).toBe(false);
22-
expect(bfs(sampleGraph, [0, 0], [-1, 0])).toBe(false);
23-
expect(bfs(sampleGraph, [0, 1000], [-1, 0])).toBe(false);
24-
expect(bfs(sampleGraph, [100000, 1000], [-1, 0])).toBe(false);
25-
expect(bfs(sampleGraph, [0, 0], [100, 100])).toBe(false);
26-
expect(bfs(sampleGraph, [0, 0], [6, 6])).toBe(false);
19+
expect(function () {
20+
bfs(null, [1, 1], [1, 1]);
21+
}).toThrow();
22+
// expect(bfs(sampleGraph, [-1, -1], [0, 0])).toBe(false);
23+
// expect(bfs(sampleGraph, [0, -1], [-1, 0])).toBe(false);
24+
// expect(bfs(sampleGraph, [0, 0], [-1, 0])).toBe(false);
25+
// expect(bfs(sampleGraph, [0, 1000], [-1, 0])).toBe(false);
26+
// expect(bfs(sampleGraph, [100000, 1000], [-1, 0])).toBe(false);
27+
// expect(bfs(sampleGraph, [0, 0], [100, 100])).toBe(false);
28+
// expect(bfs(sampleGraph, [0, 0], [6, 6])).toBe(false);
2729
});
2830

2931
it('should work with 1x1 matrix', function () {

0 commit comments

Comments
 (0)