|
3 | 3 |
|
4 | 4 | const graphFactory = () => {
|
5 | 5 | let graph = {}
|
6 |
| - let graphProto = {} |
7 | 6 | let vertices = 0
|
8 | 7 |
|
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 | + } |
29 | 27 | }
|
| 28 | + vertices -= 1 |
| 29 | + delete graph[node] |
30 | 30 | }
|
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' |
56 | 52 | }
|
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 |
60 | 60 | }
|
61 | 61 |
|
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)}) |
71 | 63 |
|
72 |
| - return Object.assign(graphProto, {bfs: bfs.bind(graphProto), dfs: dfs.bind(graphProto)}) |
| 64 | + return Object.create(graphProto) |
73 | 65 | }
|
74 | 66 |
|
75 | 67 | Object.assign(exports, {graph: graphFactory})
|
|
0 commit comments