Question

This documentation of netowrkx says that it accepts the data structure that looks like a dictionary of dictionaries of dictionaries.

I went through this post but I could not figure out the way of representing it in the way acceptable to networkx.

Suppose I have a case where I have a user_no and corresponding user_nos which are actually his friends. How can I represent it like a dictionary?

Was it helpful?

Solution

Note that you're linking the networkx documentation of version 1.6 whereas the current version is 1.8.1.

I recommend rather thinking about what type of graph you need, i.e., single undirected edges between your nodes, choose a networkx.Graph, multiple directed edges, choose a networkx.MultiDiGraph (DiGraph and MultiGraph also exist).

You can then use the add_edge or add_edges_from methods and they will generate the underlying data structure for you, for example,

>>> import networkx as nx
>>> net = nx.Graph()
>>> net.add_edge("user_42", "user_9", trust=9)
>>> net.adj
{'user_42': {'user_9': {'trust': 9}}, 'user_9': {'user_42': {'trust': 9}}}

OTHER TIPS

See networkx documentation -

d={0: {1: 1}} # dict-of-dicts single edge (0,1)
G=nx.Graph(d)

https://networkx.github.io/documentation/latest/reference/convert.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top