|
| 1 | +import numpy as np |
| 2 | +from graphblas import Matrix, Vector, binary, monoid, replace, select, unary |
| 3 | +from graphblas.semiring import any_pair, min_plus |
| 4 | + |
| 5 | +from ..exceptions import Unbounded |
| 6 | + |
| 7 | +__all__ = [ |
| 8 | + "single_source_bellman_ford_path_length", |
| 9 | + "bellman_ford_path_lengths", |
| 10 | +] |
| 11 | + |
| 12 | + |
| 13 | +def single_source_bellman_ford_path_length(G, source): |
| 14 | + # No need for `is_weighted=` keyword, b/c this is assumed to be weighted (I think) |
| 15 | + index = G._key_to_id[source] |
| 16 | + if G.get_property("is_iso"): |
| 17 | + # If the edges are iso-valued (and positive), then we can simply do level BFS |
| 18 | + is_negative, iso_value = G.get_properties("has_negative_edges+ iso_value") |
| 19 | + if not is_negative: |
| 20 | + d = _bfs_level(G, source, dtype=iso_value.dtype) |
| 21 | + if iso_value != 1: |
| 22 | + d *= iso_value |
| 23 | + return d |
| 24 | + # It's difficult to detect negative cycles with BFS |
| 25 | + if G._A[index, index].get() is not None: |
| 26 | + raise Unbounded("Negative cycle detected.") |
| 27 | + if not G.is_directed() and G._A[index, :].nvals > 0: |
| 28 | + # For undirected graphs, any negative edge is a cycle |
| 29 | + raise Unbounded("Negative cycle detected.") |
| 30 | + |
| 31 | + # Use `offdiag` instead of `A`, b/c self-loops don't contribute to the result, |
| 32 | + # and negative self-loops are easy negative cycles to avoid. |
| 33 | + # We check if we hit a self-loop negative cycle at the end. |
| 34 | + A, has_negative_diagonal = G.get_properties("offdiag has_negative_diagonal") |
| 35 | + if A.dtype == bool: |
| 36 | + # Should we upcast e.g. INT8 to INT64 as well? |
| 37 | + dtype = int |
| 38 | + else: |
| 39 | + dtype = A.dtype |
| 40 | + n = A.nrows |
| 41 | + d = Vector(dtype, n, name="single_source_bellman_ford_path_length") |
| 42 | + d[index] = 0 |
| 43 | + cur = d.dup(name="cur") |
| 44 | + mask = Vector(bool, n, name="mask") |
| 45 | + one = unary.one[bool] |
| 46 | + for _i in range(n - 1): |
| 47 | + # This is a slightly modified Bellman-Ford algorithm. |
| 48 | + # `cur` is the current frontier of values that improved in the previous iteration. |
| 49 | + # This means that in this iteration we drop values from `cur` that are not better. |
| 50 | + cur << min_plus(cur @ A) |
| 51 | + |
| 52 | + # Mask is True where cur not in d or cur < d |
| 53 | + mask << one(cur) |
| 54 | + mask(binary.second) << binary.lt(cur & d) |
| 55 | + |
| 56 | + # Drop values from `cur` that didn't improve |
| 57 | + cur(mask.V, replace) << cur |
| 58 | + if cur.nvals == 0: |
| 59 | + break |
| 60 | + # Update `d` with values that improved |
| 61 | + d(cur.S) << cur |
| 62 | + else: |
| 63 | + # Check for negative cycle when for loop completes without breaking |
| 64 | + cur << min_plus(cur @ A) |
| 65 | + mask << binary.lt(cur & d) |
| 66 | + if mask.reduce(monoid.lor): |
| 67 | + raise Unbounded("Negative cycle detected.") |
| 68 | + if has_negative_diagonal: |
| 69 | + # We removed diagonal entries above, so check if we visited one with a negative weight |
| 70 | + diag = G.get_property("diag") |
| 71 | + cur << select.valuelt(diag, 0) |
| 72 | + if any_pair(d @ cur): |
| 73 | + raise Unbounded("Negative cycle detected.") |
| 74 | + return d |
| 75 | + |
| 76 | + |
| 77 | +def bellman_ford_path_lengths(G, nodes=None, *, expand_output=False): |
| 78 | + """ |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + expand_output : bool, default False |
| 83 | + When False, the returned Matrix has one row per node in nodes. |
| 84 | + When True, the returned Matrix has the same shape as the input Matrix. |
| 85 | + """ |
| 86 | + # Same algorithms as in `single_source_bellman_ford_path_length`, but with |
| 87 | + # `Cur` as a Matrix with each row corresponding to a source node. |
| 88 | + if G.get_property("is_iso"): |
| 89 | + is_negative, iso_value = G.get_properties("has_negative_edges+ iso_value") |
| 90 | + if not is_negative: |
| 91 | + D = _bfs_levels(G, nodes, dtype=iso_value.dtype) |
| 92 | + if iso_value != 1: |
| 93 | + D *= iso_value |
| 94 | + if nodes is not None and expand_output and D.ncols != D.nrows: |
| 95 | + ids = G.list_to_ids(nodes) |
| 96 | + rv = Matrix(D.dtype, D.ncols, D.ncols, name=D.name) |
| 97 | + rv[ids, :] = D |
| 98 | + return rv |
| 99 | + return D |
| 100 | + if not G.is_directed(): |
| 101 | + # For undirected graphs, any negative edge is a cycle |
| 102 | + if nodes is not None: |
| 103 | + ids = G.list_to_ids(nodes) |
| 104 | + if G._A[ids, :].nvals > 0: |
| 105 | + raise Unbounded("Negative cycle detected.") |
| 106 | + elif G._A.nvals > 0: |
| 107 | + raise Unbounded("Negative cycle detected.") |
| 108 | + |
| 109 | + A, has_negative_diagonal = G.get_properties("offdiag has_negative_diagonal") |
| 110 | + if A.dtype == bool: |
| 111 | + dtype = int |
| 112 | + else: |
| 113 | + dtype = A.dtype |
| 114 | + n = A.nrows |
| 115 | + if nodes is None: |
| 116 | + # TODO: `D = Vector.from_scalar(0, n, dtype).diag()` |
| 117 | + D = Vector(dtype, n, name="bellman_ford_path_lengths_vector") |
| 118 | + D << 0 |
| 119 | + D = D.diag(name="bellman_ford_path_lengths") |
| 120 | + else: |
| 121 | + ids = G.list_to_ids(nodes) |
| 122 | + D = Matrix.from_coo( |
| 123 | + np.arange(len(ids), dtype=np.uint64), |
| 124 | + ids, |
| 125 | + 0, |
| 126 | + dtype, |
| 127 | + nrows=len(ids), |
| 128 | + ncols=n, |
| 129 | + name="bellman_ford_path_lengths", |
| 130 | + ) |
| 131 | + Cur = D.dup(name="Cur") |
| 132 | + Mask = Matrix(bool, D.nrows, D.ncols, name="Mask") |
| 133 | + one = unary.one[bool] |
| 134 | + for _i in range(n - 1): |
| 135 | + Cur << min_plus(Cur @ A) |
| 136 | + Mask << one(Cur) |
| 137 | + Mask(binary.second) << binary.lt(Cur & D) |
| 138 | + Cur(Mask.V, replace) << Cur |
| 139 | + if Cur.nvals == 0: |
| 140 | + break |
| 141 | + D(Cur.S) << Cur |
| 142 | + else: |
| 143 | + Cur << min_plus(Cur @ A) |
| 144 | + Mask << binary.lt(Cur & D) |
| 145 | + if Mask.reduce_scalar(monoid.lor): |
| 146 | + raise Unbounded("Negative cycle detected.") |
| 147 | + if has_negative_diagonal: |
| 148 | + diag = G.get_property("diag") |
| 149 | + cur = select.valuelt(diag, 0) |
| 150 | + if any_pair(D @ cur).nvals > 0: |
| 151 | + raise Unbounded("Negative cycle detected.") |
| 152 | + if nodes is not None and expand_output and D.ncols != D.nrows: |
| 153 | + rv = Matrix(D.dtype, n, n, name=D.name) |
| 154 | + rv[ids, :] = D |
| 155 | + return rv |
| 156 | + return D |
| 157 | + |
| 158 | + |
| 159 | +def _bfs_level(G, source, *, dtype=int): |
| 160 | + if dtype == bool: |
| 161 | + dtype = int |
| 162 | + index = G._key_to_id[source] |
| 163 | + A = G.get_property("offdiag") |
| 164 | + n = A.nrows |
| 165 | + v = Vector(dtype, n, name="bfs_level") |
| 166 | + q = Vector(bool, n, name="q") |
| 167 | + v[index] = 0 |
| 168 | + q[index] = True |
| 169 | + any_pair_bool = any_pair[bool] |
| 170 | + for i in range(1, n): |
| 171 | + q(~v.S, replace) << any_pair_bool(q @ A) |
| 172 | + if q.nvals == 0: |
| 173 | + break |
| 174 | + v(q.S) << i |
| 175 | + return v |
| 176 | + |
| 177 | + |
| 178 | +def _bfs_levels(G, nodes=None, *, dtype=int): |
| 179 | + if dtype == bool: |
| 180 | + dtype = int |
| 181 | + A = G.get_property("offdiag") |
| 182 | + n = A.nrows |
| 183 | + if nodes is None: |
| 184 | + # TODO: `D = Vector.from_scalar(0, n, dtype).diag()` |
| 185 | + D = Vector(dtype, n, name="bfs_levels_vector") |
| 186 | + D << 0 |
| 187 | + D = D.diag(name="bfs_levels") |
| 188 | + else: |
| 189 | + ids = G.list_to_ids(nodes) |
| 190 | + D = Matrix.from_coo( |
| 191 | + np.arange(len(ids), dtype=np.uint64), |
| 192 | + ids, |
| 193 | + 0, |
| 194 | + dtype, |
| 195 | + nrows=len(ids), |
| 196 | + ncols=n, |
| 197 | + name="bfs_levels", |
| 198 | + ) |
| 199 | + Q = Matrix(bool, D.nrows, D.ncols, name="Q") |
| 200 | + Q << unary.one[bool](D) |
| 201 | + any_pair_bool = any_pair[bool] |
| 202 | + for i in range(1, n): |
| 203 | + Q(~D.S, replace) << any_pair_bool(Q @ A) |
| 204 | + if Q.nvals == 0: |
| 205 | + break |
| 206 | + D(Q.S) << i |
| 207 | + return D |
0 commit comments