Skip to content

Commit 0593058

Browse files
committed
refactored graph class to module
1 parent 3035b75 commit 0593058

File tree

3 files changed

+91
-40
lines changed

3 files changed

+91
-40
lines changed

11-chapter-Graphs/graph.module.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
(function (exports) {
2+
const {graphSearch} = require('./graph.search')
3+
24
const graphFactory = () => {
35
let graph = {}
46
let graphProto = {}
@@ -59,19 +61,15 @@
5961

6062
graphProto.showVertex = (node) => console.log(graphProto.getVertex(node))
6163

62-
graphProto.showVertexs = () => {
63-
const vertexs = Object.keys(graph)
64-
console.log('Vertexs of the graph')
65-
vertexs.forEach(node => console.log(node))
66-
}
64+
graphProto.showVertexs = () => console.log(Object.keys(graph))
6765

6866
graphProto.getGraph = () => graph
6967

7068
graphProto.getVertex = (node) => (graphProto.contains(node)) ? graph[node] : false
7169

7270
graphProto.getNumVertices = () => vertices
7371

74-
return graphProto
72+
return Object.assign(graphProto, {search: graphSearch.bind(graphProto)})
7573
}
7674

7775
Object.assign(exports, {graph: graphFactory})

11-chapter-Graphs/graph.search.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,67 @@
11
(function (exports) {
22
const {stack} = require('../04-chapter-Stack/')
3+
const {queue} = require('../05-chapter-Queue/')
34
let graph
4-
let listOfVertices
55
let keys
6-
let graphStack
6+
let graphProto
7+
8+
const displayVertex = (node) => console.log(node)
79

810
const getUnvistedVertex = (vertex) => {
9-
for (let i = 0; i < listOfVertices.length; i++) {
10-
if (listOfVertices[i] === false) {
11-
return
11+
for (let node in graph[vertex].edges) {
12+
if (graph[node].visited === false) {
13+
return node
1214
}
1315
}
16+
return false
1417
}
1518

1619
const dfs = () => {
17-
graph.showVertex(keys[0])
18-
listOfVertices[0] = true
20+
let graphStack = stack()
21+
graphProto.getVertex(keys[0]).visited = true
22+
displayVertex(keys[0])
1923
graphStack.push(keys[0])
2024

2125
while (!graphStack.isEmpty()) {
2226
let unvistedVertex = getUnvistedVertex(graphStack.peek())
27+
28+
if (unvistedVertex === false) {
29+
graphStack.pop()
30+
} else {
31+
graph[unvistedVertex].visited = true
32+
displayVertex(unvistedVertex)
33+
graphStack.push(unvistedVertex)
34+
}
2335
}
2436
}
2537

26-
const searchFactory = (g) => {
27-
graph = Object.create(g)
28-
listOfVertices = new Array(graph.getNumVertices())
29-
keys = Object.keys(graph)
30-
graphStack = stack()
31-
return {
32-
dfs
38+
const bfs = () => {
39+
let unvistedVertex
40+
let graphQueue = queue()
41+
graphProto.getVertex(keys[0]).visited = true
42+
displayVertex(keys[0])
43+
graphQueue.enqueue(keys[0])
44+
45+
while (!graphQueue.isEmpty()) {
46+
let tempVertex = graphQueue.dequeue()
47+
unvistedVertex = getUnvistedVertex(tempVertex)
48+
49+
while (unvistedVertex !== false) {
50+
graphProto.getVertex(unvistedVertex).visited = true
51+
displayVertex(unvistedVertex)
52+
graphQueue.enqueue(unvistedVertex)
53+
unvistedVertex = getUnvistedVertex(tempVertex)
54+
}
3355
}
3456
}
3557

36-
Object.assign(exports, {search: searchFactory})
58+
function searchFactory (g) {
59+
graphProto = this
60+
graph = g
61+
keys = Object.keys(graph)
62+
63+
return Object.assign({}, {dfs, bfs})
64+
}
65+
66+
Object.assign(exports, {graphSearch: searchFactory})
3767
}((typeof module.exports !== undefined) ? module.exports : window))

11-chapter-Graphs/test.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,62 @@ const {graph} = require('./graph.module')
22

33
test('Graph Data Structure', assert => {
44
const devBook = Object.create(graph())
5+
const devBook2 = Object.create(graph())
56

6-
devBook.addVertex('James Gosling')
7-
devBook.addVertex('Guido Rossum')
8-
devBook.addVertex('Linus Torvalds')
9-
devBook.addVertex('Cristian Ramirez')
7+
devBook.addVertex('S')
8+
devBook.addVertex('A')
9+
devBook.addVertex('B')
10+
devBook.addVertex('C')
11+
devBook.addVertex('D')
12+
devBook.addVertex('E')
1013

11-
assert.equal(!!devBook.getVertex('Linus Torvalds'), true, 'adds vertex')
14+
assert.equal(!!devBook.getVertex('A'), true, 'adds vertex')
1215

13-
devBook.addEdge('James Gosling', 'Guido Rossum')
14-
devBook.addEdge('James Gosling', 'Cristian Ramirez')
15-
devBook.addEdge('Linus Torvalds', 'Cristian Ramirez')
16+
devBook.addEdge('S', 'A')
17+
devBook.addEdge('S', 'B')
18+
devBook.addEdge('S', 'C')
19+
devBook.addEdge('A', 'D')
20+
devBook.addEdge('B', 'D')
21+
devBook.addEdge('C', 'D')
22+
devBook.addEdge('A', 'E')
1623

17-
assert.equal(devBook.getVertex('Linus Torvalds').edges['Cristian Ramirez'], true, 'adds edges')
24+
const vertex = devBook.getVertex('A')
1825

19-
devBook.removeEdge('Linus Torvalds', 'Cristian Ramirez')
26+
assert.equal(vertex, devBook.getVertex('A'), 'get vertex object')
2027

21-
assert.equal(!!devBook.getVertex('Linus Torvalds').edges['Cristian Ramirez'], false, 'removes an edge')
28+
assert.equal(devBook.getVertex('C').edges['D'], true, 'adds edges')
2229

23-
devBook.removeVertex('Linus Torvalds')
30+
devBook.removeEdge('A', 'E')
2431

25-
assert.equal(devBook.getVertex('Linus Torvalds'), false, 'removes a vertex')
32+
assert.equal(!!devBook.getVertex('A').edges['E'], false, 'removes an edge')
2633

27-
assert.ok(devBook.hasEdge('James Gosling', 'Guido Rossum'), 'verify has edge')
34+
devBook.removeVertex('E')
2835

29-
devBook.showGraph()
36+
assert.equal(devBook.getVertex('E'), false, 'removes a vertex')
37+
38+
assert.ok(devBook.hasEdge('B', 'D'), 'verify has edge')
3039

31-
console.log(JSON.stringify(devBook.getGraph(), null, 2))
40+
devBook.showGraph()
3241

3342
devBook.showVertexs()
3443

35-
// devBook.showVertex('James Gosling')
44+
console.log('DFS() search...')
45+
46+
devBook.search(devBook.getGraph()).dfs()
47+
48+
devBook2.addVertex('S')
49+
devBook2.addVertex('A')
50+
devBook2.addVertex('B')
51+
devBook2.addVertex('C')
52+
devBook2.addVertex('D')
53+
devBook2.addEdge('S', 'A')
54+
devBook2.addEdge('S', 'B')
55+
devBook2.addEdge('S', 'C')
56+
devBook2.addEdge('A', 'D')
57+
devBook2.addEdge('B', 'D')
58+
devBook2.addEdge('C', 'D')
3659

37-
const vertex = devBook.getVertex('Cristian Ramirez')
60+
console.log('BFS() search...')
3861

39-
assert.equal(vertex, devBook.getVertex('Cristian Ramirez'), 'get vertex object')
62+
devBook2.search(devBook2.getGraph()).bfs()
4063
})

0 commit comments

Comments
 (0)