Skip to content

Commit 21268fd

Browse files
committed
Fix issue in bfs...
1 parent 8fe14d3 commit 21268fd

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/graphs/searching/bfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
function addNode(node) {
3535
if (visited[node] ||
3636
node[0] < 0 || node[1] < 0 ||
37-
node[0] > graph.length || node[1] > graph[0].length ||
38-
!graph[node[0]] || !graph[node[0]]) {
37+
node[0] >= graph.length || node[1] >= graph[0].length ||
38+
!graph[node[0]][node[1]]) {
3939
return;
4040
}
4141
queue.push(node);

test/graphs/searching/bfs.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
var sampleGraph = [[1, 0, 1, 0, 0, 0],
4-
[0, 1, 0, 1, 0, 0],
5-
[1, 0, 1, 0, 1, 0],
3+
var sampleGraph = [[1, 1, 1, 0, 0, 0],
4+
[0, 1, 1, 1, 0, 0],
5+
[1, 0, 1, 1, 1, 0],
66
[0, 1, 0, 1, 1, 0],
77
[0, 1, 0, 1, 1, 0],
88
[0, 1, 0, 1, 1, 0],
@@ -47,4 +47,9 @@ describe('BFS', function () {
4747
expect(bfs(graph, [0, 0], [0, 0])).toBeFalsy();
4848
});
4949

50+
it('should work in the general case', function () {
51+
expect(bfs(sampleGraph, [0, 0], [1, 1])).toBeTruthy();
52+
expect(bfs(sampleGraph, [0, 0], [6, 5])).toBeTruthy();
53+
});
54+
5055
});

0 commit comments

Comments
 (0)