-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES6_ClassGraph.js
More file actions
74 lines (65 loc) · 1.5 KB
/
ES6_ClassGraph.js
File metadata and controls
74 lines (65 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const expect = require('expect');
class Graph {
constructor() {
this.vertices = [];
this.edges = [];
this.numberOfEdges = 0;
}
addVertex(vertex) {
this.vertices.push(vertex);
this.edges[vertex] = [];
}
addEdge(v1, v2) {
this.edges[v1].push(v2);
this.edges[v2].push(v1);
this.numberOfEdges++;
}
traverseDFS(vertex, cb) {
let visited = [];
if (!this.vertices.includes(vertex)) {
return console.log('Vertex Not Found');
}
this._traverseDFS(vertex, visited, cb);
}
_traverseDFS(vertex, visited, cb) {
visited[vertex] = true;
if(this.edges[vertex]) {
cb(vertex)
}
this.edges[vertex].forEach(v => {
if(!visited[v]) {
this._traverseDFS(v, visited, cb)
}
})
}
relations() {
return this.numberOfEdges;
}
print() {
return (this.vertices.map(v => {
return (v + ' -> ' + this.edges[v].join(', ')).trim();
}, this).join(' | '));
}
}
const testGraph = () => {
const graph = new Graph();
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
graph.addVertex(4);
graph.addVertex(5);
graph.addVertex(6);
graph.addEdge(1, 2);
graph.addEdge(1, 5);
graph.addEdge(2, 3);
graph.addEdge(2, 5);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
graph.addEdge(4, 6);
graph.traverseDFS(1, (x) => console.log(x));
expect(
graph.print()
).toEqual('1 -> 2, 5 | 2 -> 1, 3, 5 | 3 -> 2, 4 | 4 -> 3, 5, 6 | 5 -> 1, 2, 4 | 6 -> 4');
};
testGraph();
console.log('All tests passed');