Partition Generation¶
Initial feasible partition generators and helpers.
- asunder.base.utils.partition_generation._build_components_links_only(N, must_link, cannot_link)¶
Internal helper for building components given pairwise constraints.
- Parameters:
N (int) – Number of nodes.
must_link (sequence of tuple of int, optional) – Pairwise must-link constraints.
cannot_link (sequence of tuple of int, optional) – Pairwise cannot-link constraints.
- Returns:
Computed result.
- Return type:
Optional[Dict[str, Any]]
- asunder.base.utils.partition_generation._component_order_from_node_order(order_idx, cid, C)¶
Internal helper for building a component visitation order induced by a node visitation order.
- Parameters:
order_idx (List[int]) – Node indices in the desired traversal order.
cid (np.ndarray) – Component ID for each node; cid[i] gives the component containing node i.
C (int) – Total number of components.
- Returns:
comp_order – Component IDs in the order they first appear in order_idx, followed by any components not encountered (in increasing component ID).
- Return type:
List[int]
- asunder.base.utils.partition_generation._greedy_color_fixed_K(comp_order, forb, K_used, rng, jitter_top=1)¶
Internal helper for greedy coloring given a fixed K.
- Parameters:
comp_order (List[int]) – Ordered list of must-link component IDs to assign (e.g., in the sequence they will be colored/placed).
forb (List[set]) – Component-level cannot-link adjacency; forb[c] is the set of component IDs that component c is forbidden to share a group with.
K_used (int) – Number of communities.
rng (np.random.Generator) – Random number generator.
jitter_top (int) – If 0 or 1: no real randomness (always pick the single best). If 2: randomly choose between the top 2 candidates. Larger values → more diversity across runs but can limit freedom when K is fixed.
- Returns:
comp2g – Component-to-group assignment vector of length C. Entry comp2g[c] is the group/color ID assigned to component c.
- Return type:
Optional[np.ndarray]
- asunder.base.utils.partition_generation._greedy_color_unbounded_K(comp_order, forb)¶
Internal helper for greedy coloring given an unbounded K.
- Parameters:
comp_order (List[int]) – Ordered list of must-link component IDs to assign (e.g., in the sequence they will be colored/placed).
forb (List[set]) – Component-level cannot-link adjacency; forb[c] is the set of component IDs that component c is forbidden to share a group with.
- Returns:
comp2g – Component-to-group assignment vector of length C. Entry comp2g[c] is the group/color ID assigned to component c.
- Return type:
Optional[np.ndarray]
- asunder.base.utils.partition_generation.assign_from_order_with_links_links_only(order_idx, N, K=None, must_link=None, cannot_link=None, max_K_increase=50, max_restarts=20, jitter_top=2, seed=0)¶
Returns a partition given an order, pairwise constraints and other parameters.
- Parameters:
order_idx (List[int]) – Input parameter.
N (int) – Number of nodes.
K (int or None, optional) – Target number of communities.
must_link (sequence of tuple of int, optional) – Pairwise must-link constraints.
cannot_link (sequence of tuple of int, optional) – Pairwise cannot-link constraints.
max_K_increase (int, optional) – Maximum allowed increase above the requested K when additional partitions are needed to satisfy the cannot-link structure. If 0, the routine enforces the requested K exactly.
max_restarts (int) – maximum number of restarts.
jitter_top (int) – If 0 or 1: no real randomness (always pick the single best). If 2: randomly choose between the top 2 candidates. Larger values → more diversity across runs but can limit freedom when K is fixed.
seed (int, optional) – Random seed used to initialize the sampling procedure.
- Returns:
Partition vector and metadata.
- Return type:
Optional[Tuple[np.ndarray, Dict[str, int]]]
- asunder.base.utils.partition_generation._pairs_to_indices(pairs, node_to_idx)¶
Internal helper for mapping the names of node pairs to indices.
- Parameters:
pairs (Optional[Sequence[Tuple[Hashable, Hashable]]]) – Sequence of node pairs.
node_to_idx (Dict[Hashable, int]) – Node to index map.
- Returns:
Sequence of node pairs with integer identifiers.
- Return type:
Optional[List[Tuple[int, int]]]
- asunder.base.utils.partition_generation.make_partitions_links_only(G, K=None, must_link=None, cannot_link=None, n_cols=15, seed=42, nodes=None, max_K_increase=50)¶
Generates partly ordered feasible partitions subject to only pairwise link constraints.
- Parameters:
G (nx.Graph) – NetworkX graph.
K (int or None, optional) – Target number of communities.
must_link (sequence of tuple of int, optional) – Pairwise must-link constraints.
cannot_link (sequence of tuple of int, optional) – Pairwise cannot-link constraints.
n_cols (int, optional) – Number of columns.
seed (int, optional) – Random seed used to initialize the sampling procedure.
nodes (array-like, optional) – List of Graph nodes.
max_K_increase (int, optional) – Maximum allowed increase above the requested K` when additional partitions are needed to satisfy the cannot-link structure. If
0, the routine enforces the requestedKexactly.
- Returns:
Z_star – List of generated partitions.
- Return type:
list(array)
- asunder.base.utils.partition_generation.make_partitions_random_links_only(N, K=None, must_link=None, cannot_link=None, seed=42, return_Z=True, max_K_increase=50, n_parts=10)¶
Generate random feasible partitions subject only to pairwise link constraints.
- Parameters:
N (int) – Number of nodes.
K (int or None, optional) – Target number of communities.
must_link (sequence of tuple of int, optional) – Pairwise must-link constraints.
cannot_link (sequence of tuple of int, optional) – Pairwise cannot-link constraints.
seed (int, optional) – Random seed used to initialize the sampling procedure.
return_Z (bool, optional) – Boolean that determines if the
(N, N)partition matrixZis returned or not.max_K_increase (int, optional) – Maximum allowed increase above the requested
Kwhen additional partitions are needed to satisfy the cannot-link structure. If0, the routine enforces the requestedKexactly.n_parts (int, optional) – Number of feasible random partitions to generate.
- Returns:
List of feasible random partitions.
If
return_ZisTrue, each element is a partition assignment matrix, typically of shape(N, N).If
return_ZisFalse, each element is a dict with keys that refer to the name of the partition, a 1D representation of the partition and the number of communities in it.- Return type:
list
- asunder.base.utils.partition_generation.make_simple_partition(N, cannot_link=None, seed=42)¶
Create a single all-ones partition matrix with cannot-link zeroed pairs.
- Parameters:
N (int) – Number of nodes.
cannot_link (Sequence[tuple[int, int]] | None) – List of nodes that cannot be linked.
- Returns:
Generated partition.
- Return type:
list[array]