Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ graphblas_algorithms.nxapi
│ │ └── group_out_degree_centrality
│ ├── harmonic
│ │ └── harmonic_centrality
│ └── katz
│ └── katz_centrality
│ ├── katz
│ │ └── katz_centrality
│ └── load
│ ├── edge_load_centrality
│ └── newman_betweenness_centrality
├── cluster
│ ├── average_clustering
│ ├── clustering
Expand Down
4 changes: 4 additions & 0 deletions _nx_graphblas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def get_info():
"edge_betweenness_centrality_subset",
"edge_boundary",
"edge_expansion",
"edge_load_centrality",
"efficiency",
"ego_graph",
"eigenvector_centrality",
Expand Down Expand Up @@ -124,6 +125,7 @@ def get_info():
"modularity_matrix",
"mutual_weight",
"negative_edge_cycle",
"newman_betweenness_centrality",
"node_boundary",
"node_connected_component",
"node_expansion",
Expand Down Expand Up @@ -208,6 +210,7 @@ def get_info():
"eccentricity": 'A precomputed `sp`, and the null graph, are left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"edge_betweenness_centrality": "Unweighted: a call with `weight` is left to NetworkX. With `k`, the sources are drawn\nfrom `seed` as NetworkX draws them, `seed.sample(list(G), k)`, and the scores are\nscaled as NetworkX scales them. The edges come by their first ends in node order, then\nby their second ends in node order, which may differ from the order of `G.edges()`.",
"edge_betweenness_centrality_subset": "Unweighted: a call with `weight` is left to NetworkX. The edges come by their first ends\nin node order, then by their second ends in node order, which may differ from the order\nof `G.edges()`.",
"edge_load_centrality": "The pairs come by their first ends in node order, then by their second ends in node\norder, which may differ from NetworkX's order. A `cutoff` that is not an int or float\nis left to NetworkX.",
"ego_graph": 'With `distance`, lengths are Bellman-Ford\'s, so a negative edge without a negative\ncycle is answered where NetworkX\'s Dijkstra raises "Contradictory paths found", and the\ncall raises OverflowError rather than add integer weights where the number of nodes\ntimes the heaviest absolute weight reaches 2**62, as a length could then pass 2**63.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.',
"floyd_warshall": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"floyd_warshall_predecessor_and_distance": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
Expand All @@ -224,6 +227,7 @@ def get_info():
"k_truss": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"local_efficiency": "The cost is the breadth-first levels times the chunks of walks, so a node whose\nneighbors induce long paths (the hub of a wheel) is no faster than in NetworkX; the\nmemory stays bounded.",
"negative_edge_cycle": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"newman_betweenness_centrality": "With `weight`, served where every edge between two nodes weighs one positive, finite\nvalue, all of one type (int, float or float32), with each self-loop at least as heavy\nand lengths that keep rising to `cutoff`; NetworkX's Dijkstra then finds each level's\npredecessors, ties included. Other weights are left to NetworkX. Int weights add as\nPython ints, which do not wrap past 2**63 where NetworkX's sums of NumPy int64s do.\nNodes that may not compare with `<`, which NetworkX sorts, and a `cutoff` that is not\nan int or float are left to NetworkX.",
"periphery": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"radius": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"reverse": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
Expand Down
14 changes: 14 additions & 0 deletions _nx_graphblas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
"in node order, then by their second ends in node order, which may differ from the order\n"
"of `G.edges()`."
),
"edge_load_centrality": (
"The pairs come by their first ends in node order, then by their second ends in node\n"
"order, which may differ from NetworkX's order. A `cutoff` that is not an int or float\n"
"is left to NetworkX."
),
"ego_graph": (
"With `distance`, lengths are Bellman-Ford's, so a negative edge without a negative\n"
'cycle is answered where NetworkX\'s Dijkstra raises "Contradictory paths found", and the\n'
Expand Down Expand Up @@ -174,6 +179,15 @@
"memory stays bounded."
),
"negative_edge_cycle": _PATH_OVERFLOW,
"newman_betweenness_centrality": (
"With `weight`, served where every edge between two nodes weighs one positive, finite\n"
"value, all of one type (int, float or float32), with each self-loop at least as heavy\n"
"and lengths that keep rising to `cutoff`; NetworkX's Dijkstra then finds each level's\n"
"predecessors, ties included. Other weights are left to NetworkX. Int weights add as\n"
"Python ints, which do not wrap past 2**63 where NetworkX's sums of NumPy int64s do.\n"
"Nodes that may not compare with `<`, which NetworkX sorts, and a `cutoff` that is not\n"
"an int or float are left to NetworkX."
),
"periphery": _ECCENTRICITY_BOUNDS,
"radius": _ECCENTRICITY_BOUNDS,
"schultz_index": 'A graph with a node named "weight" is left to NetworkX.\n'
Expand Down
1 change: 1 addition & 0 deletions graphblas_algorithms/algorithms/centrality/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .group import *
from .harmonic import *
from .katz import *
from .load import *
94 changes: 94 additions & 0 deletions graphblas_algorithms/algorithms/centrality/load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Load centrality over a batch of sources at a time, as NetworkX's load_centrality computes it.

