Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to created a weighted directed graph from edge list in Networkx

I have an edge-list, it consists of two columns, I want to create a weighted directed graph such that for each row in the edge-list a directed edge with weight one goes from node in column one to node in column two. if the same row appears more than once in the edge-list it should increase the weight by one for each time it appears.

I'm using Python Networkx library how can I do this?

like image 555
Ramin Zahedi Avatar asked Jan 14 '17 04:01

Ramin Zahedi


People also ask

How do I read and write a NetworkX graph?

Read and write NetworkX graphs as edge lists. The multi-line adjacency list format is useful for graphs with nodes that can be meaningfully represented as strings. With the edgelist format simple edge data can be stored but node or graph data is not.

What is NetworkX multigraphs?

Networkx allows us to create both directed and undirected Multigraphs. A Multigraph is a Graph where multiple parallel edges can connect the same nodes. For example, let us create a network of 10 people, A, B, C, D, E, F, G, H, I and J. They have four different relations among them namely Friend, Co-worker, Family and Neighbour.

How to add/update the edge weights for graph/digraph?

The edges must be given as 3-tuples (u, v, w) where w is a number. The attribute name for the edge weights to be added. Edge attributes to add/update for all edges. Adding the same edge twice for Graph/DiGraph simply updates the edge data. For MultiGraph/MultiDiGraph, duplicate edges are stored.

How to create a dglgraph from a NetworkX graph?

Creating a DGLGraph from a NetworkX graph is not fast especially for large scales. It is recommended to first convert a NetworkX graph into a tuple of node-tensors and then construct a DGLGraph with dgl.graph (). nx_graph ( networkx.Graph) – The NetworkX graph holding the graph structure and the node/edge attributes.


1 Answers

You can use Counter to count the number of duplicate edges in order to generate weight that is passed to DiGraph:

import networkx as nx
from collections import Counter

EDGES = [
    ('A', 'B'),
    ('B', 'C'),
    ('A', 'C'),
    ('C', 'D'),
    ('A', 'B')
]

g = nx.DiGraph((x, y, {'weight': v}) for (x, y), v in Counter(EDGES).items())
print(*g.edges(data=True), sep='\n')

Output:

('A', 'B', {'weight': 2})
('A', 'C', {'weight': 1})
('C', 'D', {'weight': 1})
('B', 'C', {'weight': 1})

In above Counter returns (edge, count) tuples. Note that edges passed to Counter must be hashable.

>>> edges = list(Counter(EDGES).items())
>>> edges
[(('A', 'B'), 2), (('B', 'C'), 1), (('C', 'D'), 1), (('A', 'C'), 1)]

Then generator expression is used to yield edges in a format that DiGraph expects:

>>> params = list((x, y, {'weight':v}) for (x,y), v in edges)
>>> params
[('A', 'B', {'weight': 2}), ('B', 'C', {'weight': 1}), ('C', 'D', {'weight': 1}), ('A', 'C', {'weight': 1})]
like image 102
niemmi Avatar answered Oct 03 '22 22:10

niemmi