cds2.graph
Graph algorithms on scipy.sparse.csgraph plus PageRank.
Graph algorithms built on scipy.sparse.csgraph, plus PageRank.
DegreeResult
dataclass
In/out/total degrees of each node.
Source code in src/cds2/graph.py
67 68 69 70 71 72 73 | |
ComponentResult
dataclass
Connected-component labeling of a graph.
Source code in src/cds2/graph.py
76 77 78 79 80 81 | |
CentralityResult
dataclass
Per-node centrality scores.
Source code in src/cds2/graph.py
84 85 86 87 88 | |
CommunityResult
dataclass
Label-propagation community detection output.
Source code in src/cds2/graph.py
91 92 93 94 95 96 97 | |
from_edges(n, edges, weighted=False, directed=True)
Build a CSR adjacency matrix from an edge list.
Unweighted edges get weight 1. Undirected graphs mirror every edge.
Source code in src/cds2/graph.py
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 | |
degree(adj, directed=True)
Node degrees from a CSR/COO adjacency matrix.
Source code in src/cds2/graph.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
connected_components(adj, directed=False, connection='weak')
Label the connected components of a graph.
Source code in src/cds2/graph.py
156 157 158 159 160 161 162 163 | |
shortest_paths(adj, indices=None, method='dijkstra', unweighted=False)
Pairwise shortest paths from indices to all nodes (Dijkstra or Bellman-Ford).
Source code in src/cds2/graph.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
single_source_shortest_paths(adj, source, method='dijkstra')
Distance vector from one node to all others.
Source code in src/cds2/graph.py
182 183 184 185 | |
floyd_warshall_paths(adj)
All-pairs shortest paths via the Floyd-Warshall algorithm.
Source code in src/cds2/graph.py
188 189 190 191 | |
bellman_ford_paths(adj, indices=None)
Shortest paths tolerating negative weights (no negative cycles).
Source code in src/cds2/graph.py
194 195 196 | |
minimum_spanning_forest(adj)
Minimum spanning tree/forest of an undirected weighted graph.
Source code in src/cds2/graph.py
199 200 201 202 203 | |
pagerank(adj, damping=0.85, max_iter=100, tol=1e-10)
PageRank scores for a finite square graph with non-negative weights.
Source code in src/cds2/graph.py
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 | |
topological_order(n, edges)
Kahn topological order of a DAG; raises ValueError when a cycle exists.
Source code in src/cds2/graph.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
betweenness_centrality(adj, normalized=True)
Brandes betweenness centrality on the unweighted successor graph.
Directed graphs use n=1 pairs; undirected graphs divide by two.
Source code in src/cds2/graph.py
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 | |
directed_flag(matrix)
True when the matrix is not symmetric.
Source code in src/cds2/graph.py
359 360 361 362 | |
closeness_centrality(adj, wf_improved=True)
Wasserman-Faust closeness centrality from single-source distances.
Source code in src/cds2/graph.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
eigenvector_centrality(adj, max_iter=200, tol=1e-10, directed=None)
Eigenvector centrality of the largest (Perron) eigenvalue.
Power iteration with an oscillation guard: bipartite graphs that fail to settle fall back to a Lanczos solve for the dominant eigenvector.
Source code in src/cds2/graph.py
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 | |
detect_communities(adj, max_sweeps=50, seed=None)
Label-propagation communities with modularity as the quality score.
Deterministic given seed; ties in the neighbour-label vote are broken
by the rng so repeated calls with the same seed reproduce exactly.
Source code in src/cds2/graph.py
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 | |
modularity(adj, labels)
Newman modularity Q of a partition on an undirected graph.
Source code in src/cds2/graph.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |