-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathundirected.js
90 lines (78 loc) · 2.15 KB
/
undirected.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
class Graph {
constructor() {
this.vertex = [];
this.edge = [];
this.edgeCounter = 0;
}
addVert(vertex) {
this.vertex.push(vertex);
this.edge[vertex] = []; //all edges
}
addEdge(nodeFrom, nodeTo) {
this.edgeCounter++;
this.edge[nodeFrom].push(nodeTo);
this.edge[nodeTo].push(nodeFrom); //undirected!
}
//returns size of graph
vertexNumber() {
return this.vertex.length;
}
edgeNumber() {
return this.edgeCounter;
}
rmVert(vert) {
const pos = this.vertex.indexOf(vert);
if(pos !== -1) {
this.vertex.splice(pos, 1);
}
while (this.edge[vert].length) {
const second = this.edge[vert].pop();
this.rmEdge(second, vert);
}
}
rmEdge(nodeFrom, nodeTo) {
const pos1 = this.edge[nodeFrom] ? this.edge[nodeFrom].indexOf(nodeTo) : -1;
const pos2 = this.edge[nodeTo] ? this.edge[nodeTo].indexOf(nodeFrom) : -1;
if(pos1 !== -1) {
this.edge[nodeFrom].splice(pos1, 1);
this.edgeCounter--;
}
if(pos2 !== -1) {
this.edge[nodeTo].splice(pos2, 1);
}
}
output() {
let i;
for(i of this.vertex) {
console.dir('Vertex: ' + i);
let j;
for (j of this.edge[i]) {
console.dir('Edge: ' + j);
}
}
console.dir('Graph has ' + this.vertexNumber() + ' nodes');
console.dir('Graph has ' + this.edgeNumber() + ' edges');
}
}
const graph = new Graph();
graph.addVert('first');
graph.addVert('second');
graph.addVert('third');
graph.addVert('fourth');
graph.addVert('fifth');
graph.addVert('sixth');
graph.output();
graph.addEdge('first', 'second');
graph.addEdge('first', 'fifth');
graph.addEdge('fourth', 'fifth');
graph.addEdge('fifth', 'second');
graph.addEdge('third', 'fourth');
graph.addEdge('second', 'third');
graph.addEdge('fourth', 'sixth');
graph.output();
graph.rmEdge('first', 'second');
graph.rmEdge('fourth', 'fifth');
graph.rmEdge('something', '????');
graph.rmVert('fifth');
graph.output();