Skip to content

Commit 207eba0

Browse files
committed
add graph breadth first traversal method
1 parent ab58c21 commit 207eba0

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

dist/datastructures/Graph.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const Stack_1 = __importDefault(require("./Stack"));
7+
const Queue_1 = __importDefault(require("./Queue"));
78
;
89
;
910
;
@@ -100,6 +101,28 @@ class Graph {
100101
}
101102
return results;
102103
};
104+
this.breathFirstTraversal = (startingVertex, map = v => v) => {
105+
let results = [];
106+
if (!startingVertex)
107+
return null;
108+
const queue = new Queue_1.default();
109+
const visited = { [startingVertex]: true };
110+
queue.enqueue(startingVertex);
111+
let dequeuedNode;
112+
while (queue.length) {
113+
dequeuedNode = queue.dequeue();
114+
if (dequeuedNode) {
115+
results.push(map(dequeuedNode.data));
116+
for (let neighbour of this.adjacencyList[dequeuedNode.data]) {
117+
if (!visited[neighbour]) {
118+
visited[neighbour] = true;
119+
queue.enqueue(neighbour);
120+
}
121+
}
122+
}
123+
}
124+
return results;
125+
};
103126
this.adjacencyList = {};
104127
// TODO: change directional to boolean and have it be set in constructor
105128
}

dist/datastructures/Graph.test.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ describe('basic graph functionality', () => {
8383
});
8484
});
8585
describe('depth first traversal of graphs', () => {
86-
test('dfs method exists', () => {
86+
test('DFS method exists', () => {
8787
expect(typeof graph.depthFirstTraversal).toBe('function');
8888
});
89-
test('dfs method returns array of all connected vertices', () => {
89+
test('DFS method returns array of all connected vertices', () => {
9090
populateGraph(graph);
9191
let results = graph.depthFirstTraversal('Alice');
9292
expect(results).toEqual(["Alice", "Cheshire Cat", "Mad Hatter", "March Hare", "Dormouse", "Time"]);
@@ -95,7 +95,7 @@ describe('depth first traversal of graphs', () => {
9595
});
9696
});
9797
describe('iterative implementation of DFS', () => {
98-
test('iterative dfs method exists', () => {
98+
test('iterative DFS method exists', () => {
9999
expect(typeof graph.iterativeDFS).toBe('function');
100100
});
101101
test('iterative dfs method returns array of all connected vertices in proper order', () => {
@@ -106,3 +106,19 @@ describe('iterative implementation of DFS', () => {
106106
expect(results).toEqual(["Time", "Mad Hatter", "Dormouse", "March Hare", "Alice", "Cheshire Cat"]);
107107
});
108108
});
109+
describe('breadth first search implementation', () => {
110+
test('BFS method exists', () => {
111+
expect(typeof graph.breathFirstTraversal).toBe('function');
112+
});
113+
test('breath first search returns array of all connected vertices in proper order', () => {
114+
populateGraph(graph);
115+
let results = graph.breathFirstTraversal('Alice');
116+
expect(results).toEqual(['Alice', 'Cheshire Cat', 'Mad Hatter',
117+
'March Hare', 'Dormouse', 'Time']);
118+
// n.b. time comes last as it has a distance of 2
119+
results = graph.breathFirstTraversal('Time');
120+
expect(results).toEqual(['Time', 'Mad Hatter', 'Alice', 'March Hare',
121+
'Dormouse', 'Cheshire Cat']);
122+
// n.b. cheshire cat comes last as it has a distance of 2
123+
});
124+
});

src/datastructures/Graph.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ describe('basic graph functionality', () => {
8484
});
8585

8686
describe('depth first traversal of graphs', () => {
87-
test('dfs method exists', () => {
87+
test('DFS method exists', () => {
8888
expect(typeof graph.depthFirstTraversal).toBe('function');
8989
});
90-
test('dfs method returns array of all connected vertices', () => {
90+
test('DFS method returns array of all connected vertices', () => {
9191
populateGraph(graph);
9292
let results = graph.depthFirstTraversal('Alice');
9393
expect(results).toEqual(["Alice", "Cheshire Cat", "Mad Hatter", "March Hare", "Dormouse", "Time"]);
@@ -97,7 +97,7 @@ describe('depth first traversal of graphs', () => {
9797
});
9898

9999
describe('iterative implementation of DFS', () => {
100-
test('iterative dfs method exists', () => {
100+
test('iterative DFS method exists', () => {
101101
expect(typeof graph.iterativeDFS).toBe('function')
102102
});
103103
test('iterative dfs method returns array of all connected vertices in proper order', () => {
@@ -107,4 +107,21 @@ describe('iterative implementation of DFS', () => {
107107
results = graph.iterativeDFS('Time'); // different route
108108
expect(results).toEqual(["Time", "Mad Hatter", "Dormouse", "March Hare", "Alice", "Cheshire Cat"]);
109109
});
110-
})
110+
});
111+
112+
describe('breadth first search implementation', () => {
113+
test('BFS method exists', () => {
114+
expect(typeof graph.breathFirstTraversal).toBe('function')
115+
});
116+
test('breath first search returns array of all connected vertices in proper order', () => {
117+
populateGraph(graph);
118+
let results = graph.breathFirstTraversal('Alice');
119+
expect(results).toEqual(['Alice', 'Cheshire Cat', 'Mad Hatter',
120+
'March Hare', 'Dormouse', 'Time']);
121+
// n.b. time comes last as it has a distance of 2
122+
results = graph.breathFirstTraversal('Time');
123+
expect(results).toEqual(['Time', 'Mad Hatter', 'Alice', 'March Hare',
124+
'Dormouse', 'Cheshire Cat']);
125+
// n.b. cheshire cat comes last as it has a distance of 2
126+
});
127+
});

src/datastructures/Graph.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Stack from './Stack';
2+
import Queue from './Queue';
23
import _Node from './Node';
34

45
type vertex = string;
@@ -89,7 +90,7 @@ class Graph {
8990
}
9091
helper(vertex);
9192
return results;
92-
}
93+
};
9394

9495
iterativeDFS = (startingVertex: vertex, map: (vertex: vertex) => unknown = v => v): unknown[] => {
9596
let stack = new Stack<string>();
@@ -111,6 +112,28 @@ class Graph {
111112
}
112113
}
113114
return results;
114-
}
115+
};
116+
117+
breathFirstTraversal = (startingVertex: vertex, map: (vertex: vertex) => unknown = v => v): unknown[] | null => {
118+
let results: unknown[] = [];
119+
if (!startingVertex) return null;
120+
const queue = new Queue<vertex>()
121+
const visited: { [vertex: string]: boolean } = { [startingVertex]: true };
122+
queue.enqueue(startingVertex);
123+
let dequeuedNode;
124+
while (queue.length) {
125+
dequeuedNode = queue.dequeue();
126+
if (dequeuedNode) {
127+
results.push(map(dequeuedNode.data));
128+
for (let neighbour of this.adjacencyList[dequeuedNode.data]) {
129+
if (!visited[neighbour]) {
130+
visited[neighbour] = true;
131+
queue.enqueue(neighbour);
132+
}
133+
}
134+
}
135+
}
136+
return results;
137+
};
115138
}
116139
export default Graph;

0 commit comments

Comments
 (0)