How to display relationships between different nodes

Viewed 1044

I would like to be able to see relationships between two nodes (or more).

Let's say I have those two paths: 1-2-4-5-7-8 1-3-4-6-7-8

From this we can see that nodes 2&5 are "linked together" and so are 3&6.

But if i just add those paths to a graph I end up with something like that:

G = nx.DiGraph()

nx.add_path(G, [1,2,4,5,7,8])
nx.add_path(G, [1,3,4,6,7,8])

nx.draw(G, with_labels=True)
plt.show()

graph

Yet it doesn't show the relations between 2&5 and between 3&6 and it seems like I can have the combination 2&6 and 3&5 even though they don't exist in my initial paths.

Is there anyway to show this ?

I am coding this python and using the libraries networkx to build the graph and pygraphviz for the visualization/rendering.

1 Answers

Yet it doesn't show the relations between 2&5 and between 3&6

I think you may be confusing what a path actually is. By adding a path to your network, you're adding all contiguous nodes in the path as edges. So adding the path (1,2,4) is the same ass adding the edges ((1,2), (2,4)). Hence nodes 2 and 5 in your graph, are connected through the node sequence 2,4,5, following this logic.

But with what you're describing, you'd need the combinations of all these nodes, something like:

from itertools import combinations

G = nx.DiGraph()

l1 = [1,2,4,5,7,8]
G.add_edges_from(combinations(l1, r=2))

plt.figure(figsize=(10,6))
nx.draw(G, with_labels=True)
plt.show()

enter image description here

and it seems like I can have the combination 2&6 and 3&5 even though they don't exist in my initial paths.

Nodes 2 and 6, even though they are added through different paths, they are connected, meaning that node 6 is accessible from node 2 through 1,2,4,5,7,8 and 1,3,4,6,7,8 from the second path.

Related