How can I store information of an edge with Python Networkx

Question:

I converted a dictionary into a graph using NetworkX. Apart from just information about source and destination of an edge, the dictionary contains metadata of that edge like a name of that edge and a probability value related to that edge. Also the graph contains parallel edges. This is the first first two key value pairs for first two keys of the dictionary. You can notice there are two parallel edges between the nodes 25255942 and 72359602 with different names and probabilities.

{25255942: {52691892: [('Internet of Things (IOT) Device Management',
    0.4444444444444444)],
  72359602: [('Internet of Things (IOT) Device Management', 1.0),
   ('Questions', 0.07692307692307693)]},
 185589766: {183701781: [('"Cloud Computing" How to use it for your business',
    0.4),
   ('Learn How to Be a Success in Network Marketing', 0.16666666666666666)],
  183702935: [('"Cloud Computing" How to use it for your business', 0.4),
   ('Learn How to Be a Success in Network Marketing', 0.16666666666666666)],
  110069642: [('Learn How to Be a Success in Network Marketing',
    0.3333333333333333),
   ('How to make money in network marketing', 1.0)]}}

If I simply convert this dictionary into a graph and print the edges by this command g.edges() it looks like below, I mean, loses the metadata and the information regarding parallel edges too.

[(25255942, 52691892), (25255942, 72359602), .....]

Is there a way to create a graph using NetworkX without losing any information?

Asked By: Debbie

||

Answers:

You can use add_edges_from to add multiple edges at once. And for parallel edges, please refer to MultiDiGraph—Directed graphs with self loops and parallel edges.

import networkx as nx
import matplotlib.pyplot as plt   
from pprint import pprint

G = nx.MultiDiGraph()

# For each edge, we should assign source, destination, and attributes.
first_edge = (
    25255942, 
    52691892, 
    {'name': 'Internet of Things (IOT) Device Management', 'probability': 0.4444444444444444},
)
second_edge = (
    25255942, 
    72359602, 
    {'name': 'Internet of Things (IOT) Device Management', 'probability': 1.0},
)
third_edge = (
    25255942, 
    72359602, 
    {'name': 'Questions', 'probability': 0.07692307692307693},
)
edges = [first_edge, second_edge, third_edge]

G.add_edges_from(edges)
pprint(list(G.edges(data=True)))

and the output would of pprint(list(G.edges(data=True))) would be:

[(25255942,
  52691892,
  {'name': 'Internet of Things (IOT) Device Management',
   'probability': 0.4444444444444444}),
 (25255942,
  72359602,
  {'name': 'Internet of Things (IOT) Device Management', 'probability': 1.0}),
 (25255942,
  72359602,
  {'name': 'Questions', 'probability': 0.07692307692307693})]
Answered By: Ching Chang

You can add a dictionary to each edge and access it this way:

G.edges[25255942, 52691892]['name']

The docs have a nice example:

>>> G = nx.path_graph(3)  # or MultiGraph, etc
G.add_edge(2, 3, weight=5)
[e for e in G.edges]
[(0, 1), (1, 2), (2, 3)]
G.edges.data()  # default data is {} (empty dict)
EdgeDataView([(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})])
G.edges.data("weight", default=1)
EdgeDataView([(0, 1, 1), (1, 2, 1), (2, 3, 5)])
G.edges([0, 3])  # only edges from these nodes
EdgeDataView([(0, 1), (3, 2)])
G.edges(0)  # only edges from node 0
EdgeDataView([(0, 1)])
Answered By: neves