-
Notifications
You must be signed in to change notification settings - Fork 21
/
adjacency-list.ts
172 lines (152 loc) · 4.41 KB
/
adjacency-list.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
import type {
AdjacencyStructure,
AdjacencyStructureConstructor,
IndexedCollection,
TypedArrayConstructors,
} from "./utility-types.ts";
/**
* Creates an Adjacency List class extending a given TypedArray class.
*
* @param Base a TypedArray class to extend
*/
export function AdjacencyListMixin<U extends TypedArrayConstructors>(
Base: U,
): AdjacencyStructureConstructor<U> {
// deno-lint-ignore no-empty-interface
interface AdjacencyList extends IndexedCollection {}
/**
* Implements the Adjacency List data structure for weighted directed graphs.
*/
class AdjacencyList extends Base implements AdjacencyStructure {
static directed = true;
static weighted = true;
vertices = 2;
edges = 4;
empty = undefined;
// deno-lint-ignore no-explicit-any
constructor(...args: any[]) {
super(...args);
[this.vertices, this.edges] = AdjacencyList.getDimensions(this);
}
static get [Symbol.species](): U {
return Base;
}
static create(vertices: number, edges: number) {
const length = this.getLength(vertices, edges);
const list = new this(length);
list.vertices = vertices;
list.edges = edges;
list.setOffsets();
return list as unknown as AdjacencyList & InstanceType<U>;
}
/*
* Returns the dimensions, vertices and maximum edge count, of an existing AdjacencyList
*/
static getDimensions(
list: IndexedCollection,
): [vertices: number, edges: number] {
let vertices = 0;
while (list[vertices] <= list[vertices + 1]) {
vertices++;
}
const edges = (list.length - vertices - 1) >> 1;
return [vertices, edges];
}
static getLength(vertices: number, edges: number): number {
return vertices + (edges << 1) + 1;
}
// TODO document RangeError
addEdge(x: number, y: number, weight: number): this {
if (this.hasEdge(x, y)) return this;
// the list is full
if (this.isFull()) throw new RangeError("The list is full.");
const { vertices } = this;
// shift values
for (let i = this[vertices]; i > this[x]; i -= 2) {
[this[i], this[i + 1], this[i - 1], this[i - 2]] = [
this[i - 2],
this[i - 1],
this[i],
this[i + 1],
];
}
// set edge
this[this[x]] = y;
this[this[x] + 1] = weight;
// update offsets
for (let i = x + 1; i <= vertices; i++) {
this[i] += 2;
}
return this;
}
getEdge(x: number, y: number): number {
const offset = this[x];
const nextOffset = this[x + 1];
// no out edges from x
if (offset === nextOffset) return NaN;
for (let i = offset; i < nextOffset; i += 2) {
if (this[i] === y) return this[i + 1];
}
return NaN;
}
hasEdge(x: number, y: number): boolean {
return !Number.isNaN(this.getEdge(x, y));
}
*inEdges(vertex: number) {
const { vertices } = this;
let edge = 0;
let nextVertex = 1;
for (let i = vertices + 1; i < this[vertices]; i += 2) {
while (i >= this[nextVertex]) {
edge++;
nextVertex++;
}
if (this[i] === vertex) yield edge;
}
}
isFull(): boolean {
return this[this.vertices] >= this.length;
}
*outEdges(vertex: number) {
const offset = this[vertex];
const nextOffset = this[vertex + 1];
if (offset !== nextOffset) {
for (let i = nextOffset - 2; i >= offset; i -= 2) {
yield this[i];
}
}
}
removeEdge(x: number, y: number): this {
const offset = this[x];
const nextOffset = this[x + 1];
// no out edges from x
if (offset === nextOffset) return this;
let edgeIndex = 0;
for (let i = offset; i < nextOffset; i += 2) {
if (this[i] === y) {
edgeIndex = i;
break;
}
}
// there is no such edge
if (!edgeIndex) return this;
// shift value
for (let i = edgeIndex; i < this[this.vertices]; i += 2) {
this[i] = this[i + 2];
this[i + 1] = this[i + 3];
}
// update offsets
for (let i = x + 1; i <= this.vertices; i++) {
this[i] -= 2;
}
return this;
}
setOffsets(): void {
const lastElement = this.vertices + 1;
for (let i = 0; i < lastElement; i++) {
this[i] = lastElement;
}
}
}
return AdjacencyList;
}