|
| 1 | +import numpy as np |
| 2 | +from graphblas import Matrix, binary, dtypes, unary |
| 3 | + |
| 4 | +from ..exceptions import GraphBlasAlgorithmException |
| 5 | + |
| 6 | +__all__ = [ |
| 7 | + "compose", |
| 8 | + "difference", |
| 9 | + "disjoint_union", |
| 10 | + "full_join", |
| 11 | + "intersection", |
| 12 | + "symmetric_difference", |
| 13 | + "union", |
| 14 | +] |
| 15 | + |
| 16 | + |
| 17 | +def union(G, H, rename=(), *, name="union"): |
| 18 | + if G.is_multigraph() != H.is_multigraph(): |
| 19 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 20 | + if G.is_multigraph(): |
| 21 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 22 | + if rename: |
| 23 | + prefix = rename[0] |
| 24 | + if prefix is not None: |
| 25 | + G = type(G)( |
| 26 | + G._A, key_to_id={f"{prefix}{key}": val for key, val in G._key_to_id.items()} |
| 27 | + ) |
| 28 | + if len(rename) > 1: |
| 29 | + prefix = rename[1] |
| 30 | + if prefix is not None: |
| 31 | + H = type(H)( |
| 32 | + H._A, key_to_id={f"{prefix}{key}": val for key, val in H._key_to_id.items()} |
| 33 | + ) |
| 34 | + A = G._A |
| 35 | + B = H._A |
| 36 | + if not G._key_to_id.keys().isdisjoint(H._key_to_id.keys()): |
| 37 | + raise GraphBlasAlgorithmException("The node sets of the graphs are not disjoint.") |
| 38 | + C = Matrix(dtypes.unify(A.dtype, B.dtype), A.nrows + B.nrows, A.ncols + B.ncols, name=name) |
| 39 | + C[: A.nrows, : A.ncols] = A |
| 40 | + C[A.nrows :, A.ncols :] = B |
| 41 | + offset = A.nrows |
| 42 | + key_to_id = {key: val + offset for key, val in H._key_to_id.items()} |
| 43 | + key_to_id.update(G._key_to_id) |
| 44 | + return type(G)(C, key_to_id=key_to_id) |
| 45 | + |
| 46 | + |
| 47 | +def disjoint_union(G, H, *, name="disjoint_union"): |
| 48 | + if G.is_multigraph() != H.is_multigraph(): |
| 49 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 50 | + if G.is_multigraph(): |
| 51 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 52 | + A = G._A |
| 53 | + B = H._A |
| 54 | + C = Matrix(dtypes.unify(A.dtype, B.dtype), A.nrows + B.nrows, A.ncols + B.ncols, name=name) |
| 55 | + C[: A.nrows, : A.ncols] = A |
| 56 | + C[A.nrows :, A.ncols :] = B |
| 57 | + return type(G)(C) |
| 58 | + |
| 59 | + |
| 60 | +def intersection(G, H, *, name="intersection"): |
| 61 | + if G.is_multigraph() != H.is_multigraph(): |
| 62 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 63 | + if G.is_multigraph(): |
| 64 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 65 | + keys = sorted(G._key_to_id.keys() & H._key_to_id.keys(), key=G._key_to_id.__getitem__) |
| 66 | + ids = np.array(G.list_to_ids(keys), np.uint64) |
| 67 | + A = G._A[ids, ids].new() |
| 68 | + ids = np.array(H.list_to_ids(keys), np.uint64) |
| 69 | + B = H._A[ids, ids].new(dtypes.unify(A.dtype, H._A.dtype), mask=A.S, name=name) |
| 70 | + B << unary.one(B) |
| 71 | + return type(G)(B, key_to_id=dict(zip(keys, range(len(keys))))) |
| 72 | + |
| 73 | + |
| 74 | +def difference(G, H, *, name="difference"): |
| 75 | + if G.is_multigraph() != H.is_multigraph(): |
| 76 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 77 | + if G.is_multigraph(): |
| 78 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 79 | + if G._key_to_id.keys() != H._key_to_id.keys(): |
| 80 | + raise GraphBlasAlgorithmException("Node sets of graphs not equal") |
| 81 | + A = G._A |
| 82 | + if G._key_to_id == H._key_to_id: |
| 83 | + B = H._A |
| 84 | + else: |
| 85 | + # Need to perform a permutation |
| 86 | + keys = sorted(G._key_to_id, key=G._key_to_id.__getitem__) |
| 87 | + ids = np.array(H.list_to_ids(keys), np.uint64) |
| 88 | + B = H._A[ids, ids].new() |
| 89 | + C = unary.one(A).new(mask=~B.S, name=name) |
| 90 | + return type(G)(C, key_to_id=G._key_to_id) |
| 91 | + |
| 92 | + |
| 93 | +def symmetric_difference(G, H, *, name="symmetric_difference"): |
| 94 | + if G.is_multigraph() != H.is_multigraph(): |
| 95 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 96 | + if G.is_multigraph(): |
| 97 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 98 | + if G._key_to_id.keys() != H._key_to_id.keys(): |
| 99 | + raise GraphBlasAlgorithmException("Node sets of graphs not equal") |
| 100 | + A = G._A |
| 101 | + if G._key_to_id == H._key_to_id: |
| 102 | + B = H._A |
| 103 | + else: |
| 104 | + # Need to perform a permutation |
| 105 | + keys = sorted(G._key_to_id, key=G._key_to_id.__getitem__) |
| 106 | + ids = np.array(H.list_to_ids(keys), np.uint64) |
| 107 | + B = H._A[ids, ids].new() |
| 108 | + Mask = binary.pair[bool](A & B).new(name="mask") |
| 109 | + C = binary.pair(A | B, left_default=True, right_default=True).new(mask=~Mask.S, name=name) |
| 110 | + return type(G)(C, key_to_id=G._key_to_id) |
| 111 | + |
| 112 | + |
| 113 | +def compose(G, H, *, name="compose"): |
| 114 | + if G.is_multigraph() != H.is_multigraph(): |
| 115 | + raise GraphBlasAlgorithmException("All graphs must be graphs or multigraphs.") |
| 116 | + if G.is_multigraph(): |
| 117 | + raise NotImplementedError("Not yet implemented for multigraphs") |
| 118 | + A = G._A |
| 119 | + B = H._A |
| 120 | + if G._key_to_id.keys() == H._key_to_id.keys(): |
| 121 | + if G._key_to_id != H._key_to_id: |
| 122 | + # Need to perform a permutation |
| 123 | + keys = sorted(G._key_to_id, key=G._key_to_id.__getitem__) |
| 124 | + ids = np.array(H.list_to_ids(keys), np.uint64) |
| 125 | + B = B[ids, ids].new() |
| 126 | + C = binary.second(A | B).new(name=name) |
| 127 | + key_to_id = G._key_to_id |
| 128 | + else: |
| 129 | + keys = sorted(G._key_to_id.keys() & H._key_to_id.keys(), key=G._key_to_id.__getitem__) |
| 130 | + B = H._A |
| 131 | + C = Matrix( |
| 132 | + dtypes.unify(A.dtype, B.dtype), |
| 133 | + A.nrows + B.nrows - len(keys), |
| 134 | + A.ncols + B.ncols - len(keys), |
| 135 | + name=name, |
| 136 | + ) |
| 137 | + C[: A.nrows, : A.ncols] = A |
| 138 | + ids1 = np.array(G.list_to_ids(keys), np.uint64) |
| 139 | + ids2 = np.array(H.list_to_ids(keys), np.uint64) |
| 140 | + C[ids1, ids1] = B[ids2, ids2] |
| 141 | + newkeys = sorted(H._key_to_id.keys() - G._key_to_id.keys(), key=H._key_to_id.__getitem__) |
| 142 | + ids = np.array(H.list_to_ids(newkeys), np.uint64) |
| 143 | + C[A.nrows :, A.ncols :] = B[ids, ids] |
| 144 | + # Now make new `key_to_id` |
| 145 | + ids += A.nrows |
| 146 | + key_to_id = dict(zip(newkeys, ids.tolist())) |
| 147 | + key_to_id.update(G._key_to_id) |
| 148 | + return type(G)(C, key_to_id=key_to_id) |
| 149 | + |
| 150 | + |
| 151 | +def full_join(G, H, rename=(), *, name="full_join"): |
| 152 | + rv = union(G, H, rename, name=name) |
| 153 | + nrows, ncols = G._A.shape |
| 154 | + rv._A[:nrows, ncols:] = True |
| 155 | + rv._A[nrows:, :ncols] = True |
| 156 | + return rv |
0 commit comments