A first hypergraph: incidence matrix, dual and degrees#

A hypergraph \(H=(V,E)\) consists of vertices and hyperedges, where every hyperedge is an arbitrary subset of the vertices. This script builds the five-vertex example of the HAT-VIS paper, prints its incidence matrix \(\mathbf{B}\), the dual hypergraph \(H^*\) (incidence \(\mathbf{B}^\mathsf{T}\)) and the basic statistics, and draws the two views side by side.

import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import draw_hypergraph, plot_incidence

# [snippet:build]
H = hv.Hypergraph({"e1": ["v1", "v2", "v3"], "e2": ["v1", "v2", "v4"], "e3": ["v3", "v4", "v5"]})
print(H.to_dataframe().astype(int))
print(H.degrees().to_dict(), H.edge_sizes().to_dict())
D = H.dual()
print(D.edge_dict())
# [/snippet:build]
print(H.summary())
    e1  e2  e3
v1   1   1   0
v2   1   1   0
v3   1   0   1
v4   0   1   1
v5   0   0   1
{'v1': 2, 'v2': 2, 'v3': 2, 'v4': 2, 'v5': 1} {'e1': 3, 'e2': 3, 'e3': 3}
{'v1': ('e1', 'e2'), 'v2': ('e1', 'e2'), 'v3': ('e1', 'e3'), 'v4': ('e2', 'e3'), 'v5': ('e3',)}
vertices                5.0
hyperedges              3.0
incidences              9.0
density                 0.6
mean size               3.0
max size                3.0
mean degree             1.8
max degree              2.0
isolated vertices       0.0
duplicate hyperedges    0.0
Name: hypergraph, dtype: float64

The incidence matrix (left) is a complete description; the node-link view (middle) draws every hyperedge as a padded convex hull around its members, here with the force-directed layout of HyperGodot; the dual (right) swaps the roles: the hyperedges become the vertices.

fig, axes = plt.subplots(1, 3, figsize=(13, 3.8), gridspec_kw={"width_ratios": [1, 1.3, 1.3]})
plot_incidence(H, order=None, ax=axes[0], title="incidence matrix")
draw_hypergraph(H, layout="force", ax=axes[1], title="H", legend=False, edge_labels=True, style="smooth")
draw_hypergraph(D, layout="force", ax=axes[2], title="dual H*", legend=False, edge_labels=True, style="smooth")
plt.tight_layout()
incidence matrix, H, dual H*

Total running time of the script: (0 minutes 0.398 seconds)

Gallery generated by Sphinx-Gallery