|
| 1 | +"""BFS routines used by other algorithms""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from graphblas import Matrix, Vector, binary, replace, unary |
| 5 | +from graphblas.semiring import any_pair |
| 6 | + |
| 7 | + |
| 8 | +def _get_cutoff(n, cutoff): |
| 9 | + if cutoff is None or cutoff >= n: |
| 10 | + return n # Everything |
| 11 | + return cutoff + 1 # Inclusive |
| 12 | + |
| 13 | + |
| 14 | +def _plain_bfs(G, source, *, cutoff=None): |
| 15 | + index = G._key_to_id[source] |
| 16 | + A = G.get_property("offdiag") |
| 17 | + n = A.nrows |
| 18 | + v = Vector(bool, n, name="bfs_plain") |
| 19 | + q = Vector(bool, n, name="q") |
| 20 | + v[index] = True |
| 21 | + q[index] = True |
| 22 | + any_pair_bool = any_pair[bool] |
| 23 | + cutoff = _get_cutoff(n, cutoff) |
| 24 | + for _i in range(1, cutoff): |
| 25 | + q(~v.S, replace) << any_pair_bool(q @ A) |
| 26 | + if q.nvals == 0: |
| 27 | + break |
| 28 | + v(q.S) << True |
| 29 | + return v |
| 30 | + |
| 31 | + |
| 32 | +def _bfs_level(G, source, cutoff=None, *, transpose=False, dtype=int): |
| 33 | + if dtype == bool: |
| 34 | + dtype = int |
| 35 | + index = G._key_to_id[source] |
| 36 | + A = G.get_property("offdiag") |
| 37 | + if transpose and G.is_directed(): |
| 38 | + A = A.T # TODO: should we use "AT" instead? |
| 39 | + n = A.nrows |
| 40 | + v = Vector(dtype, n, name="bfs_level") |
| 41 | + q = Vector(bool, n, name="q") |
| 42 | + v[index] = 0 |
| 43 | + q[index] = True |
| 44 | + any_pair_bool = any_pair[bool] |
| 45 | + cutoff = _get_cutoff(n, cutoff) |
| 46 | + for i in range(1, cutoff): |
| 47 | + q(~v.S, replace) << any_pair_bool(q @ A) |
| 48 | + if q.nvals == 0: |
| 49 | + break |
| 50 | + v(q.S) << i |
| 51 | + return v |
| 52 | + |
| 53 | + |
| 54 | +def _bfs_levels(G, nodes, cutoff=None, *, dtype=int): |
| 55 | + if dtype == bool: |
| 56 | + dtype = int |
| 57 | + A = G.get_property("offdiag") |
| 58 | + n = A.nrows |
| 59 | + if nodes is None: |
| 60 | + # TODO: `D = Vector.from_scalar(0, n, dtype).diag()` |
| 61 | + D = Vector(dtype, n, name="bfs_levels_vector") |
| 62 | + D << 0 |
| 63 | + D = D.diag(name="bfs_levels") |
| 64 | + else: |
| 65 | + ids = G.list_to_ids(nodes) |
| 66 | + D = Matrix.from_coo( |
| 67 | + np.arange(len(ids), dtype=np.uint64), |
| 68 | + ids, |
| 69 | + 0, |
| 70 | + dtype, |
| 71 | + nrows=len(ids), |
| 72 | + ncols=n, |
| 73 | + name="bfs_levels", |
| 74 | + ) |
| 75 | + Q = unary.one[bool](D).new(name="Q") |
| 76 | + any_pair_bool = any_pair[bool] |
| 77 | + cutoff = _get_cutoff(n, cutoff) |
| 78 | + for i in range(1, cutoff): |
| 79 | + Q(~D.S, replace) << any_pair_bool(Q @ A) |
| 80 | + if Q.nvals == 0: |
| 81 | + break |
| 82 | + D(Q.S) << i |
| 83 | + return D |
| 84 | + |
| 85 | + |
| 86 | +# TODO: benchmark this and the version commented out below |
| 87 | +def _plain_bfs_bidirectional(G, source): |
| 88 | + # Bi-directional BFS w/o symmetrizing the adjacency matrix |
| 89 | + index = G._key_to_id[source] |
| 90 | + A = G.get_property("offdiag") |
| 91 | + # XXX: should we use `AT` if available? |
| 92 | + n = A.nrows |
| 93 | + v = Vector(bool, n, name="bfs_plain") |
| 94 | + q_out = Vector(bool, n, name="q_out") |
| 95 | + q_in = Vector(bool, n, name="q_in") |
| 96 | + v[index] = True |
| 97 | + q_in[index] = True |
| 98 | + any_pair_bool = any_pair[bool] |
| 99 | + is_out_empty = True |
| 100 | + is_in_empty = False |
| 101 | + for _i in range(1, n): |
| 102 | + # Traverse out-edges from the most recent `q_in` and `q_out` |
| 103 | + if is_out_empty: |
| 104 | + q_out(~v.S) << any_pair_bool(q_in @ A) |
| 105 | + else: |
| 106 | + q_out << binary.any(q_out | q_in) |
| 107 | + q_out(~v.S, replace) << any_pair_bool(q_out @ A) |
| 108 | + is_out_empty = q_out.nvals == 0 |
| 109 | + if not is_out_empty: |
| 110 | + v(q_out.S) << True |
| 111 | + elif is_in_empty: |
| 112 | + break |
| 113 | + # Traverse in-edges from the most recent `q_in` and `q_out` |
| 114 | + if is_in_empty: |
| 115 | + q_in(~v.S) << any_pair_bool(A @ q_out) |
| 116 | + else: |
| 117 | + q_in << binary.any(q_out | q_in) |
| 118 | + q_in(~v.S, replace) << any_pair_bool(A @ q_in) |
| 119 | + is_in_empty = q_in.nvals == 0 |
| 120 | + if not is_in_empty: |
| 121 | + v(q_in.S) << True |
| 122 | + elif is_out_empty: |
| 123 | + break |
| 124 | + return v |
| 125 | + |
| 126 | + |
| 127 | +""" |
| 128 | +def _plain_bfs_bidirectional(G, source): |
| 129 | + # Bi-directional BFS w/o symmetrizing the adjacency matrix |
| 130 | + index = G._key_to_id[source] |
| 131 | + A = G.get_property("offdiag") |
| 132 | + n = A.nrows |
| 133 | + v = Vector(bool, n, name="bfs_plain") |
| 134 | + q = Vector(bool, n, name="q") |
| 135 | + q2 = Vector(bool, n, name="q_2") |
| 136 | + v[index] = True |
| 137 | + q[index] = True |
| 138 | + any_pair_bool = any_pair[bool] |
| 139 | + for _i in range(1, n): |
| 140 | + q2(~v.S, replace) << any_pair_bool(q @ A) |
| 141 | + v(q2.S) << True |
| 142 | + q(~v.S, replace) << any_pair_bool(A @ q) |
| 143 | + if q.nvals == 0: |
| 144 | + if q2.nvals == 0: |
| 145 | + break |
| 146 | + q, q2 = q2, q |
| 147 | + elif q2.nvals != 0: |
| 148 | + q << binary.any(q | q2) |
| 149 | + return v |
| 150 | +""" |
0 commit comments