DirectedGraph [有向图]
# 有向图
边为有方向的图称作有向图(英語:directed graph 或 digraph)。
# 实现
# JavaScript
const Queue = require('@datastructures-js/queue');
/**
* @class
*/
class DirectedGraph {
constructor() {
this._vertices = new Map();
this._edges = new Map();
this._edgesCount = 0;
}
/**
* Adds a vertex to the graph
* @public
* @param {number|string} key
* @param {object} value
* @return {DirectedGraph}
*/
addVertex(key, value) {
this._vertices.set(key, value);
if (!this._edges.has(key)) {
this._edges.set(key, new Map());
}
return this;
}
/**
* Checks if the graph has a vertex
* @public
* @param {number|string} key
* @return {boolean}
*/
hasVertex(key) {
return this._vertices.has(key);
}
/**
* Removes a vertex and all its edges from the graph
* @public
* @param {number|string} key
* @return {boolean}
*/
removeVertex(key) {
if (!this.hasVertex(key)) return false;
this.removeEdges(key);
this._edges.delete(key);
this._vertices.delete(key);
return true;
}
/**
* Returns the number of vertices in the graph
* @public
* @return {number}
*/
getVerticesCount() {
return this._vertices.size;
}
/**
* Adds a directed edge from a source vertex to a destination
* @public
* @param {number|string} srcKey
* @param {number|string} destKey
* @param {number} [weight] - default 1
*/
addEdge(srcKey, destKey, weight) {
if (!this._vertices.has(srcKey)) {
throw new Error(`addEdge: vertex "${srcKey}" not found`);
}
if (!this._vertices.has(destKey)) {
throw new Error(`addEdge: vertex "${destKey}" not found`);
}
if (weight && Number.isNaN(+weight)) {
throw new Error('addEdge: expects a numberic weight');
}
const w = Number.isNaN(+weight) ? 1 : +weight;
this._edges.get(srcKey).set(destKey, w);
this._edgesCount += 1;
return this;
}
/**
* Checks if there is a direction between two nodes
* @public
* @param {number|string} srcKey
* @param {number|string} destKey
* @returns {boolean}
*/
hasEdge(srcKey, destKey) {
return this.hasVertex(srcKey)
&& this.hasVertex(destKey)
&& this._edges.get(srcKey).has(destKey);
}
/**
* Gets the weight of an edge if exists
* @public
* @param {number|string} srcKey
* @param {number|string} destKey
* @returns {number}
*/
getWeight(srcKey, destKey) {
if (this.hasVertex(srcKey) && srcKey === destKey) {
return 0;
}
if (!this.hasEdge(srcKey, destKey)) {
return Infinity;
}
return this._edges.get(srcKey).get(destKey);
}
/**
* Removes the direction from source to destination
* @public
* @param {number|string} srcKey
* @param {number|string} destKey
*/
removeEdge(srcKey, destKey) {
if (!this.hasEdge(srcKey, destKey)) {
return false;
}
this._edges.get(srcKey).delete(destKey);
this._edgesCount -= 1;
return true;
}
/**
* Removes in and out directions of a vertex
* @public
* @param {number|string} key
* @return {number} number of removed edges
*/
removeEdges(key) {
if (!this.hasVertex(key)) {
return 0;
}
let removedEdgesCount = 0;
this._edges.forEach((destEdges, srcKey) => {
if (destEdges.has(key)) {
this.removeEdge(srcKey, key);
removedEdgesCount += 1;
}
});
removedEdgesCount += this._edges.get(key).size;
this._edgesCount -= this._edges.get(key).size;
this._edges.set(key, new Map());
return removedEdgesCount;
}
/**
* Returns the number of edges in the graph
* @public
* @returns {number}
*/
getEdgesCount() {
return this._edgesCount;
}
/**
* Traverse all vertices in the graph using depth-first search
* @public
* @param {number|string} srcKey - starting node
* @param {function} cb
*/
traverseDfs(srcKey, cb) {
const traverseDfsRecursive = (key, visited = new Set()) => {
if (!this.hasVertex(key) || visited.has(key)) return;
cb(key, this._vertices.get(key));
visited.add(key);
this._edges.get(key).forEach((weight, destKey) => {
traverseDfsRecursive(destKey, visited);
});
};
traverseDfsRecursive(srcKey);
}
/**
* Traverse all vertices in the graph using breadth-first search
* @public
* @param {number|string} srcKey - starting node
* @param {function} cb
*/
traverseBfs(srcKey, cb) {
if (!this.hasVertex(srcKey)) return;
const queue = new Queue([srcKey]);
const visited = new Set([srcKey]);
while (!queue.isEmpty()) {
const nextKey = queue.dequeue();
cb(nextKey, this._vertices.get(nextKey));
this._edges.get(nextKey).forEach((weight, destKey) => {
if (!visited.has(destKey)) {
queue.enqueue(destKey);
visited.add(destKey);
}
});
}
}
/**
* Clears the graph
* @public
*/
clear() {
this._vertices = new Map();
this._edges = new Map();
this._edgesCount = 0;
}
}
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 参考
编辑 (opens new window)
上次更新: 2022/10/13, 15:33:21