Skip to content

Commit 8400d9a

Browse files
committed
Added solution to ch4-1 and an empty spec file. Tests will be written later when I've had a chance to write some helpers to create and validate graph data.
1 parent f8c421b commit 8400d9a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/chapter4/ch4-q01.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
export function isConnectedBFS(graph, source, target) {
4+
let discovered = new Set(),
5+
queue = [source];
6+
7+
while (queue.length > 0) {
8+
let node = queue.shift();
9+
for (let neighbour of graph[node]) {
10+
if (!discovered.has(neighbour)) {
11+
if (neighbour === target) {
12+
return true;
13+
}
14+
discovered.add(neighbour);
15+
queue.push(neighbour);
16+
}
17+
}
18+
}
19+
20+
return false;
21+
}
22+
23+
function dfs(graph, discovered, source, target) {
24+
if (source === target) {
25+
return true;
26+
}
27+
discovered.add(source);
28+
for (let neighbour of graph[source]) {
29+
if (!discovered.has(neighbour)) {
30+
if (dfs(graph, discovered, neighbour, target)) {
31+
return true;
32+
}
33+
}
34+
}
35+
return false;
36+
}
37+
38+
export function isConnectedDFS(graph, source, target) {
39+
return dfs(graph, new Set(), source, target);
40+
}

src/chapter4/ch4-q01.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//import { expect } from 'chai';
2+
//import * as helpers from './helpers';
3+
import * as funcs from './ch4-q01';
4+
5+
for (let key in funcs) {
6+
// let func = funcs[key];
7+
8+
describe('ch4-q01: ' + key, function() {
9+
// TODO
10+
});
11+
12+
}

0 commit comments

Comments
 (0)