-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
227 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Dictionary = require('../dictionary/dictionary'); | ||
const Queue = require('../queue/Queue'); | ||
|
||
function Graph() { | ||
let vertices = []; | ||
let adjList = new Dictionary(); | ||
|
||
this.addVertex = (v) => { | ||
vertices.push(v); | ||
adjList.set(v, []); | ||
}; | ||
|
||
this.addEdge = (v, w) => { | ||
adjList.get(v).push(w); | ||
adjList.get(w).push(v); | ||
} | ||
|
||
this.getEdges = (vertex) => { | ||
return adjList.get(vertex); | ||
}; | ||
|
||
//white = not visited | ||
//grey = visited not explored | ||
//black = completely explored | ||
let initializeColor = () => { | ||
let color = []; | ||
|
||
vertices.forEach((vertex, i) => { | ||
color[vertices[i]] = 'white'; | ||
}); | ||
|
||
return color; | ||
}; | ||
|
||
//breadth-first search | ||
this.bfs = (v, callback) => { | ||
let color = initializeColor(), | ||
queue = Queue(); | ||
|
||
queue.enqueue(v); | ||
|
||
while(!queue.isEmpty()) { | ||
let u = queue.dequeue(); | ||
let neighbors = adjList.get(u); | ||
|
||
color[u] = 'grey'; | ||
|
||
for (let i = 0; i < neighbors.length; i++) { | ||
let w = neighbors[i]; | ||
|
||
if (color[w] === 'white') { | ||
color[w] = 'grey'; | ||
queue.enqueue(w); | ||
} | ||
} | ||
|
||
color[u] = 'black'; | ||
|
||
if (callback){ | ||
callback(u); | ||
} | ||
} | ||
}; | ||
|
||
} | ||
|
||
module.exports = Graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const test = require('ava'); | ||
const Graph = require('./graph'); | ||
|
||
test('add vertex', t => { | ||
const graph = new Graph(); | ||
|
||
const myVertices = [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I' | ||
]; | ||
|
||
myVertices.forEach(vertex => { | ||
graph.addVertex(vertex); | ||
}); | ||
|
||
graph.addEdge('A', 'B'); | ||
graph.addEdge('A', 'C'); | ||
graph.addEdge('A', 'D'); | ||
graph.addEdge('C', 'D'); | ||
graph.addEdge('C', 'G'); | ||
graph.addEdge('D', 'G'); | ||
graph.addEdge('D', 'H'); | ||
graph.addEdge('B', 'E'); | ||
graph.addEdge('B', 'F'); | ||
graph.addEdge('E', 'I'); | ||
|
||
t.deepEqual(graph.getEdges('A'), ['B', 'C', 'D']); | ||
}); | ||
|
||
test('breadth-first traversal', t => { | ||
const graph = new Graph(); | ||
|
||
const myVertices = [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I' | ||
]; | ||
|
||
myVertices.forEach(vertex => { | ||
graph.addVertex(vertex); | ||
}); | ||
|
||
graph.addEdge('A', 'B'); | ||
graph.addEdge('A', 'C'); | ||
graph.addEdge('A', 'D'); | ||
graph.addEdge('C', 'D'); | ||
graph.addEdge('C', 'G'); | ||
graph.addEdge('D', 'G'); | ||
graph.addEdge('D', 'H'); | ||
graph.addEdge('B', 'E'); | ||
graph.addEdge('B', 'F'); | ||
graph.addEdge('E', 'I'); | ||
|
||
let result = []; | ||
function printVertices(vertex) { | ||
result.push(vertex) | ||
} | ||
|
||
graph.bfs('A', printVertices) | ||
|
||
t.deepEqual(result, [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I' | ||
]) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters