Spaces:
Running
Running
File size: 29,838 Bytes
3193174 | 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | """
Service layer over rustworkx algorithms for graph analysis.
Provides:
- K shortest paths
- Centralities (betweenness, closeness, degree, eigenvector, PageRank)
- Community detection
- Cycle detection
- Subgraph filtering by metadata
- Integration with the router
"""
import contextlib
from collections import deque
from collections.abc import Callable
from enum import Enum
from typing import Any
import rustworkx as rx
import torch
from pydantic import BaseModel, ConfigDict, Field
__all__ = [
"CentralityResult",
# Enums
"CentralityType",
"CommunityResult",
"CycleInfo",
# Main service
"GraphAlgorithms",
"PathMetric",
# Data classes
"PathResult",
"SubgraphFilter",
# Utility functions
"compute_all_centralities",
"find_critical_nodes",
"get_graph_metrics",
]
class CentralityType(str, Enum):
"""Centrality types."""
BETWEENNESS = "betweenness"
CLOSENESS = "closeness"
DEGREE = "degree"
EIGENVECTOR = "eigenvector"
PAGERANK = "pagerank"
KATZ = "katz"
class PathMetric(str, Enum):
"""Metric for path computation."""
HOPS = "hops"
WEIGHT = "weight"
LATENCY = "latency"
COST = "cost"
RELIABILITY = "reliability"
class PathResult(BaseModel):
"""Description of a found path with weights and arbitrary metadata."""
nodes: list[str]
total_weight: float
edge_weights: list[float]
metadata: dict[str, Any] = Field(default_factory=dict)
@property
def length(self) -> int:
"""Number of edges in the path."""
return len(self.nodes) - 1 if len(self.nodes) > 1 else 0
def __repr__(self) -> str:
return f"PathResult({' -> '.join(self.nodes)}, weight={self.total_weight:.3f})"
class CentralityResult(BaseModel):
"""Result of centrality computation for graph nodes."""
centrality_type: CentralityType
values: dict[str, float]
normalized: bool = True
def top_k(self, k: int = 5) -> list[tuple[str, float]]:
"""Return the top-k nodes by centrality value."""
sorted_items = sorted(self.values.items(), key=lambda x: x[1], reverse=True)
return sorted_items[:k]
def get_node_rank(self, node_id: str) -> int | None:
"""Node position in the ranking (1-based) or None if absent."""
sorted_nodes = sorted(self.values.keys(), key=lambda n: self.values[n], reverse=True)
try:
return sorted_nodes.index(node_id) + 1
except ValueError:
return None
class CommunityResult(BaseModel):
"""Result of community detection."""
communities: list[set[str]]
modularity: float | None = None
algorithm: str = "unknown"
@property
def num_communities(self) -> int:
"""Number of detected communities."""
return len(self.communities)
def get_node_community(self, node_id: str) -> int | None:
"""Find the community index that the node belongs to."""
for i, community in enumerate(self.communities):
if node_id in community:
return i
return None
def get_community_sizes(self) -> list[int]:
"""Return a list of community sizes."""
return [len(c) for c in self.communities]
class CycleInfo(BaseModel):
"""Information about a detected cycle."""
nodes: list[str]
edges: list[tuple[str, str]]
total_weight: float = 0.0
@property
def length(self) -> int:
"""Number of nodes in the cycle."""
return len(self.nodes)
class SubgraphFilter(BaseModel):
"""Rules for filtering nodes and edges when building a subgraph."""
model_config = ConfigDict(arbitrary_types_allowed=True)
node_filter: Callable[[str, dict[str, Any]], bool] | None = None
edge_filter: Callable[[str, str, dict[str, Any]], bool] | None = None
include_nodes: set[str] | None = None
exclude_nodes: set[str] | None = None
min_weight: float | None = None
max_weight: float | None = None
required_attrs: list[str] | None = None
def matches_node(self, node_id: str, attrs: dict[str, Any]) -> bool:
"""Check whether the node satisfies the given conditions."""
if self.exclude_nodes and node_id in self.exclude_nodes:
return False
if self.include_nodes and node_id not in self.include_nodes:
return False
if self.required_attrs and not all(attr in attrs for attr in self.required_attrs):
return False
return not (self.node_filter and not self.node_filter(node_id, attrs))
def matches_edge(self, src: str, tgt: str, attrs: dict[str, Any]) -> bool:
"""Check whether the edge satisfies the given conditions."""
weight = attrs.get("weight", 1.0)
if self.min_weight is not None and weight < self.min_weight:
return False
if self.max_weight is not None and weight > self.max_weight:
return False
return not (self.edge_filter and not self.edge_filter(src, tgt, attrs))
class GraphAlgorithms:
"""Service layer over `rustworkx` for analysing a `RoleGraph`."""
def __init__(
self,
graph: Any, # RoleGraph
weight_attr: str = "weight",
default_weight: float = 1.0,
):
"""
Initialise the wrapper around the graph.
Args:
graph: A RoleGraph instance (or an object with a `graph: PyDiGraph` attribute).
weight_attr: Key for the edge weight in the data.
default_weight: Weight value when not specified in the edge.
"""
self._role_graph = graph
self._graph: rx.PyDiGraph = graph.graph
self._weight_attr = weight_attr
self._default_weight = default_weight
self._node_id_to_idx: dict[str, int] = {}
self._idx_to_node_id: dict[int, str] = {}
self._rebuild_index_cache()
def _rebuild_index_cache(self) -> None:
"""Rebuild the cache mapping node_id ↔ rustworkx index."""
self._node_id_to_idx.clear()
self._idx_to_node_id.clear()
for idx in self._graph.node_indices():
data = self._graph.get_node_data(idx)
if isinstance(data, dict):
node_id = data.get("id", str(idx))
elif hasattr(data, "agent_id"):
node_id = data.agent_id
else:
node_id = str(idx)
self._node_id_to_idx[node_id] = idx
self._idx_to_node_id[idx] = node_id
def _get_node_idx(self, node_id: str) -> int:
"""Get the node index by ID."""
if node_id not in self._node_id_to_idx:
self._rebuild_index_cache()
if node_id not in self._node_id_to_idx:
msg = f"Node '{node_id}' not found in graph"
raise ValueError(msg)
return self._node_id_to_idx[node_id]
def _get_node_id(self, idx: int) -> str:
"""Get the node ID by its internal graph index."""
if idx not in self._idx_to_node_id:
self._rebuild_index_cache()
return self._idx_to_node_id.get(idx, str(idx))
def _get_edge_weight(
self,
edge_data: Any,
metric: PathMetric = PathMetric.WEIGHT,
) -> float:
"""Get the edge weight according to the selected metric."""
if edge_data is None:
return self._default_weight
if isinstance(edge_data, dict):
if metric == PathMetric.HOPS:
return 1.0
if metric == PathMetric.WEIGHT:
return edge_data.get(self._weight_attr, self._default_weight)
if metric == PathMetric.LATENCY:
return edge_data.get("latency", self._default_weight)
if metric == PathMetric.COST:
return edge_data.get("cost", self._default_weight)
if metric == PathMetric.RELIABILITY:
rel = edge_data.get("reliability", 1.0)
return -torch.log(torch.tensor(max(rel, 1e-10))).item()
return self._default_weight
def k_shortest_paths(
self,
source: str,
target: str,
k: int = 3,
metric: PathMetric = PathMetric.WEIGHT,
) -> list[PathResult]:
"""Find k shortest paths between nodes using the given metric."""
src_idx = self._get_node_idx(source)
tgt_idx = self._get_node_idx(target)
def weight_fn(edge_data: Any) -> float:
return self._get_edge_weight(edge_data, metric)
paths = self._yen_k_shortest_paths(src_idx, tgt_idx, k, weight_fn)
results = []
for path_indices, total_weight in paths:
nodes = [self._get_node_id(idx) for idx in path_indices]
edge_weights = []
for i in range(len(path_indices) - 1):
edge_data = self._graph.get_edge_data(path_indices[i], path_indices[i + 1])
edge_weights.append(self._get_edge_weight(edge_data, metric))
results.append(
PathResult(
nodes=nodes,
total_weight=total_weight,
edge_weights=edge_weights,
metadata={"metric": metric.value},
)
)
return results
def _find_initial_shortest_path(
self,
source: int,
target: int,
weight_fn: Callable[[Any], float],
) -> tuple[list[int], float] | None:
"""Find the first shortest path."""
try:
distances = rx.dijkstra_shortest_path_lengths(self._graph, source, weight_fn)
if target not in distances:
return None
path_map = rx.dijkstra_shortest_paths(self._graph, source, target=target, weight_fn=weight_fn)
if target not in path_map:
return None
return list(path_map[target]), distances[target]
except (ValueError, KeyError, RuntimeError):
return None
def _remove_conflicting_edges(
self,
found_paths: list[tuple[list[int], float]],
root_path: list[int],
j: int,
) -> list[tuple[int, int, Any]]:
"""Remove edges that conflict with the found paths."""
removed_edges = []
for path, _ in found_paths:
if len(path) > j and path[: j + 1] == root_path and j + 1 < len(path):
try:
edge_data = self._graph.get_edge_data(path[j], path[j + 1])
self._graph.remove_edge(path[j], path[j + 1])
removed_edges.append((path[j], path[j + 1], edge_data))
except (ValueError, KeyError, RuntimeError):
pass
return removed_edges
def _calculate_path_weight(self, path: list[int], weight_fn: Callable[[Any], float]) -> float:
"""Compute the total weight of a path."""
total_weight = 0.0
for idx in range(len(path) - 1):
edge_data = self._graph.get_edge_data(path[idx], path[idx + 1])
total_weight += weight_fn(edge_data) if edge_data else self._default_weight
return total_weight
def _find_spur_path(
self,
spur_node: int,
target: int,
root_path: list[int],
weight_fn: Callable[[Any], float],
) -> tuple[list[int], float] | None:
"""Find an alternative path from the spur node."""
try:
spur_distances = rx.dijkstra_shortest_path_lengths(self._graph, spur_node, weight_fn)
if target not in spur_distances:
return None
spur_path_map = rx.dijkstra_shortest_paths(self._graph, spur_node, target=target, weight_fn=weight_fn)
if target not in spur_path_map:
return None
except (ValueError, KeyError, RuntimeError):
return None
else:
spur_path = list(spur_path_map[target])
total_path = root_path[:-1] + spur_path
total_weight = self._calculate_path_weight(total_path, weight_fn)
return total_path, total_weight
def _yen_k_shortest_paths(
self,
source: int,
target: int,
k: int,
weight_fn: Callable[[Any], float],
) -> list[tuple[list[int], float]]:
"""Yen's algorithm: return paths as lists of node indices and total weight."""
import heapq
initial_path = self._find_initial_shortest_path(source, target, weight_fn)
if not initial_path:
return []
first_path, first_weight = initial_path
found_paths = [(first_path, first_weight)]
candidate_heap: list[tuple[float, list[int]]] = []
for i in range(1, k):
if i - 1 >= len(found_paths):
break
prev_path, _ = found_paths[i - 1]
for j in range(len(prev_path) - 1):
spur_node = prev_path[j]
root_path = prev_path[: j + 1]
removed_edges = self._remove_conflicting_edges(found_paths, root_path, j)
try:
spur_result = self._find_spur_path(spur_node, target, root_path, weight_fn)
if spur_result:
total_path, total_weight = spur_result
if not any(p == total_path for _, p in candidate_heap) and not any(
p == total_path for p, _ in found_paths
):
heapq.heappush(candidate_heap, (total_weight, total_path))
finally:
# Restore removed edges in any case
for u, v, data in removed_edges:
self._graph.add_edge(u, v, data)
if candidate_heap:
weight, path = heapq.heappop(candidate_heap)
found_paths.append((path, weight))
return found_paths
def shortest_path(
self,
source: str,
target: str,
metric: PathMetric = PathMetric.WEIGHT,
) -> PathResult | None:
"""Find one shortest path between two nodes."""
paths = self.k_shortest_paths(source, target, k=1, metric=metric)
return paths[0] if paths else None
def all_pairs_shortest_paths(
self,
metric: PathMetric = PathMetric.WEIGHT,
) -> dict[str, dict[str, float]]:
"""Compute shortest paths between all pairs of nodes."""
def weight_fn(edge_data: Any) -> float:
return self._get_edge_weight(edge_data, metric)
all_distances = rx.all_pairs_dijkstra_path_lengths(self._graph, weight_fn)
result = {}
for src_idx, distances in all_distances.items():
src_id = self._get_node_id(src_idx)
result[src_id] = {}
for tgt_idx, dist in distances.items():
tgt_id = self._get_node_id(tgt_idx)
result[src_id][tgt_id] = dist
return result
def compute_centrality(
self,
centrality_type: CentralityType,
normalized: bool = True,
**kwargs: Any,
) -> CentralityResult:
"""Compute the selected centrality type for all graph nodes."""
values: dict[int, int | float] = {}
if centrality_type == CentralityType.BETWEENNESS:
raw_result = rx.betweenness_centrality(self._graph, normalized=normalized)
values = (
dict(raw_result.items())
if hasattr(raw_result, "items")
else dict(enumerate(raw_result))
if isinstance(raw_result, list)
else raw_result
)
elif centrality_type == CentralityType.CLOSENESS:
undirected = self._graph.to_undirected()
raw_values = rx.closeness_centrality(undirected)
values = (
dict(enumerate(raw_values))
if isinstance(raw_values, list)
else dict(raw_values.items())
if hasattr(raw_values, "items")
else raw_values
)
elif centrality_type == CentralityType.DEGREE:
for idx in self._graph.node_indices():
in_deg = self._graph.in_degree(idx)
out_deg = self._graph.out_degree(idx)
values[idx] = float(in_deg + out_deg)
if normalized and self._graph.num_nodes() > 1:
max_deg = 2 * (self._graph.num_nodes() - 1)
values = {k: v / max_deg for k, v in values.items()}
elif centrality_type == CentralityType.EIGENVECTOR:
try:
raw = rx.eigenvector_centrality(self._graph)
values = (
dict(enumerate(raw))
if isinstance(raw, list)
else dict(raw.items())
if hasattr(raw, "items")
else raw
)
except (ValueError, RuntimeError, AttributeError):
raw_pr = rx.pagerank(self._graph)
values = (
dict(raw_pr.items())
if hasattr(raw_pr, "items")
else dict(enumerate(raw_pr))
if isinstance(raw_pr, list)
else raw_pr
)
elif centrality_type == CentralityType.PAGERANK:
alpha = kwargs.get("alpha", 0.85)
raw_pr = rx.pagerank(self._graph, alpha=alpha)
values = (
dict(raw_pr.items())
if hasattr(raw_pr, "items")
else dict(enumerate(raw_pr))
if isinstance(raw_pr, list)
else raw_pr
)
elif centrality_type == CentralityType.KATZ:
alpha = kwargs.get("alpha", 0.1)
beta = kwargs.get("beta", 1.0)
try:
raw_katz = rx.katz_centrality(self._graph, alpha=alpha, beta=beta)
values = (
dict(raw_katz.items())
if hasattr(raw_katz, "items")
else dict(enumerate(raw_katz))
if isinstance(raw_katz, list)
else raw_katz
)
except (ValueError, RuntimeError, AttributeError):
raw_pr = rx.pagerank(self._graph)
values = (
dict(raw_pr.items())
if hasattr(raw_pr, "items")
else dict(enumerate(raw_pr))
if isinstance(raw_pr, list)
else raw_pr
)
result_values = {}
for idx, val in values.items():
node_id = self._get_node_id(idx)
result_values[node_id] = float(val)
return CentralityResult(
centrality_type=centrality_type,
values=result_values,
normalized=normalized,
)
def compute_all_centralities(self, normalized: bool = True) -> dict[CentralityType, CentralityResult]:
"""Compute all centralities and return a dict keyed by type."""
results = {}
for ct in CentralityType:
with contextlib.suppress(Exception):
results[ct] = self.compute_centrality(ct, normalized=normalized)
return results
def detect_communities(
self,
algorithm: str = "louvain",
_resolution: float = 1.0,
) -> CommunityResult:
"""Detect communities using the specified algorithm (louvain/label_propagation)."""
undirected = self._graph.to_undirected()
communities: list[set[str]] = []
modularity: float | None = None
if algorithm == "louvain":
try:
components = rx.connected_components(undirected)
communities = [{self._get_node_id(idx) for idx in comp} for comp in components]
except (ValueError, RuntimeError):
communities = [{self._get_node_id(idx) for idx in undirected.node_indices()}]
elif algorithm == "label_propagation":
communities = self._label_propagation(undirected)
elif algorithm == "connected_components":
components = rx.connected_components(undirected)
communities = [{self._get_node_id(idx) for idx in comp} for comp in components]
else:
components = rx.connected_components(undirected)
communities = [{self._get_node_id(idx) for idx in comp} for comp in components]
return CommunityResult(
communities=communities,
modularity=modularity,
algorithm=algorithm,
)
def _label_propagation(self, graph: rx.PyGraph) -> list[set[str]]:
"""Simple label propagation implementation for an undirected graph."""
import random
labels = {idx: idx for idx in graph.node_indices()}
for _ in range(100):
changed = False
nodes = list(graph.node_indices())
random.shuffle(nodes)
for node in nodes:
neighbors = list(graph.neighbors(node))
if not neighbors:
continue
label_counts: dict[int, int] = {}
for neighbor in neighbors:
lbl = labels[neighbor]
label_counts[lbl] = label_counts.get(lbl, 0) + 1
max_count = max(label_counts.values())
best_labels = [label for label, count in label_counts.items() if count == max_count]
# Use first label if only one, otherwise pick randomly (non-cryptographic use)
new_label = best_labels[0] if len(best_labels) == 1 else random.choice(best_labels)
if labels[node] != new_label:
labels[node] = new_label
changed = True
if not changed:
break
label_to_nodes: dict[int, set[str]] = {}
for node, label in labels.items():
if label not in label_to_nodes:
label_to_nodes[label] = set()
label_to_nodes[label].add(self._get_node_id(node))
return list(label_to_nodes.values())
def detect_cycles(self, max_length: int | None = None) -> list[CycleInfo]:
"""Find simple cycles, optionally limiting the maximum length."""
cycles = []
try:
simple_cycles = rx.simple_cycles(self._graph)
for cycle_indices in simple_cycles:
if max_length and len(cycle_indices) > max_length:
continue
nodes = [self._get_node_id(idx) for idx in cycle_indices]
edges = []
total_weight = 0.0
for i in range(len(cycle_indices)):
src = cycle_indices[i]
tgt = cycle_indices[(i + 1) % len(cycle_indices)]
edges.append((self._get_node_id(src), self._get_node_id(tgt)))
edge_data = self._graph.get_edge_data(src, tgt)
if edge_data and isinstance(edge_data, dict):
total_weight += edge_data.get(self._weight_attr, self._default_weight)
else:
total_weight += self._default_weight
cycles.append(
CycleInfo(
nodes=nodes,
edges=edges,
total_weight=total_weight,
)
)
except (ValueError, RuntimeError):
pass # Cycle detection may fail
return cycles
def is_dag(self) -> bool:
"""Check whether the graph is a directed acyclic graph (DAG)."""
return rx.is_directed_acyclic_graph(self._graph)
def topological_sort(self) -> list[str] | None:
"""Return the topological ordering of nodes if the graph is a DAG."""
if not self.is_dag():
return None
order = rx.topological_sort(self._graph)
return [self._get_node_id(idx) for idx in order]
def filter_subgraph(
self,
filter_spec: SubgraphFilter,
) -> "GraphAlgorithms":
"""Filter nodes/edges by rules and return a wrapper over the subgraph."""
keep_nodes = set()
for idx in self._graph.node_indices():
node_id = self._get_node_id(idx)
node_data = self._graph.get_node_data(idx)
attrs = node_data if isinstance(node_data, dict) else {}
if filter_spec.matches_node(node_id, attrs):
keep_nodes.add(idx)
new_graph = rx.PyDiGraph()
old_to_new: dict[int, int] = {}
for old_idx in keep_nodes:
node_data = self._graph.get_node_data(old_idx)
new_idx = new_graph.add_node(node_data)
old_to_new[old_idx] = new_idx
for edge_idx in self._graph.edge_indices():
src, tgt = self._graph.get_edge_endpoints_by_index(edge_idx)
if src not in keep_nodes or tgt not in keep_nodes:
continue
edge_data = self._graph.get_edge_data_by_index(edge_idx)
attrs = edge_data if isinstance(edge_data, dict) else {}
src_id = self._get_node_id(src)
tgt_id = self._get_node_id(tgt)
if filter_spec.matches_edge(src_id, tgt_id, attrs):
new_graph.add_edge(old_to_new[src], old_to_new[tgt], edge_data)
class SubgraphWrapper:
def __init__(self, g: rx.PyDiGraph):
self.graph = g
return GraphAlgorithms(
SubgraphWrapper(new_graph),
weight_attr=self._weight_attr,
default_weight=self._default_weight,
)
def get_reachable_nodes(self, source: str, max_depth: int | None = None) -> set[str]:
"""Return the set of nodes reachable from source, optionally limited by depth."""
src_idx = self._get_node_idx(source)
visited = set()
queue = deque([(src_idx, 0)])
while queue:
node, depth = queue.popleft()
if node in visited:
continue
if max_depth is not None and depth > max_depth:
continue
visited.add(node)
successors_to_add = [
(successor, depth + 1) for successor in self._graph.successor_indices(node) if successor not in visited
]
queue.extend(successors_to_add)
return {self._get_node_id(idx) for idx in visited}
def get_predecessors(self, node: str, max_depth: int | None = None) -> set[str]:
"""Return the set of predecessors of a node, optionally limited by depth."""
node_idx = self._get_node_idx(node)
visited = set()
queue = deque([(node_idx, 0)])
while queue:
n, depth = queue.popleft()
if n in visited:
continue
if max_depth is not None and depth > max_depth:
continue
visited.add(n)
predecessors_to_add = [
(predecessor, depth + 1)
for predecessor in self._graph.predecessor_indices(n)
if predecessor not in visited
]
queue.extend(predecessors_to_add)
visited.discard(node_idx)
return {self._get_node_id(idx) for idx in visited}
def get_routing_metrics(self, source: str, target: str) -> dict[str, Any]:
"""Collect a brief summary of paths and centrality for a pair of nodes."""
paths_list: list[dict[str, Any]] = []
centrality_dict: dict[str, float] = {}
is_reachable = False
for metric in [PathMetric.WEIGHT, PathMetric.LATENCY, PathMetric.COST]:
try:
paths = self.k_shortest_paths(source, target, k=3, metric=metric)
if paths:
is_reachable = True
paths_list.append(
{
"metric": metric.value,
"best_path": paths[0].nodes,
"best_weight": paths[0].total_weight,
"alternatives": len(paths) - 1,
}
)
except (ValueError, RuntimeError) as e:
from config.logging import logger
logger.debug(f"Error: {e}")
try:
pr = self.compute_centrality(CentralityType.PAGERANK)
centrality_dict["pagerank"] = pr.values.get(target, 0.0)
except (ValueError, RuntimeError):
pass # Centrality computation may fail
return {
"source": source,
"target": target,
"paths": paths_list,
"centrality": centrality_dict,
"is_reachable": is_reachable,
}
def compute_all_centralities(graph: Any) -> dict[str, CentralityResult]:
"""Compute all centrality types and return them by string keys."""
alg = GraphAlgorithms(graph)
results = alg.compute_all_centralities()
return {ct.value: result for ct, result in results.items()}
def find_critical_nodes(graph: Any, top_k: int = 5) -> list[str]:
"""Return the nodes with the highest betweenness centrality."""
alg = GraphAlgorithms(graph)
bc = alg.compute_centrality(CentralityType.BETWEENNESS)
return [node_id for node_id, _ in bc.top_k(top_k)]
def get_graph_metrics(graph: Any) -> dict[str, Any]:
"""Collect key graph metrics: size, DAG status, communities, cycles."""
alg = GraphAlgorithms(graph)
return {
"num_nodes": graph.graph.num_nodes(),
"num_edges": graph.graph.num_edges(),
"is_dag": alg.is_dag(),
"num_communities": alg.detect_communities().num_communities,
"num_cycles": len(alg.detect_cycles(max_length=10)),
}
|