-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patharticulationPoint.js
More file actions
100 lines (91 loc) · 2.47 KB
/
articulationPoint.js
File metadata and controls
100 lines (91 loc) · 2.47 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const GraphBase = require('./graph-base');
const Queue = require('./ds/queue');
class ArticulationPoint extends GraphBase {
constructor(numVertices = 0, numEdges = 0, startingPoint = 0) {
super(numVertices, numEdges);
}
findArticulationPoint(graph, v, time) {
graph[v].visited = true;
time++;
graph[v].disc = time;
graph[v].low = time;
var child = 0;
for (var i = 0; i < graph[v].al.length; i++) {
var av = graph[v].al[i];
if (graph[av].visited == false) {
child++;
graph[av].parent = v;
this.findArticulationPoint(graph, av, time);
graph[v].low = Math.min(graph[v].low, graph[av].low);
if (graph[v].parent == -1 && child > 1) {
graph[v].res = true;
} else if (graph[v].parent != -1 && graph[av].low >= graph[v].disc) {
graph[v].res = true;
}
} else if (av != graph[v].parent) {
graph[v].low = Math.min(graph[v].low, graph[av].disc);
}
}
}
ArticulationPointCheck() {
let Adj = this.getSimpleAdj();
var graph = [];
for (var i = 0; i < this.numVertices; i++) {
graph[i] = {
al: [],
visited: false,
disc: 0,
low: 0,
parent: -1,
res: false,
};
}
// building the array of objects
Adj.forEach((value, key) => {
value.forEach((i) => {
if (!graph[key].al.includes(i)) {
graph[key].al.push(i);
}
if (!graph[i].al.includes(key)) {
graph[i].al.push(key);
}
});
});
this.findArticulationPoint(graph, 0, 0);
var res = [];
for (var i = 0; i < this.numVertices; i++) {
if (graph[i].res) {
res.push(i);
}
}
return res;
}
}
// ----------Ex. of not ArticulationPoint graph
// const g = new ArticulationPoint(7);
// vertices = [0, 1, 2, 3,4 ,5,6]
// for (let i = 0; i < vertices.length; i++) {
// g.addVertex(vertices[i]);
// }
// g.addEdge(0, 1);
// g.addEdge(0, 3);
// g.addEdge(1, 2);
// g.addEdge(2, 3);
// g.addEdge(2, 6);
// g.addEdge(3, 4);
// g.addEdge(3, 5);
// g.addEdge(4, 5);
// console.log(g.ArticulationPointCheck());
// ----------Ex. of a ArticulationPoint graph
//const f = new ArticulationPoint(5);
//vertices = [0, 1, 2, 3]
//for (let i = 0; i < vertices.length; i++) {
// f.addVertex(vertices[i]);
//}
//f.addEdge(1,0);
//f.addEdge(0, 2);
//f.addEdge(2,1);
//f.addEdge(0,3);
//f.addEdge(3,4);
//console.log(f.ArticulationPointCheck());
module.exports = ArticulationPoint;