Bipartite graph to a projection graph

119 views Asked by At

Hi I'm trying to project a bipartite graph (B) using Networkx's projected_graph but due to memory constraints (B has more than 140K nodes), this keeps crashing. I tried a matrix multiplication using a sparse matrix for computing the adjacency matrix but due to my matrix not being sparse enough, that didn't work either. Can anyone please help with this?

import networkx as nx
from networkx.algorithms import bipartite

B.add_nodes_from(inmates_list, bipartite=0)
B.add_nodes_from(cells_list, bipartite=1)

inmates = set(n for n,d in B.nodes(data=True) if d['bipartite']==0)
cells = = set(B) - inmates
G = bipartite.projected_graph(B, inmates)
1

There are 1 answers

0
user2146697 On
import pandas as pd
import networkx as nx

# Assuming your DataFrame is named 'df' with n rows and k columns for labels

# Create an empty graph
graph = nx.Graph()

# Add nodes to the graph
graph.add_nodes_from(df.index)

# Create a dictionary to store the weights for each pair of nodes
weights = {}

# Iterate over the labels and update the weights based on the count of common labels
for label in df.columns:
    label_nodes = df.index[df[label] == 1].tolist()
    edges = [(u, v) for u, v in nx.utils.pairwise(label_nodes)]
    
    # Update the weights for each edge
    for u, v in edges:
        if (u, v) in weights:
            weights[(u, v)] += (df.loc[u, label].astype(bool) & df.loc[v, label].astype(bool)).sum()
        else:
            weights[(u, v)] = (df.loc[u, label].astype(bool) & df.loc[v, label].astype(bool)).sum()

# Add the edges with updated weights to the graph
graph.add_weighted_edges_from((u, v, weight) for (u, v), weight in weights.items())

# Now you have the graph with weighted edges based on the count of common labels