topobench.transforms.liftings.graph2hypergraph.modularity_maximization_lifting module#
This module implements the ModularityMaximizationLifting class.
- class Graph2HypergraphLifting(**kwargs)#
Bases:
GraphLiftingAbstract class for lifting graphs to hypergraphs.
- Parameters:
- **kwargsoptional
Additional arguments for the class.
- __init__(**kwargs)#
- class ModularityMaximizationLifting(num_communities=2, k_neighbors=3, **kwargs)#
Bases:
Graph2HypergraphLiftingLifts graphs to hypergraph domain using modularity maximization and community detection.
This method creates hyperedges based on the community structure of the graph and k-nearest neighbors within each community.
- Parameters:
- num_communitiesint, optional
The number of communities to detect. Default is 2.
- k_neighborsint, optional
The number of nearest neighbors to consider within each community. Default is 3.
- **kwargsoptional
Additional arguments for the base class.
- __init__(num_communities=2, k_neighbors=3, **kwargs)#
- detect_communities(b)#
Detect communities using spectral clustering on the modularity matrix.
- Parameters:
- btorch.Tensor
The modularity matrix.
- Returns:
- torch.Tensor
The community assignments for each node.
- kmeans(x, n_clusters, n_iterations=100)#
Perform k-means clustering on the input data.
Note: This implementation uses random initialization, so results may vary between runs even for the same input data.
- Parameters:
- xtorch.Tensor
The input data to cluster.
- n_clustersint
The number of clusters to form.
- n_iterationsint, optional
The maximum number of iterations. Default is 100.
- Returns:
- torch.Tensor
The cluster assignments for each input point.
- lift_topology(data)#
Lift the graph topology to a hypergraph based on community structure and k-nearest neighbors.
- Parameters:
- datatorch_geometric.data.Data
The input graph data.
- Returns:
- dict
A dictionary containing the incidence matrix of the hypergraph, number of hyperedges, and the original node features.
- modularity_matrix(data)#
Compute the modularity matrix B of the graph.
B_ij = A_ij - (k_i * k_j) / (2m)
- Parameters:
- datatorch_geometric.data.Data
The input graph data.
- Returns:
- torch.Tensor
The modularity matrix B.