-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
66 lines (61 loc) · 1.6 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
export interface Graph<X, Y, V> {
verticies: Map<X, V | undefined>;
edges: Map<X | Y, Map<Y | X, V | undefined>>;
adjacent(x: X, y: Y): boolean;
neighbors(x: X): Map<Y, V>;
addVertex(x: X): void;
removeVertex(x: X): void;
getVertexValue(x: X): V;
setVertexValue(x: X, v: V): void;
addEdge(x: X, y: Y): void;
removeEdge(x: X, y: Y): void;
}
export class Graph<X, Y, V> implements Graph<X, Y, V> {
verticies: Map<X, V | undefined> = new Map();
edges: Map<X | Y, Map<Y | X, V | undefined>> = new Map();
adjacent(x: X, y: Y) {
return this.edges.get(x)?.has(y);
}
neighbors(x: X) {
return this.edges.get(x);
}
addVertex(x: X) {
this.verticies.set(x, undefined);
if (!this.edges.has(x)) {
this.edges.set(x, new Map());
}
}
removeVertex(x: X) {
// Remove vertex
this.verticies.delete(x);
// Remove edges for x
this.edges.delete(x);
// Iterate over edges for other verticies and remove x if found.
for (const edge of this.edges.values()) {
if (edge.has(x)) {
edge.delete(x);
}
}
}
getVertexValue(x: X) {
return this.verticies.get(x);
}
setVertexValue(x: X, v: V) {
this.verticies.set(x, v);
}
addEdge(x: X, y: Y) {
// TODO: Add case for Undirected/Bi-directional
this.edges.get(x)?.set(y, undefined);
this.edges.get(y)?.set(x, undefined);
}
removeEdge(x: X, y: Y) {
this.edges.get(x)?.delete(y);
this.edges.get(y)?.delete(x);
}
getEdgeValue(x: X, y: Y) {
return this.edges.get(x)?.get(y);
}
setEdgeValue(x: X, y: Y, v: V) {
return this.edges.get(x)?.set(y, v);
}
}