A hypergraph $H = (V, E)$ is composed of nodes ($V$) and hyperedges ($E$). HyperNetX (HNX) supports multi-edges by distinguishing edges via unique identifiers rather than just their node content.
To create a hypergraph, you must provide a setsystem, which defines the many-to-many relationships between edges and nodes. HNX supports five types of setsystems:
- Iterable of iterables: A barebones approach where edge IDs are generated using Pandas default indexing. Elements must be hashable.
- Dictionary of iterables: Provides explicit edge IDs as keys and iterables of nodes as values.
- Dictionary of dictionaries: Allows assigning cell_properties (metadata for specific edge-node incidence pairs) directly within the setsystem.
- pandas.DataFrame: Most efficient for large datasets. The first two columns must represent incidence pairs. You can specify columns for cell weights and miscellaneous cell properties.
- numpy.ndarray: For homogeneous $n imes 2$ arrays. A DataFrame is generated internally, and incidence properties must be added after construction.
# 1. Iterable of iterables
list_of_lists = [['book','candle','cat'],['book','coffee cup'],['coffee cup','radio']]
H = Hypergraph(list_of_lists)
# 2. Dictionary of iterables
sce_dict = {0: ('FN', 'TH'), 1: ('TH', 'JV')}
H = hnx.Hypergraph(sce_dict)
# 3. Dictionary of dictionaries (with cell properties)
nested_dict = {0: {'FN':{'time':'early'}, 'TH':{'time':'late'}}}
H = hnx.Hypergraph(nested_dict)
# 4. pandas.DataFrame
import pandas as pd
d = {'col1': ['e1', 'e1', 'e2'], 'col2': [1, 2, 1], 'w': [0.5, 0.1, 0.52], 'col3':[{'name': 'related_to'}, {'name': 'related_to', 'startdate':'05.13.2020'}, {'name': 'owned_by'}]}
df = pd.DataFrame(d)
H = hnx.Hypergraph(df, edge_col='col1', node_col='col2', cell_weight_col='w', misc_cell_properties_col='col3')
# 5. numpy.ndarray
import numpy as np
np_array = np.array([['A','a'],['A','b'],['B','a']])
H = hnx.Hypergraph(np_array)
H.incidences[('A','a')].color = 'red'