forked from Graph-Visualization/graph-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toposort_cycle_find.js
158 lines (121 loc) · 3.73 KB
/
toposort_cycle_find.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const GraphBase = require('./graph-base');
const Queue = require('./ds/queue');
class FindCycle extends GraphBase {
constructor(numVertices = 0, numEdges = 0, isDirected = false) {
super(numVertices, numEdges);
this.isDirected = isDirected;
}
findCycle() {
let isCyclic = false;
let Adj = this.getSimpleAdj();
if (this.isDirected) {
let sorted_list = this.toposort();
let pos = [];
for (let i = 0; i < sorted_list.length; i++) {
let node = sorted_list[i];
pos[node] = i;
}
Adj.forEach(function (children, parent) {
for (let i = 0; i < children.length; i++) {
let child = children[i];
if (pos[parent] > pos[child]) {
isCyclic = true;
}
if (isCyclic) break;
}
});
} else {
let visited = [],
vertices = [],
parent = [];
Adj.forEach(function (neighbours, vertex) {
visited[vertex] = false;
parent[vertex] = -1;
vertices.push(vertex);
});
for (let i = 0; i < vertices.length; i++) {
let current_vertex = vertices[i];
if (visited[current_vertex]) continue;
let queue = new Queue();
visited[current_vertex] = true;
queue.enqueue(current_vertex);
while (!queue.isEmpty()) {
let vertex = queue.dequeue();
for (let i = 0; i < Adj.get(vertex).length; i++) {
let next_vertex = Adj.get(vertex)[i];
if (!visited[next_vertex]) {
visited[next_vertex] = true;
queue.enqueue(next_vertex);
parent[next_vertex] = vertex;
} else if (parent[vertex] != next_vertex) {
isCyclic = true;
break;
}
}
if (isCyclic) break;
}
if (isCyclic) break;
}
}
return isCyclic;
}
toposort() {
let Adj = this.getSimpleAdj();
let visited = [];
let vertices = [];
Adj.forEach(function (neighbours, vertex) {
visited[vertex] = false;
vertices.push(vertex);
});
let topo_stack = [];
for (let i = 0; i < vertices.length; i++) {
let current_vertex = vertices[i];
if (!visited[current_vertex]) {
dfs_recursive(Adj, current_vertex, visited, topo_stack);
}
}
let sorted_list = [];
while (topo_stack.length) {
sorted_list.push(topo_stack.pop());
}
return sorted_list;
}
}
function dfs_recursive(Adj, current_vertex, visited, topo_stack = []) {
visited[current_vertex] = true;
for (let i = 0; i < Adj.get(current_vertex).length; i++) {
let next_vertex = Adj.get(current_vertex)[i];
if (!visited[next_vertex]) {
dfs_recursive(Adj, next_vertex, visited, topo_stack);
}
}
topo_stack.push(current_vertex);
}
/***************** TEST RUN **********************/
const input_file = require('./testfiles/test1.json');
const fs = require('fs');
output_data = new Map();
for (let testcase = 0; testcase < input_file.length; testcase++) {
let input = input_file[testcase];
const g = new FindCycle(input.numVertices, input.numEdges, input.isDirected);
vertices = input.vertices;
for (let i = 0; i < vertices.length; i++) {
g.addVertex(vertices[i]);
}
for (let i = 0; i < input.numEdges; i++) {
let edge = input.edges[i];
g.addEdge(edge[0], edge[1]);
if (input.isDirected == false) {
g.addEdge(edge[1], edge[0]);
//only add when undirected.
}
}
// console.log("Output For TestCase " + (testcase+1)+ " : g.findCycle()")
output_data.set('Testcase ' + testcase, g.findCycle());
}
// console.log(output_data)
fs.writeFileSync(
'testfiles/output.json',
JSON.stringify(Array.from(output_data))
);
module.exports = FindCycle;