Skip to content

Commit 07a314b

Browse files
committed
refactored to factory function to the Graph Data Strucutre
1 parent 82abb1f commit 07a314b

File tree

2 files changed

+77
-60
lines changed

2 files changed

+77
-60
lines changed

11-chapter-Graphs/graph.module.js

+51-59
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,65 @@
33

44
const graphFactory = () => {
55
let graph = {}
6-
let graphProto = {}
76
let vertices = 0
87

9-
graphProto.contains = (node) => !!graph[node]
10-
11-
graphProto.hasEdge = (nodeOne, nodeTwo) => {
12-
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
13-
return !!graph[nodeOne].edges[nodeTwo]
14-
}
15-
}
16-
17-
graphProto.addVertex = (node) => {
18-
if (!graphProto.contains(node)) {
19-
graph[node] = {edges: {}, visited: false}
20-
vertices += 1
21-
}
22-
}
23-
24-
graphProto.removeVertex = (node) => {
25-
if (graphProto.contains(node)) {
26-
for (let item in graph[node].edges) {
27-
if (graph[node].edges.hasOwnProperty(item)) {
28-
graph.removeEdge(node, item)
8+
const graphProto = {
9+
contains: (node) => !!graph[node],
10+
hasEdge: (nodeOne, nodeTwo) => {
11+
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
12+
return !!graph[nodeOne].edges[nodeTwo]
13+
}
14+
},
15+
addVertex: (node) => {
16+
if (!graphProto.contains(node)) {
17+
graph[node] = {edges: {}, visited: false}
18+
vertices += 1
19+
}
20+
},
21+
removeVertex: (node) => {
22+
if (graphProto.contains(node)) {
23+
for (let item in graph[node].edges) {
24+
if (graph[node].edges.hasOwnProperty(item)) {
25+
graph.removeEdge(node, item)
26+
}
2927
}
28+
vertices -= 1
29+
delete graph[node]
3030
}
31-
vertices -= 1
32-
delete graph[node]
33-
}
34-
}
35-
36-
graphProto.addEdge = (nodeOne, nodeTwo) => {
37-
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
38-
graph[nodeOne].edges[nodeTwo] = true
39-
graph[nodeTwo].edges[nodeOne] = true
40-
}
41-
}
42-
43-
graphProto.removeEdge = (nodeOne, nodeTwo) => {
44-
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
45-
delete graph[nodeOne].edges[nodeTwo]
46-
delete graph[nodeTwo].edges[nodeOne]
47-
}
48-
}
49-
50-
graphProto.showGraph = () => {
51-
let show = ''
52-
for (let v in graph) {
53-
show += `${v} -> `
54-
for (let n in graph[v].edges) {
55-
show += n + ', '
31+
},
32+
addEdge: (nodeOne, nodeTwo) => {
33+
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
34+
graph[nodeOne].edges[nodeTwo] = true
35+
graph[nodeTwo].edges[nodeOne] = true
36+
}
37+
},
38+
removeEdge: (nodeOne, nodeTwo) => {
39+
if (graphProto.contains(nodeOne) && graphProto.contains(nodeTwo)) {
40+
delete graph[nodeOne].edges[nodeTwo]
41+
delete graph[nodeTwo].edges[nodeOne]
42+
}
43+
},
44+
showGraph: () => {
45+
let show = ''
46+
for (let v in graph) {
47+
show += `${v} -> `
48+
for (let n in graph[v].edges) {
49+
show += n + ', '
50+
}
51+
show += '\n'
5652
}
57-
show += '\n'
58-
}
59-
console.log(show)
53+
console.log(show)
54+
},
55+
showVertex: (node) => console.log(graphProto.getVertex(node)),
56+
showVertexs: () => console.log(Object.keys(graph)),
57+
getGraph: () => graph,
58+
getVertex: (node) => (graphProto.contains(node)) ? graph[node] : false,
59+
getNumVertices: () => vertices
6060
}
6161

62-
graphProto.showVertex = (node) => console.log(graphProto.getVertex(node))
63-
64-
graphProto.showVertexs = () => console.log(Object.keys(graph))
65-
66-
graphProto.getGraph = () => graph
67-
68-
graphProto.getVertex = (node) => (graphProto.contains(node)) ? graph[node] : false
69-
70-
graphProto.getNumVertices = () => vertices
62+
Object.assign(graphProto, {bfs: bfs.bind(graphProto), dfs: dfs.bind(graphProto)})
7163

72-
return Object.assign(graphProto, {bfs: bfs.bind(graphProto), dfs: dfs.bind(graphProto)})
64+
return Object.create(graphProto)
7365
}
7466

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

11-chapter-Graphs/test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const {graph} = require('./graph.module')
22

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

67
devBook.addVertex('S')
78
devBook.addVertex('A')
@@ -47,4 +48,28 @@ test('Graph Data Structure', assert => {
4748
console.log('BFS() search...')
4849

4950
devBook.bfs()
51+
52+
devBook2.addVertex('F')
53+
devBook2.addVertex('G')
54+
devBook2.addVertex('H')
55+
devBook2.addVertex('I')
56+
devBook2.addVertex('J')
57+
devBook2.addEdge('F', 'G')
58+
devBook2.addEdge('F', 'H')
59+
devBook2.addEdge('F', 'I')
60+
devBook2.addEdge('G', 'J')
61+
devBook2.addEdge('H', 'J')
62+
devBook2.addEdge('I', 'J')
63+
64+
devBook2.showGraph()
65+
66+
devBook2.showVertexs()
67+
68+
console.log('DFS() search...')
69+
70+
devBook2.dfs()
71+
72+
console.log('BFS() search...')
73+
74+
devBook2.bfs()
5075
})

0 commit comments

Comments
 (0)