Load is betweenness with each unit a node passes up split equally among its predecessors, the
nodes a level above with an edge to it, rather than by their counts of shortest paths. The walk
is betweenness's, a row per source, with each level holding its nodes' predecessor counts.
"""

from graphblas import Matrix, Vector, binary, monoid, unary
from graphblas.semiring import plus_first, plus_pair

from .betweenness import (
_BYTES_PER_PAIR,
_EDGE_BYTES_PER_PAIR,
_add_edge_credit,
_batches,
_sources,
_walk_matrices,
)

__all__ = ["edge_load_centrality", "load_centrality"]


def load_centrality(G, *, levels=None, chunksize=None):
"""Unnormalized load of each node, summed over every source.

From a source `s`, a node `v` on level `d` passes `1 + L(v)` up, split equally among its
predecessors, where `L(v)` is what `v` got from below; a node on level 1 passes nothing, as
its one predecessor is `s`. `levels` is the most levels walked below each source, None for
all. `chunksize` is the number of sources per batch, None for a batch of about 256 MiB.
"""
A, AT = _walk_matrices(G)
result = Vector(float, A.nrows, name="load_centrality")
for batch in _batches(G, None, chunksize, _BYTES_PER_PAIR):
below = _forward(A, batch, levels)
L = None
for d in range(len(below) - 1, 0, -1):
passed = unary.one[float](below[d]).new()
if L is not None:
passed(binary.plus) << L
share = binary.truediv(passed & below[d]).new()
L = plus_first[float](share @ AT).new(mask=below[d - 1].S)
result(binary.plus) << L.reduce_columnwise(monoid.plus)
return result


def edge_load_centrality(G, *, levels=None, chunksize=None):
"""Load of each ordered pair of nodes an edge joins, either way, summed over every source.

From a source, an edge from a node to one of its successors a level below carries
`g(w) = 1 + U(w) / p(w)` at its lower end `w`, where `p(w)` counts `w`'s predecessors and
`U(w)` sums `g` over the edges below `w`; every other edge carries 1. A pair `(u, v)` with
an edge either way gets what both edges carry, so the Matrix is symmetric, a self-loop at
the number of nodes. `levels` and `chunksize` are as for `load_centrality`.
"""
A, AT = _walk_matrices(G)
n = A.nrows
credit = Matrix(float, n, n, name="edge_load_credit")
for batch in _batches(G, None, chunksize, _EDGE_BYTES_PER_PAIR):
below = _forward(A, batch, levels)
# `g(w) - 1` by level, the credit of each edge into a node on that level.
Y = [None] * len(below)
U = None
for d in range(len(below) - 1, -1, -1):
g = unary.one[float](below[d]).new()
if U is None:
Y[d] = Matrix(float, *below[d].shape)
else:
Y[d] = binary.truediv(U & below[d]).new()
g(binary.plus) << Y[d]
if d > 0:
U = plus_first[float](g @ AT).new(mask=below[d - 1].S)
above = [_sources(batch, n), *(unary.one[float](level).new() for level in below[:-1])]
_add_edge_credit(credit, A, above, Y)
pairs = G._A.dup(float, name="edge_load_centrality")
pairs << binary.any(pairs | pairs.T)
pairs(pairs.S) << float(n)
pairs(binary.plus) << credit
pairs(binary.plus) << credit.T
return pairs


def _forward(A, batch, levels):
"""Each level below the sources, at most `levels`, with each node's count of predecessors."""
level = _sources(batch, A.ncols)
seen = level.dup(name="seen")
seen.ss.config["bitmap_switch"] = 0.01 # as betweenness's `sigma`
below = []
while levels is None or len(below) < levels:
level = plus_pair[float](level @ A).new(mask=~seen.S)
if level.nvals == 0:
break
below.append(level)
seen(binary.plus) << level
return below
2 changes: 2 additions & 0 deletions graphblas_algorithms/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ class Dispatcher:
group_out_degree_centrality = mod.group.group_out_degree_centrality
harmonic_centrality = mod.harmonic.harmonic_centrality
katz_centrality = mod.katz.katz_centrality
edge_load_centrality = mod.load.edge_load_centrality
newman_betweenness_centrality = mod.load.newman_betweenness_centrality

mod = nxapi.cluster
# =================
Expand Down
1 change: 1 addition & 0 deletions graphblas_algorithms/nxapi/centrality/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .group import *
from .harmonic import *
from .katz import *
from .load import *
Loading
Loading