Вопрос

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:

  • DiGraph composed of 2 nodes: "square" and "triangle".
  • Edge between "square" and "triangle"
  • Node "square" is a subgraph containing 4 nodes (1 for each corner point);
  • Edges connecting these nodes;
  • Node "triangle" is a subgraph containing 3 nodes (1 for each corner point);
  • Edges connecting these nodes.

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

Это было полезно?

Решение

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)]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top