-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.ts
181 lines (162 loc) · 4.41 KB
/
Graph.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class Vertex {
label: string;
adjacencyList: [];
constructor(label) {
this.label = label;
this.adjacencyList = [];
}
}
class Graph {
map = {};
addNode(label: string) {
const vertex = new Vertex(label);
if (!this.map[label]) this.map[label] = vertex;
}
addEdge(from: string, to: string) {
if (from === to) return;
// check if the vertex exsit
if (!this.map[from] || !this.map[to]) return false;
this.map[from].adjacencyList.push(to);
}
print() {
for (const node in this.map) {
if (Object.prototype.hasOwnProperty.call(this.map, node)) {
console.log(
`${node} is Connected with [${this.map[
node
].adjacencyList.toString()}]`
);
}
}
}
removeNode(label: string) {
if (!this.map[label]) return false;
delete this.map[label];
for (let n in this.map) {
let adjacencyList = this.map[n].adjacencyList;
const idx = adjacencyList.indexOf(label);
adjacencyList.splice(idx, 1);
}
}
removeEdge(from: string, to: string) {
if (from == to) return;
if (!this.map[from] || !this.map[from]) return;
let adjacencyList = this.map[from].adjacencyList;
let idx = adjacencyList.indexOf(to);
if (idx === -1) return false;
adjacencyList.splice(idx, 1);
return true;
}
DepthFirstTraversal(root) {
let node = this.map[root];
if (!node) return;
const set = new Set();
this._DepthFirstTraversal(node, set);
console.log(set);
return set;
}
_DepthFirstTraversal(node, set) {
set.add(node.label);
let adjacencyList = node.adjacencyList;
if (adjacencyList.length === 0 || !node || set.has(adjacencyList[0]))
return;
let nextNode = this.map[adjacencyList[0]];
this._DepthFirstTraversal(nextNode, set);
}
DepthStack(root) {
let node = this.map[root];
if (!node) return;
let stack = [];
let visited = new Set();
stack.push(node);
while (stack.length > 0) {
let current = stack.pop();
visited.add(current.label);
for (const node of current.adjacencyList) {
if (visited.has(node)) continue;
stack.push(this.map[node]);
}
}
console.log(visited);
return visited;
}
BreathFirstSearch(root) {
let node = this.map[root];
if (node == null) return;
let visited = new Set();
let queue = [];
queue.push(node);
while (queue.length > 0) {
let current = queue.shift();
visited.add(current.label);
for (let n of current.adjacencyList) {
if (visited.has(n)) continue;
let nextNode = this.map[n];
queue.push(nextNode);
}
}
console.log(visited);
return visited;
}
topologicalSorting() {
let visited = new Set();
let stack = [];
for (let n in this.map) {
this._topologicalSorting(this.map[n], visited, stack);
}
let sorted = [];
while (stack.length > 0) {
sorted.push(stack.pop());
}
return sorted;
}
_topologicalSorting(node: Vertex, visited: any, stack: string[]) {
if (visited.has(node.label)) return;
visited.add(node.label);
let adjacencyList = node.adjacencyList;
for (let n in adjacencyList) {
this._topologicalSorting(this.map[adjacencyList[n]], visited, stack);
}
// we are in the very end
stack.push(node.label);
}
hasCycle() {
let visiting = new Set();
let visited = new Set();
for (let n in this.map) {
const isCycle = this._hasCycle(this.map[n], visiting, visited);
if (isCycle) return true;
}
console.log(visited);
console.log(visiting);
return false;
}
_hasCycle(node: Vertex, visiting, visited) {
visiting.add(node.label);
let adjacencyList = node.adjacencyList;
for (let n of adjacencyList) {
if (visited.has(n)) continue;
if (visiting.has(n)) return true;
if (this._hasCycle(this.map[n], visiting, visited)) return true;
}
visited.add(node.label);
visiting.delete(node.label);
return false;
}
}
const graph = new Graph();
graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
graph.addEdge('C', 'A');
// graph.removeNode('Jone');
// console.log(graph.removeEdge('Jone', 'Doe'));
// graph.DepthFirstTraversal('C');
// graph.DepthStack('A');
// graph.BreathFirstSearch('A');
// graph.topologicalSorting();
console.log(graph.hasCycle());
// graph.print();
// console.log(graph);