-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.py
More file actions
463 lines (385 loc) · 14 KB
/
dynamic.py
File metadata and controls
463 lines (385 loc) · 14 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""Dynamic graph algorithms: edge orientation, matching, and weighted MIS."""
from __future__ import annotations
from dataclasses import dataclass
import numpy as np
@dataclass
class DynOrientationResult:
"""Result of a dynamic edge orientation query."""
max_out_degree: int
"""Maximum out-degree across all nodes."""
out_degrees: np.ndarray
"""Out-degree array for all nodes (int32)."""
@dataclass
class DynMatchingResult:
"""Result of a dynamic matching query."""
matching_size: int
"""Number of matched edges."""
matching: np.ndarray
"""Matching array: matching[v] = mate of v, or -1 if unmatched (int32)."""
@dataclass
class DynWMISResult:
"""Result of a dynamic weighted MIS query."""
weight: int
"""Total weight of the independent set."""
vertices: np.ndarray
"""Boolean array: True if vertex is in the independent set."""
class DynEdgeOrientation:
"""Dynamic edge orientation.
Maintains an orientation of edges such that the maximum out-degree
is minimized. Edges can be inserted and deleted incrementally.
Parameters
----------
num_nodes : int
Number of vertices.
algorithm : str, optional
Algorithm name (default ``"kflips"``).
seed : int, optional
Random seed (default 0).
"""
ALGORITHMS: tuple[str, ...] = (
"bfs", "naive_opt", "impro_opt", "kflips", "rwalk", "naive",
"brodal_fagerberg", "max_descending", "strong_opt", "strong_opt_dfs",
"improved_opt", "improved_opt_dfs",
)
"""Valid algorithm names."""
DEFAULT_ALGORITHM: str = "kflips"
def __init__(
self,
num_nodes: int,
algorithm: str = "kflips",
seed: int = 0,
) -> None:
if algorithm not in self.ALGORITHMS:
from chszlablib.exceptions import InvalidModeError
raise InvalidModeError(
f"Unknown algorithm {algorithm!r}. Valid: {self.ALGORITHMS}"
)
self._num_nodes = num_nodes
self._algorithm = algorithm
from chszlablib._dyn_orientation import DynOrientationSolver
self._solver = DynOrientationSolver(num_nodes, algorithm, seed)
def insert_edge(self, u: int, v: int) -> None:
"""Insert an undirected edge (u, v)."""
self._solver.insert_edge(u, v)
def delete_edge(self, u: int, v: int) -> None:
"""Delete an undirected edge (u, v)."""
self._solver.delete_edge(u, v)
def get_current_solution(self) -> DynOrientationResult:
"""Return the current edge orientation solution.
Returns
-------
DynOrientationResult
``max_out_degree`` and ``out_degrees`` array.
"""
return DynOrientationResult(
max_out_degree=self._solver.get_max_out_degree(),
out_degrees=self._solver.get_out_degrees(),
)
class DynDeltaApproxOrientation:
"""Dynamic edge orientation (approximate algorithms).
Maintains an approximate orientation of edges with bounded
maximum out-degree. Edges can be inserted and deleted incrementally.
Parameters
----------
num_nodes : int
Number of vertices.
num_edges_hint : int, optional
Hint for maximum number of edges (default 0, used for memory
pre-allocation in CCHHQRS variants).
algorithm : str, optional
Algorithm name (default ``"improved_bfs"``).
lambda_param : float, optional
Lambda parameter for CCHHQRS variants (default 0.1).
theta : int, optional
Theta parameter for CCHHQRS variants (default 0).
b : int, optional
Fractional edge parameter for CCHHQRS variants (default 1).
bfs_depth : int, optional
BFS depth for BFS-based algorithms (default 20).
"""
ALGORITHMS: tuple[str, ...] = (
"cchhqrs", "limited_bfs", "strong_bfs", "improved_bfs",
"packed_cchhqrs", "packed_cchhqrs_list", "packed_cchhqrs_map",
)
"""Valid algorithm names."""
DEFAULT_ALGORITHM: str = "improved_bfs"
def __init__(
self,
num_nodes: int,
num_edges_hint: int = 0,
algorithm: str = "improved_bfs",
lambda_param: float = 0.1,
theta: int = 0,
b: int = 1,
bfs_depth: int = 20,
) -> None:
if algorithm not in self.ALGORITHMS:
from chszlablib.exceptions import InvalidModeError
raise InvalidModeError(
f"Unknown algorithm {algorithm!r}. Valid: {self.ALGORITHMS}"
)
self._num_nodes = num_nodes
self._algorithm = algorithm
from chszlablib._dyn_delta_approx import DynDeltaApproxSolver
self._solver = DynDeltaApproxSolver(
num_nodes, num_edges_hint, algorithm,
lambda_param, theta, b, bfs_depth,
)
def insert_edge(self, u: int, v: int) -> None:
"""Insert an undirected edge (u, v)."""
self._solver.insert_edge(u, v)
def delete_edge(self, u: int, v: int) -> None:
"""Delete an undirected edge (u, v)."""
self._solver.delete_edge(u, v)
def get_current_solution(self) -> int:
"""Return the current maximum out-degree.
Returns
-------
int
The maximum out-degree in the current orientation.
"""
return self._solver.get_max_out_degree()
class DynMatching:
"""Dynamic graph matching.
Maintains a matching on a dynamic graph where edges can be
inserted and deleted incrementally.
Parameters
----------
num_nodes : int
Number of vertices.
algorithm : str, optional
Algorithm name (default ``"blossom"``).
seed : int, optional
Random seed (default 0).
"""
ALGORITHMS: tuple[str, ...] = (
"random_walk", "baswana_gupta_sen", "neiman_solomon",
"naive", "blossom", "blossom_naive", "static_blossom",
)
"""Valid algorithm names."""
DEFAULT_ALGORITHM: str = "blossom"
def __init__(
self,
num_nodes: int,
algorithm: str = "blossom",
seed: int = 0,
) -> None:
if algorithm not in self.ALGORITHMS:
from chszlablib.exceptions import InvalidModeError
raise InvalidModeError(
f"Unknown algorithm {algorithm!r}. Valid: {self.ALGORITHMS}"
)
self._num_nodes = num_nodes
self._algorithm = algorithm
from chszlablib._dyn_matching import DynMatchingSolver
self._solver = DynMatchingSolver(num_nodes, algorithm, seed)
def insert_edge(self, u: int, v: int) -> None:
"""Insert an undirected edge (u, v)."""
self._solver.insert_edge(u, v)
def delete_edge(self, u: int, v: int) -> None:
"""Delete an undirected edge (u, v)."""
self._solver.delete_edge(u, v)
def get_current_solution(self) -> DynMatchingResult:
"""Return the current matching.
Returns
-------
DynMatchingResult
``matching_size`` and ``matching`` array
(matching[v] = mate or -1).
"""
return DynMatchingResult(
matching_size=self._solver.get_matching_size(),
matching=self._solver.get_matching(),
)
class DynWeightedMIS:
"""Dynamic weighted maximum independent set.
Maintains a weighted independent set on a dynamic graph where
edges can be inserted and deleted incrementally. Node weights
are fixed at construction time.
Parameters
----------
num_nodes : int
Number of vertices.
node_weights : array-like
Node weight array (length num_nodes, int32).
algorithm : str, optional
Algorithm name (default ``"deg_greedy"``).
seed : int, optional
Random seed (default 0).
bfs_depth : int, optional
BFS depth for local algorithms (default 10).
time_limit : float, optional
Time limit for local solver in seconds (default 1000.0).
"""
ALGORITHMS: tuple[str, ...] = (
"simple", "one_fast", "greedy", "deg_greedy", "bfs",
"static", "one_strong",
)
"""Valid algorithm names."""
DEFAULT_ALGORITHM: str = "deg_greedy"
def __init__(
self,
num_nodes: int,
node_weights: np.ndarray | list[int],
algorithm: str = "deg_greedy",
seed: int = 0,
bfs_depth: int = 10,
time_limit: float = 1000.0,
) -> None:
if algorithm not in self.ALGORITHMS:
from chszlablib.exceptions import InvalidModeError
raise InvalidModeError(
f"Unknown algorithm {algorithm!r}. Valid: {self.ALGORITHMS}"
)
self._num_nodes = num_nodes
self._algorithm = algorithm
weights = np.asarray(node_weights, dtype=np.int32)
from chszlablib._dyn_wmis import DynWMISSolver
self._solver = DynWMISSolver(
num_nodes, weights, algorithm, seed, bfs_depth, time_limit,
)
def insert_edge(self, u: int, v: int) -> None:
"""Insert an undirected edge (u, v)."""
self._solver.insert_edge(u, v)
def delete_edge(self, u: int, v: int) -> None:
"""Delete an undirected edge (u, v)."""
self._solver.delete_edge(u, v)
def get_current_solution(self) -> DynWMISResult:
"""Return the current weighted independent set.
Returns
-------
DynWMISResult
``weight`` and boolean ``vertices`` array.
"""
return DynWMISResult(
weight=self._solver.get_weight(),
vertices=self._solver.get_mis(),
)
class DynamicProblems:
"""Dynamic graph algorithms — edge orientation, matching, and weighted MIS.
Non-instantiable namespace providing factory methods for dynamic solvers.
Each solver maintains a solution on a graph where edges can be inserted
and deleted incrementally.
"""
def __new__(cls):
raise TypeError(f"{cls.__name__} is a namespace and cannot be instantiated")
@classmethod
def available_methods(cls) -> dict[str, str]:
"""Return a dict mapping method names to short descriptions."""
return {
"edge_orientation": "Dynamic edge orientation (DynDeltaOrientation)",
"approx_edge_orientation": "Dynamic edge orientation — approximate (DynDeltaApprox)",
"matching": "Dynamic graph matching (DynMatch)",
"weighted_mis": "Dynamic weighted MIS (DynWMIS)",
}
@staticmethod
def edge_orientation(
num_nodes: int,
algorithm: str = "kflips",
seed: int = 0,
) -> DynEdgeOrientation:
"""Create a dynamic edge orientation solver.
Parameters
----------
num_nodes : int
Number of vertices.
algorithm : str, optional
Algorithm name (default ``"kflips"``).
Valid: ``{DynEdgeOrientation.ALGORITHMS}``.
seed : int, optional
Random seed (default 0).
Returns
-------
DynEdgeOrientation
"""
return DynEdgeOrientation(num_nodes, algorithm=algorithm, seed=seed)
@staticmethod
def approx_edge_orientation(
num_nodes: int,
num_edges_hint: int = 0,
algorithm: str = "improved_bfs",
lambda_param: float = 0.1,
theta: int = 0,
b: int = 1,
bfs_depth: int = 20,
) -> DynDeltaApproxOrientation:
"""Create a dynamic edge orientation solver (approximate algorithms).
Parameters
----------
num_nodes : int
Number of vertices.
num_edges_hint : int, optional
Hint for maximum number of edges (default 0).
algorithm : str, optional
Algorithm name (default ``"improved_bfs"``).
Valid: ``{DynDeltaApproxOrientation.ALGORITHMS}``.
lambda_param : float, optional
Lambda parameter for CCHHQRS variants (default 0.1).
theta : int, optional
Theta parameter for CCHHQRS variants (default 0).
b : int, optional
Fractional edge parameter (default 1).
bfs_depth : int, optional
BFS depth for BFS-based algorithms (default 20).
Returns
-------
DynDeltaApproxOrientation
"""
return DynDeltaApproxOrientation(
num_nodes, num_edges_hint=num_edges_hint, algorithm=algorithm,
lambda_param=lambda_param, theta=theta, b=b, bfs_depth=bfs_depth,
)
@staticmethod
def matching(
num_nodes: int,
algorithm: str = "blossom",
seed: int = 0,
) -> DynMatching:
"""Create a dynamic matching solver.
Parameters
----------
num_nodes : int
Number of vertices.
algorithm : str, optional
Algorithm name (default ``"blossom"``).
Valid: ``{DynMatching.ALGORITHMS}``.
seed : int, optional
Random seed (default 0).
Returns
-------
DynMatching
"""
return DynMatching(num_nodes, algorithm=algorithm, seed=seed)
@staticmethod
def weighted_mis(
num_nodes: int,
node_weights: np.ndarray | list[int],
algorithm: str = "deg_greedy",
seed: int = 0,
bfs_depth: int = 10,
time_limit: float = 1000.0,
) -> DynWeightedMIS:
"""Create a dynamic weighted MIS solver.
Parameters
----------
num_nodes : int
Number of vertices.
node_weights : array-like
Node weight array (length num_nodes, int32).
algorithm : str, optional
Algorithm name (default ``"deg_greedy"``).
Valid: ``{DynWeightedMIS.ALGORITHMS}``.
seed : int, optional
Random seed (default 0).
bfs_depth : int, optional
BFS depth for local algorithms (default 10).
time_limit : float, optional
Time limit for local solver in seconds (default 1000.0).
Returns
-------
DynWeightedMIS
"""
return DynWeightedMIS(
num_nodes, node_weights, algorithm=algorithm, seed=seed,
bfs_depth=bfs_depth, time_limit=time_limit,
)