/
Graph.ts
74 lines (55 loc) · 1.2 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
class Graph<T> {
nodes: Map<String, Vertex<T>>;
directed: boolean;
constructor() {
this.nodes = new Map<String, Vertex<T>>();
}
getVertex(vertex: String) {
return this.nodes.get(vertex);
}
addVertex(vertex: String, value: T): void {
if (this.nodes.has(vertex)) return;
const newVertex: Vertex<T> = new Vertex<T>(value);
this.nodes.set(vertex, newVertex);
}
removeVertex(vertex: String): T {
if (this.nodes.has(vertex)) {
for (const v of this.nodes.values()) {
}
}
}
addEdge() {
}
removeEdge() {
}
maxDegree() {
}
}
class Vertex<T> {
value: T;
edges: Array<Vertex<T>>;
constructor(value: T) {
this.value = value;
this.edges = [];
}
addAdjacent(vertex: Vertex<T>): void {
this.edges.push(vertex);
}
removeAdjacent(vertex: Vertex<T>): Vertex<T> {
const index = this.edges.indexOf(vertex);
if (index !== -1) {
this.edges.splice(index, 1);
return vertex;
}
}
getAdjacent(): Array<Vertex<T>> {
return this.edges;
}
degree(): number {
return this.edges.length;
}
isAdjacent(vertex: Vertex<T>) {
return this.edges.includes(vertex);
}
}
export default Graph;