OOP Networkx

100 views
Skip to first unread message

Untitled Master

unread,
Mar 18, 2024, 8:23:19 PMMar 18
to networkx-discuss
I was wondering if there is an object oriented version of the graph. 
Normally, Networkx usually handles nodes, edges, and other objects by using dictionaries.

This isn't viable for me because I'm planning on using Networkx to make an app. 

Thanks!

seth....@gmail.com

unread,
Mar 19, 2024, 1:19:54 PMMar 19
to networkx-discuss
NetworkX Graph objects are already working in an object oriented paradigm.

What exactly is the blocker for your use case?

Untitled Master

unread,
Mar 19, 2024, 5:49:09 PMMar 19
to networkx-discuss
There aren''t any objects being invovled. Things like nodes are not Python objects - they are dictionaries. In my case I'm trying to extend the functionality of nodes, by adding click and hover events. 

For example, if I was to get all the nodes in a graph, it will just return to me a dictionary with the nodes, but I need the parameters of the nodes and I also want to add my own variables/parameters to the nodes which I can easily do with OOP. 
There might be some solution for dictionaries but thats not good for my use case. OOP in my opinion is just the better way to go because it provides better debugging, and is also more stable for applications. 

Dan Schult

unread,
Mar 19, 2024, 8:16:11 PMMar 19
to networkx...@googlegroups.com
Nodes can be any object -- you get to choose (it has to be hashable).  
NetworkX is very flexible that way. Take a read through the tutorial in the docs.
Let's suppose your nodes are some custom-designed object. To set up the graph:

G = nx.Graph()
G.add_nodes_from([n1, n2, n3, n4])
G.add_edges_from([(n1, n2), (n2, n3), (n4, n4)])

Or, you can denote the nodes using strings, or integers. And hold the node attributes on the graph structure: Similarly for edges.

G = nx.Graph()
G.add_nodes_from(range(5))
for i in G:
    G.nodes[i]["color"] = "blue"
    G.add_edge(i, i+1, distance= <some expressions for the distance>)

Chris Myers

unread,
Mar 19, 2024, 8:45:41 PMMar 19
to networkx...@googlegroups.com
I concur with the others that NetworkX provides a comprehensive object-oriented interface for graphs.  In fact, I used the example of NetworkX a few weeks ago in an online lecture I was giving that included a discussion of some of the basics of OOP in Python.

Since the original poster indicated an interest in being able to interact with NetworkX graphs through click and hover events, I thought I would mention that there is some amount of integration of this sort in Bokeh, which supports interactive data visualization in web browsers.  I haven't used Bokeh+NetworkX specifically, but I have used Bokeh for other sorts of data visualization, and would encourage those interested in interactive graph visualization to consider it.

Best,
Chris


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/CA%2BXMcTMPxUO9QHQSwCt6AFpPrU1G65JOSBt-9EKmoTTL8E21GA%40mail.gmail.com.

Untitled Master

unread,
Mar 19, 2024, 9:16:28 PMMar 19
to networkx-discuss
Thank you so much for this! I will continue to research this!

Juan BC

unread,
Mar 19, 2024, 9:41:10 PMMar 19
to networkx-discuss
Conceptually you can't escape pop in python... Maybe you want to use a custom node class, if your class has the Dundee hash method... Is enough 

Dan Schult

unread,
Mar 19, 2024, 9:57:26 PMMar 19
to networkx...@googlegroups.com

Conceptually you can't escape pop in python... 
Can you explain what you mean by "escape pop in python"? 

Juan BC

unread,
Mar 19, 2024, 11:56:50 PMMar 19
to networkx-discuss
Escape oop (autocorrector sorry)

El mar, 19 de mar. de 2024 22:57, Dan Schult <dsc...@colgate.edu> escribió:

Conceptually you can't escape pop in python... 
Can you explain what you mean by "escape pop in python"? 

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.

Untitled Master

unread,
Mar 20, 2024, 4:50:44 PMMar 20
to networkx-discuss
Hi just one last question! For the properties of the custom nodes. How does networkx apply them. For example networkx has some default properties like the X and Y positions right, and maybe some other parameters. How would I integrate that into my custom node class. 

Dan Schult

unread,
Mar 20, 2024, 11:15:08 PMMar 20
to networkx...@googlegroups.com
NetworkX allows you to store any data attributes you want on the graph.
And some of the function (e.g. shortest_path) allow you to use those attributes directly
in the computations. But the drawing tools don't directly access the values on the Graph.

We have been talking about possibly making it possible to use this behavior. But at the moment,
we follow the matplotlib style of providing the drawing functions with arrays/lists of values for the
attributes. For example, `node_size` could be stored in a node's data attribute "size". But the drawing
program asks for a list of node_sizes in the order that matches the nodelist order (usually the order
of nodes in the graph).  To make this easier to manipulate, we provide functions `nx.get_node_attributes`
and its "set" partner.  So, you could do something like this:

```python
sizes = list(nx.get_node_attributes(G, "size").values())
```
Reply all
Reply to author
Forward
0 new messages