Francesco Sgaramella
Francesco Sgaramella

Reputation: 1017

In NetworkX is it possible to define a node as a subgraph?

I was checking on the web, but I didn't find an answer.

Do you guys know if in NetworkX is possible to define a node as a subgraph?

Let me pose the question better: I have a graph composed of some nodes that are shapes (square, circle, triangle, etc.). I want now to define each node as a subgraph. The nodes of the subgraph should be the corner points of the shape (for triangle: subgraph of 3 nodes since there are 3 corner points). And this subgraph creation, of course, should not affect the starting graph.

Example:

Is it possible to do it in NetworkX? Any help or suggestion is apreciated.

Upvotes: 2

Views: 777

Answers (1)

Aric
Aric

Reputation: 25299

Yes, you are allowed to do that, e.g.

In [1]: import networkx as nx

In [2]: square = nx.Graph()

In [3]: square.add_cycle([1,2,3,4])

In [4]: triangle = nx.Graph()

In [5]: triangle.add_cycle([10,20,30])

In [6]: shapes = nx.Graph()

In [7]: shapes.add_edge(triangle,square)

In [8]: shapes.edges()
Out[8]: 
[(<networkx.classes.graph.Graph at 0x962040c>,
  <networkx.classes.graph.Graph at 0x962042c>)]

In [9]: shapes.nodes()[0].edges()
Out[9]: [(1, 2), (1, 4), (2, 3), (3, 4)]

In [10]: shapes.nodes()[1].edges()
Out[10]: [(10, 20), (10, 30), (20, 30)]

Upvotes: 2

Related Questions