Issues relating to creating a graph for GNNs

108 views
Skip to first unread message

Edoziem Enyinnaya

unread,
Mar 2, 2024, 8:36:05 PMMar 2
to networkx-discuss

i’m having an issue in turning a tabular dataset to a graph with nodes and edges. After converting it to a graph data, not all was connected and i was still having a disconnected graph in it even after preprocessing the dataset?

So is it normal or i’m i missing out?

Dan Schult

unread,
Mar 4, 2024, 9:29:47 PMMar 4
to networkx...@googlegroups.com
There isn't enough information in your post to know what is going on.
There are a number of ways to read tabular data into a graph.
The tabular data could represent an adjacency matrix, or an edgelist
or adjacency lists, etc. So you have to tell NX what type of tabular data you have.
You might also find is easy to read your data into pandas or numpy and then 
convert that to networkx (and in the process use the function for adjacency, or edgelist, etc)

Best,
Dan

Edoziem Enyinnaya

unread,
Mar 5, 2024, 4:11:07 AMMar 5
to networkx-discuss
import networkx as nx
import pandas as pd

# Create bipartite graph
user_restaurant_graph = nx.Graph()
user_category_graph = nx.Graph()

# Extract unique users, restaurants, and categories
unique_users = subset_review_merged1['user_id'].unique()
unique_restaurants = subset_review_merged1['business_id'].unique()
unique_categories = subset_review_merged['categories'].unique()

# Add nodes with 'bipartite' attribute to user-restaurant graph
user_restaurant_graph.add_nodes_from(unique_users, bipartite='user')
user_restaurant_graph.add_nodes_from(unique_restaurants, bipartite='restaurant')

# Add nodes with 'bipartite' attribute to user-category graph
user_category_graph.add_nodes_from(unique_users, bipartite='restaurant')
user_category_graph.add_nodes_from(unique_categories, bipartite='category')

# Add edges connecting users to restaurants
user_restaurant_edges = [(row['user_id'], row['business_id']) for _, row in subset_review_merged1.iterrows()]
user_restaurant_graph.add_edges_from(user_restaurant_edges)

# Add edges connecting users to categories
user_category_edges = [(row['user_id'], row['categories']) for _, row in subset_review_merged.iterrows()]
user_category_graph.add_edges_from(user_category_edges)

# Add node features to the user-restaurant graph
# Add 'review_count' as a node feature
user_review_count = dict(zip(subset_review_merged1['user_id'], subset_review_merged1['review_count_x']))
restaurant_review_count = dict(zip(subset_review_merged1['business_id'], subset_review_merged1['review_count']))
nx.set_node_attributes(user_restaurant_graph, user_review_count, name='review_count_user')
nx.set_node_attributes(user_restaurant_graph, restaurant_review_count, name='review_count_restaurant')

This is the code for the bipartite graph, the issue i'm having is that when i check for the connectivity of the code, i noticed some where disconnected?

Edoziem Enyinnaya

unread,
Mar 5, 2024, 8:39:03 AMMar 5
to networkx-discuss
# Function to check bipartiteness using BFS
def is_bipartite_bfs(graph):
    colors = {}
    queue = []

    for node in graph.nodes:
        if node not in colors:
            colors[node] = 0  # Assign color 0 to the first uncolored node
            queue.append(node)

            while queue:
                current_node = queue.pop(0)

                for neighbor in graph.neighbors(current_node):
                    if neighbor not in colors:
                        colors[neighbor] = 1 - colors[current_node]  # Assign opposite color
                        queue.append(neighbor)
                    elif colors[neighbor] == colors[current_node]:
                        print(f"Graph is not bipartite: Conflict between {current_node} and {neighbor}")
                        print(f"Edge causing the conflict: {current_node} - {neighbor}")
                        return False  # Graph is not bipartite if adjacent nodes have the same color

    return True

After checking if the graph is bipartite, which it is. My question is:
1) is it the right way to create bipartite graph and also forming the node, edges, node features and edge features?
2) i'm i on track since its a bipartite graph?
3) And is there another way i can do it, or is the original method i implied wrong?
Reply all
Reply to author
Forward
0 new messages