-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
184 lines (158 loc) · 3.72 KB
/
index.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
182
183
184
import { IGraph } from "../types/datastructures.types";
import { GraphObject } from "../types/nodes.types";
/**
* @class Graph
*/
export class Graph implements IGraph {
_graph: GraphObject;
vertices: number;
unidirectional: boolean;
weighted: boolean;
constructor(isUniDirectional = false, isWeighted = false) {
this._graph = {};
this.vertices = 0;
this.unidirectional = isUniDirectional;
this.weighted = isWeighted;
}
/**
* Add a new vertex to the graph
* @param vertex
* @returns self reference
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
```
*/
addVertex(vertex: string): Graph {
if (this._graph[vertex]) throw "Vertex already exists.";
this._graph[vertex] = {};
this.vertices += 1;
return this;
}
/**
* Add edge between 2 existing vertices
* @param u
* @param v
* @param w only pass a value if the isWeighted flag is true
* @returns self reference
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.addEdge("a", "b");
// or
const g = new Graph(false, true);
g.addVertex("a").addVertex("b");
g.addEdge("a", "b", 5);
```
*/
addEdge(u: string, v: string, w: number = 1): Graph {
if (!this.weighted && w != 1)
throw "Can not pass weight to unweighted graph.";
if (!this._graph[u] || !this._graph[v])
throw "Can not add edge between undefined edges.";
let W = 1;
if (this.weighted) W = w;
if (!this.unidirectional) this._graph[v][u] = W;
this._graph[u][v] = W;
return this;
}
/**
* Remove a vertex from the graph with all of it's links with other vertices
* @param vertex
* @returns self reference
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.addEdge("a", "b");
g.removeVertex("a");
```
*/
removeVertex(vertex: string): Graph {
if (!this._graph[vertex])
throw "Can not remove a vertex that does not exist.";
delete this._graph[vertex];
for (const [k, v] of Object.entries(this._graph)) {
for (const [k2, v2] of Object.entries(this._graph[k])) {
if (k2 == vertex) delete this._graph[k][k2];
}
}
this.vertices -= 1;
return this;
}
/**
* Remove edge between 2 vertices if the graph is directional it will remove 1 way
* @param u
* @param v
* @returns self reference
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.addEdge("a", "b");
g.removeEdge("a", "b")
```
*/
removeEdge(u: string, v: string): Graph {
if (!this._graph[u] || !this._graph[u][v])
throw "Can not remove a non-existent edge.";
delete this._graph[u][v];
if (!this.unidirectional) delete this._graph[v][u];
return this;
}
/**
* retrieve the neighbors of a vertex
* @param vertex
* @returns
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.addEdge("a", "b");
g.getNeighbors("a"); // {b:1}
```
*/
getNeighbors(vertex: string): GraphObject[keyof GraphObject] {
if (!this._graph[vertex]) throw "Can not get a non-existent vertex";
return this._graph[vertex];
}
/**
* Prints a visual representation of the graph
* @returns
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.addEdge("a", "b");
g.view();
// a => [b, 1] :
// b => [a, 1] :
```
*/
view(): Graph {
let log = `Graph contains ${this.vertices} vertices.\n\n`;
for (const [k, v] of Object.entries(this._graph)) {
let link = "";
for (const [k2, v2] of Object.entries(this._graph[k]))
link += `[${k2}, ${v2}] : `;
log += `${k} => ${link} \n`;
}
console.log(log);
return this;
}
/**
* Get the current number of vertices in the graph
* @returns
* @example
```ts
const g = new Graph();
g.addVertex("a").addVertex("b");
g.getVerticesNumbers(); //2
```
*/
getVerticesNumbers(): number {
return this.vertices;
}
}