|
| 1 | +from graphblas import monoid |
| 2 | +from graphblas.semiring import any_pair, plus_first |
| 3 | + |
| 4 | +from graphblas_algorithms.classes.digraph import to_graph |
| 5 | +from graphblas_algorithms.utils import get_all |
| 6 | + |
| 7 | +from .boundary import edge_boundary_core, node_boundary_core |
| 8 | + |
| 9 | + |
| 10 | +def cut_size_core(G, S, T=None, *, is_weighted=False): |
| 11 | + edges = edge_boundary_core(G, S, T, is_weighted=is_weighted) |
| 12 | + if is_weighted: |
| 13 | + rv = edges.reduce_scalar(monoid.plus).get(0) |
| 14 | + else: |
| 15 | + rv = edges.nvals |
| 16 | + if G.is_directed(): |
| 17 | + edges = edge_boundary_core(G, T, S, is_weighted=is_weighted) |
| 18 | + if is_weighted: |
| 19 | + rv += edges.reduce_scalar(monoid.plus).get(0) |
| 20 | + else: |
| 21 | + rv += edges.nvals |
| 22 | + return rv |
| 23 | + |
| 24 | + |
| 25 | +def cut_size(G, S, T=None, weight=None): |
| 26 | + is_multigraph = G.is_multigraph() |
| 27 | + G = to_graph(G, weight=weight) |
| 28 | + S = G.set_to_vector(S, ignore_extra=True) |
| 29 | + T = G.set_to_vector(T, ignore_extra=True) |
| 30 | + return cut_size_core(G, S, T, is_weighted=is_multigraph or weight is not None) |
| 31 | + |
| 32 | + |
| 33 | +def volume_core(G, S, *, weighted=False): |
| 34 | + if weighted: |
| 35 | + degrees = plus_first(G._A @ S) |
| 36 | + else: |
| 37 | + degrees = G.get_property("row_degrees+", mask=S.S) |
| 38 | + return degrees.reduce(monoid.plus).get(0) |
| 39 | + |
| 40 | + |
| 41 | +def volume(G, S, weight=None): |
| 42 | + is_multigraph = G.is_multigraph() |
| 43 | + G = to_graph(G, weight=weight) |
| 44 | + S = G.list_to_vector(S) |
| 45 | + return volume_core(G, S, weighted=is_multigraph or weight is not None) |
| 46 | + |
| 47 | + |
| 48 | +def normalized_cut_size_core(G, S, T=None): |
| 49 | + num_cut_edges = cut_size_core(G, S, T) |
| 50 | + volume_S = volume_core(G, S) |
| 51 | + volume_T = volume_core(G, T) |
| 52 | + return num_cut_edges * ((1 / volume_S) + (1 / volume_T)) |
| 53 | + |
| 54 | + |
| 55 | +def normalized_cut_size(G, S, T=None, weight=None): |
| 56 | + G = to_graph(G, weight=weight) |
| 57 | + S = G.set_to_vector(S, ignore_extra=True) |
| 58 | + if T is None: |
| 59 | + T = (~S.S).new() |
| 60 | + else: |
| 61 | + T = G.set_to_vector(T, ignore_extra=True) |
| 62 | + return normalized_cut_size_core(G, S, T) |
| 63 | + |
| 64 | + |
| 65 | +def conductance_core(G, S, T=None): |
| 66 | + num_cut_edges = cut_size_core(G, S, T) |
| 67 | + volume_S = volume_core(G, S) |
| 68 | + volume_T = volume_core(G, T) |
| 69 | + return num_cut_edges / min(volume_S, volume_T) |
| 70 | + |
| 71 | + |
| 72 | +def conductance(G, S, T=None, weight=None): |
| 73 | + G = to_graph(G, weight=weight) |
| 74 | + S = G.set_to_vector(S, ignore_extra=True) |
| 75 | + if T is None: |
| 76 | + T = (~S.S).new() |
| 77 | + else: |
| 78 | + T = G.set_to_vector(T, ignore_extra=True) |
| 79 | + return conductance_core(G, S, T) |
| 80 | + |
| 81 | + |
| 82 | +def edge_expansion_core(G, S, T=None): |
| 83 | + num_cut_edges = cut_size_core(G, S, T) |
| 84 | + if T is None: |
| 85 | + Tnvals = S.size - S.nvals |
| 86 | + else: |
| 87 | + Tnvals = T.nvals |
| 88 | + return num_cut_edges / min(S.nvals, Tnvals) |
| 89 | + |
| 90 | + |
| 91 | +def edge_expansion(G, S, T=None, weight=None): |
| 92 | + G = to_graph(G, weight=weight) |
| 93 | + S = G.set_to_vector(S, ignore_extra=True) |
| 94 | + T = G.set_to_vector(T, ignore_extra=True) |
| 95 | + return edge_expansion_core(G, S, T) |
| 96 | + |
| 97 | + |
| 98 | +def mixing_expansion_core(G, S, T=None): |
| 99 | + num_cut_edges = cut_size_core(G, S, T) |
| 100 | + return num_cut_edges / G._A.nvals # Why no factor of 2 in denominator? |
| 101 | + |
| 102 | + |
| 103 | +def mixing_expansion(G, S, T=None, weight=None): |
| 104 | + G = to_graph(G, weight=weight) |
| 105 | + S = G.set_to_vector(S, ignore_extra=True) |
| 106 | + T = G.set_to_vector(T, ignore_extra=True) |
| 107 | + return mixing_expansion_core(G, S, T) |
| 108 | + |
| 109 | + |
| 110 | +def node_expansion_core(G, S): |
| 111 | + neighborhood = any_pair(G._A.T @ S) |
| 112 | + return neighborhood.nvals / S.nvals |
| 113 | + |
| 114 | + |
| 115 | +def node_expansion(G, S): |
| 116 | + G = to_graph(G) |
| 117 | + S = G.list_to_vector(S) |
| 118 | + return node_expansion_core(G, S) |
| 119 | + |
| 120 | + |
| 121 | +def boundary_expansion_core(G, S): |
| 122 | + result = node_boundary_core(G, S) |
| 123 | + return result.nvals / S.nvals |
| 124 | + |
| 125 | + |
| 126 | +def boundary_expansion(G, S): |
| 127 | + G = to_graph(G) |
| 128 | + S = G.set_to_vector(S, ignore_extra=True) |
| 129 | + return boundary_expansion_core(G, S) |
| 130 | + |
| 131 | + |
| 132 | +__all__ = get_all(__name__) |
0 commit comments