Python library for the creation and study of hypergraphs with analysis, visualization, and algorithm capabilities
—
Comprehensive collection of hypergraph algorithms including centrality measures, homology computations, contagion models, clustering methods, generative models, modularity functions, and matching algorithms.
S-centrality measures that generalize traditional graph centrality to hypergraphs by considering higher-order relationships through s-walks.
def s_betweenness_centrality(
H: "Hypergraph",
s: int = 1,
edges: bool = True,
normalized: bool = True,
return_singletons: bool = True
) -> Dict[Union[str, int], float]:
"""
S-betweenness centrality for hypergraph nodes or edges.
Parameters:
- H: Input hypergraph
- s: Centrality level (paths through edges of size ≥ s+1)
- edges: Compute for edges (True) or nodes (False)
- normalized: Normalize centrality values
- return_singletons: Include singleton components
Returns:
Dictionary mapping node/edge UIDs to centrality values
"""
def s_closeness_centrality(
H: "Hypergraph",
s: int = 1,
edges: bool = True,
return_singletons: bool = True,
source: Optional[Union[str, int]] = None
) -> Dict[Union[str, int], float]:
"""
S-closeness centrality for hypergraph nodes or edges.
Parameters:
- H: Input hypergraph
- s: Centrality level
- edges: Compute for edges (True) or nodes (False)
- return_singletons: Include singleton components
- source: Compute only for specific source node/edge
"""
def s_harmonic_centrality(
H: "Hypergraph",
s: int = 1,
edges: bool = True,
source: Optional[Union[str, int]] = None,
normalized: bool = False,
return_singletons: bool = True
) -> Dict[Union[str, int], float]:
"""
S-harmonic centrality for hypergraph nodes or edges.
Parameters:
- H: Input hypergraph
- s: Centrality level
- edges: Compute for edges (True) or nodes (False)
- source: Compute only for specific source node/edge
- normalized: Normalize centrality values
- return_singletons: Include singleton components
"""
def s_harmonic_closeness_centrality(
H: "Hypergraph",
s: int = 1
) -> Dict[Union[str, int], float]:
"""S-harmonic closeness centrality for hypergraph nodes."""
def s_eccentricity(
H: "Hypergraph",
s: int = 1,
edges: bool = True,
source: Optional[Union[str, int]] = None,
return_singletons: bool = True
) -> Dict[Union[str, int], float]:
"""
S-eccentricity for hypergraph nodes or edges.
Parameters:
- H: Input hypergraph
- s: Eccentricity level
- edges: Compute for edges (True) or nodes (False)
- source: Compute only for specific source node/edge
- return_singletons: Include singleton components
Returns:
Dictionary mapping node/edge UIDs to eccentricity values
"""Algebraic topology computations for hypergraphs including homology, Betti numbers, and chain complexes using mod-2 arithmetic.
def betti_numbers(
h: "Hypergraph",
k: Optional[int] = None
) -> Dict[int, int]:
"""
Compute Betti numbers of the hypergraph.
Parameters:
- h: Input hypergraph
- k: Specific dimension (returns all if None)
Returns:
Dictionary mapping dimension to Betti number
"""
def betti(
H: "Hypergraph",
k: int
) -> int:
"""Compute k-th Betti number."""
def homology_basis(
H: "Hypergraph",
k: int
) -> List:
"""Compute homology basis for dimension k."""
def hypergraph_homology_basis(
H: "Hypergraph"
) -> Dict[int, List]:
"""Compute homology basis for all dimensions."""
def chain_complex(H: "Hypergraph") -> Dict[int, Any]:
"""Construct chain complex from hypergraph."""
def boundary_group(
H: "Hypergraph",
k: int
) -> Any:
"""Compute boundary group for dimension k."""
def kchainbasis(
H: "Hypergraph",
k: int
) -> List:
"""Compute k-chain basis."""
def bkMatrix(
H: "Hypergraph",
k: int
) -> Any:
"""Compute k-th boundary matrix."""
def smith_normal_form_mod2(matrix: Any) -> tuple:
"""Compute Smith normal form modulo 2."""
def reduced_row_echelon_form_mod2(matrix: Any) -> tuple:
"""Compute reduced row echelon form modulo 2."""
def interpret(result: Any) -> str:
"""Interpret homology computation results."""
# Matrix operation utilities
def swap_rows(matrix: Any, i: int, j: int) -> Any:
"""Swap two rows in matrix."""
def swap_columns(matrix: Any, i: int, j: int) -> Any:
"""Swap two columns in matrix."""
def add_to_row(matrix: Any, i: int, j: int) -> Any:
"""Add row j to row i."""
def add_to_column(matrix: Any, i: int, j: int) -> Any:
"""Add column j to column i."""
def logical_dot(a: Any, b: Any) -> Any:
"""Logical dot product."""
def logical_matmul(a: Any, b: Any) -> Any:
"""Logical matrix multiplication."""
def logical_matadd(a: Any, b: Any) -> Any:
"""Logical matrix addition."""
def matmulreduce(a: Any, b: Any) -> Any:
"""Matrix multiplication with reduction."""Epidemic and contagion models adapted for hypergraphs, supporting both individual and collective contagion mechanisms.
def collective_contagion(
H: "Hypergraph",
**kwargs
) -> Dict:
"""
Collective contagion model where nodes influence each other through hyperedges.
Parameters:
- H: Input hypergraph
- **kwargs: Model parameters (initial conditions, rates, etc.)
Returns:
Dictionary with simulation results
"""
def individual_contagion(
H: "Hypergraph",
**kwargs
) -> Dict:
"""Individual contagion model with pairwise interactions."""
def discrete_SIR(
H: "Hypergraph",
**kwargs
) -> Dict:
"""
Discrete-time SIR (Susceptible-Infected-Recovered) model.
Parameters:
- H: Input hypergraph
- **kwargs: Model parameters
Returns:
Time series of compartment populations
"""
def discrete_SIS(
H: "Hypergraph",
**kwargs
) -> Dict:
"""Discrete-time SIS (Susceptible-Infected-Susceptible) model."""
def Gillespie_SIR(
H: "Hypergraph",
**kwargs
) -> Dict:
"""Continuous-time SIR model using Gillespie algorithm."""
def Gillespie_SIS(
H: "Hypergraph",
**kwargs
) -> Dict:
"""Continuous-time SIS model using Gillespie algorithm."""
def contagion_animation(
H: "Hypergraph",
**kwargs
) -> Any:
"""Create animation of contagion spreading process."""
def threshold(value: float, threshold: float) -> bool:
"""Threshold function for contagion models."""
def majority_vote(values: List[float]) -> bool:
"""Majority vote decision function."""Spectral clustering methods using hypergraph Laplacians and probability transition matrices.
def spec_clus(
H: "Hypergraph",
k: int = 2
) -> List[int]:
"""
Spectral clustering of hypergraph nodes.
Parameters:
- H: Input hypergraph
- k: Number of clusters
Returns:
List of cluster assignments for each node
"""
def norm_lap(H: "Hypergraph") -> Any:
"""
Compute normalized Laplacian matrix.
Parameters:
- H: Input hypergraph
Returns:
Normalized Laplacian matrix
"""
def prob_trans(H: "Hypergraph") -> Any:
"""Compute probability transition matrix."""
def get_pi(H: "Hypergraph") -> Any:
"""Compute stationary distribution."""Random hypergraph generation models including Erdős-Rényi, Chung-Lu, and degree-corrected stochastic block models.
def erdos_renyi_hypergraph(
n: int,
m: int,
k: Union[int, List[int]] = 2,
**kwargs
) -> "Hypergraph":
"""
Generate Erdős-Rényi random hypergraph.
Parameters:
- n: Number of nodes
- m: Number of edges
- k: Edge size (fixed or distribution)
- **kwargs: Additional parameters
Returns:
Random hypergraph
"""
def chung_lu_hypergraph(
degree_seq: List[int],
edge_size_seq: List[int],
**kwargs
) -> "Hypergraph":
"""
Generate Chung-Lu random hypergraph.
Parameters:
- degree_seq: Node degree sequence
- edge_size_seq: Edge size sequence
- **kwargs: Additional parameters
Returns:
Random hypergraph matching degree and edge size sequences
"""
def dcsbm_hypergraph(
degree_seq: List[int],
communities: List[int],
**kwargs
) -> "Hypergraph":
"""
Generate degree-corrected stochastic block model hypergraph.
Parameters:
- degree_seq: Node degree sequence
- communities: Community assignments
- **kwargs: Model parameters
Returns:
Random hypergraph with community structure
"""Modularity functions and community detection methods for hypergraphs.
def modularity(
H: "Hypergraph",
partition: Union[Dict, List],
wdc: callable = None
) -> float:
"""
Compute hypergraph modularity for a given partition.
Parameters:
- H: Input hypergraph
- partition: Node partition (dict or list of communities)
- wdc: Weight distribution function
Returns:
Modularity value
"""
def dict2part(partition_dict: Dict) -> List[set]:
"""Convert partition dictionary to list of communities."""
def part2dict(partition_list: List[set]) -> Dict:
"""Convert partition list to dictionary."""
def linear(H: "Hypergraph", partition: Union[Dict, List]) -> float:
"""Linear modularity function."""
def majority(H: "Hypergraph", partition: Union[Dict, List]) -> float:
"""Majority modularity function."""
def strict(H: "Hypergraph", partition: Union[Dict, List]) -> float:
"""Strict modularity function."""
def two_section(H: "Hypergraph") -> Any:
"""Two-section graph construction."""
def kumar(H: "Hypergraph", **kwargs) -> Any:
"""Kumar's modularity optimization method."""
def last_step(H: "Hypergraph", **kwargs) -> Any:
"""Last step modularity optimization."""Various matching algorithms for hypergraphs including greedy, maximal, and approximation methods.
def greedy_matching(
hypergraph: "Hypergraph",
k: int
) -> list:
"""
Greedy matching algorithm for hypergraphs.
Parameters:
- hypergraph: Input hypergraph
- k: Matching parameter
Returns:
List of matched edge UIDs
"""
def maximal_matching(H: "Hypergraph") -> List[Union[str, int]]:
"""
Maximal matching algorithm.
Parameters:
- H: Input hypergraph
Returns:
List of edges forming maximal matching
"""
def iterated_sampling(
hypergraph: "Hypergraph",
s: int,
max_iterations: int = 100
) -> list:
"""
Iterated sampling matching method.
Parameters:
- hypergraph: Input hypergraph
- s: Sampling parameter
- max_iterations: Maximum number of sampling iterations
Returns:
Best matching found
"""
def HEDCS_matching(
hypergraph: "Hypergraph",
s: int
) -> list:
"""
HEDCS (Hypergraph Edge Disjoint Cover Set) matching algorithm.
Parameters:
- hypergraph: Input hypergraph
- s: HEDCS parameter
Returns:
HEDCS matching
"""
def approximation_matching_checking(
H: "Hypergraph",
matching: List[Union[str, int]]
) -> float:
"""
Check approximation quality of a matching.
Parameters:
- H: Input hypergraph
- matching: Candidate matching
Returns:
Approximation ratio
"""Install with Tessl CLI
npx tessl i tessl/pypi-hypernetx