The graph_utils module provides utility functions for manipulating and analyzing graphs. This includes functions for setting random seeds, checking graph properties, building and manipulating adjacency matrices, and computing various graph metrics.
Basic Utilities
set_seed
defset_seed(seed:int)->None
Sets random seeds for reproducibility across all relevant libraries.
Parameters
Parameter
Type
Description
seed
int
Random seed value
Example
frombridge.utilsimportset_seed# Set seed for reproducible experiments
set_seed(42)
check_symmetry
defcheck_symmetry(g:dgl.DGLGraph)->bool
Checks if a DGL graph is symmetric (undirected).
Parameters
Parameter
Type
Description
g
dgl.DGLGraph
DGL graph to check
Returns
Return Type
Description
bool
True if graph is symmetric, False otherwise
Example
importdglfrombridge.utilsimportcheck_symmetry# Create a directed graph
g=dgl.graph(([0,1],[1,2]))# Check if it's symmetric
is_symmetric=check_symmetry(g)print(f"Graph is symmetric: {is_symmetric}")# Create a symmetric graph
g_sym=dgl.graph(([0,1,1,0],[1,0,2,2]))print(f"Symmetric graph check: {check_symmetry(g_sym)}")
Makes a DGL graph symmetric by adding reverse edges.
Parameters
Parameter
Type
Description
g
dgl.DGLGraph
DGL graph to symmetrize
sym_type
str
Symmetrization type: ‘both’, ‘upper’, or ‘lower’
Returns
Return Type
Description
dgl.DGLGraph
New symmetric DGL graph
Example
importdglfrombridge.utilsimportmake_symmetric# Create a directed graph
g=dgl.graph(([0,1],[1,2]))# Make it symmetric by adding all reverse edges
g_sym_both=make_symmetric(g,sym_type='both')# Make it symmetric using upper triangular part
g_sym_upper=make_symmetric(g,sym_type='upper')# Make it symmetric using lower triangular part
g_sym_lower=make_symmetric(g,sym_type='lower')print(f"Original edges: {g.num_edges()}")print(f"Symmetric (both) edges: {g_sym_both.num_edges()}")print(f"Symmetric (upper) edges: {g_sym_upper.num_edges()}")print(f"Symmetric (lower) edges: {g_sym_lower.num_edges()}")
Graph Metrics
homophily
defhomophily(g:dgl.DGLGraph)->float
Computes the homophily score of a graph, defined as the fraction of edges that connect nodes of the same class.
Parameters
Parameter
Type
Description
g
dgl.DGLGraph
Input graph with node labels in g.ndata[‘label’]
Returns
Return Type
Description
float
Homophily score between 0 and 1
Example
importdglimporttorchfrombridge.utilsimporthomophily# Create a graph with labels
g=dgl.graph(([0,0,1,1,2],[1,2,0,2,1]))g.ndata['label']=torch.tensor([0,0,1])# Nodes 0 and 1 are class 0, node 2 is class 1
# Compute homophily
h=homophily(g)print(f"Graph homophily: {h:.4f}")
Builds a sparse adjacency matrix from a DGL graph.
Parameters
Parameter
Type
Description
g
dgl.DGLGraph
Input DGL graph
self_loops
bool
Whether to include self-loops in the adjacency matrix
sym
bool
Whether to symmetrize the adjacency matrix
device
Union[str, torch.device]
Device to place the output tensor
Returns
Return Type
Description
torch.Tensor
Sparse adjacency matrix of shape (n_nodes, n_nodes)
Example
importdglfrombridge.utilsimportbuild_sparse_adj_matrix# Create a graph
g=dgl.graph(([0,1],[1,2]))# Build sparse adjacency matrix with self-loops
A_sparse=build_sparse_adj_matrix(g,self_loops=True)# Convert to dense for printing
A_dense=A_sparse.to_dense()print(f"Adjacency matrix with self-loops:\n{A_dense}")
Computes the adjacency matrix raised to the power p (without normalization).
Parameters
Parameter
Type
Description
p
int
The power to which the adjacency matrix is raised
g
dgl.DGLGraph
Input graph
self_loops
bool
Whether to include self-loops in the adjacency matrix
fix_d
bool
Placeholder for compatibility; not used here
sym
bool
Whether to symmetrize the adjacency matrix
device
Union[str, torch.device]
Device to perform computations on
Returns
Return Type
Description
torch.Tensor
The adjacency matrix raised to the power p
Usage in BRIDGE Pipeline
The graph utilities are essential components of the BRIDGE rewiring pipeline:
check_symmetry determines whether the graph is directed or undirected
build_sparse_adj_matrix and normalize_sparse_adj prepare adjacency matrices for computation
get_A_hat_p is used for computing higher-order connectivity patterns
homophily measures the quality of the graph structure
Example integration in the pipeline:
frombridge.utilsimportcheck_symmetry,homophilyfrombridge.rewiringimportcreate_rewired_graph# Check if the input graph is symmetric
is_symmetric=check_symmetry(g)sym_type='upper'ifis_symmetricelse'asymetric'# Create a rewired graph
g_rewired=create_rewired_graph(g=g,B_opt_tensor=B_opt_tensor,pred=pred,Z_pred=Z_pred,p_remove=0.1,p_add=0.1,sym_type=sym_type)# Compare homophily before and after rewiring
h_original=homophily(g)h_rewired=homophily(g_rewired)print(f"Original homophily: {h_original:.4f}")print(f"Rewired homophily: {h_rewired:.4f}")