module documentation

实现 igraph.Graph.Formula()

只有在你有非常强烈的理由时才应直接使用此模块。在几乎所有情况下,调用 igraph.Graph.Formula() 会更好。

函数 construct_graph_from_formula Graph.Formula(formula = None, attr = "name", simplify = True)
函数 generate_edges 从给定公式部分的头部解析边规范,并生成以下内容:
def construct_graph_from_formula(cls, formula=None, attr='name', simplify=True): (source)

Graph.Formula(formula = None, attr = "name", simplify = True)

从图公式生成图

图形公式是图形的简单字符串表示。它非常适合快速创建小型图形。字符串由顶点名称和边操作符分隔组成。边操作符是由破折号(-)组成的序列,可能以箭头开头(序列开头的<或序列末尾的>)。边操作符可以任意长,即你可以使用任意数量的破折号来绘制它们。这总共有四种不同的边操作符:

  • ----- makes an undirected edge
  • <---- makes a directed edge pointing from the vertex on the right hand side of the operator to the vertex on the left hand side
  • ----> is the opposite of <----
  • <---> creates a mutual directed edge pair between the two vertices

如果你只使用无向边操作符(-----),图将是无向的。否则,它将是有向的。公式中使用的顶点名称将被分配给图的name顶点属性。

一些简单的例子:

>>> from igraph import Graph
>>> print(Graph.Formula())          # empty graph
IGRAPH UN-- 0 0 --
+ attr: name (v)
>>> g = Graph.Formula("A-B")        # undirected graph
>>> g.vs["name"]
['A', 'B']
>>> print(g)
IGRAPH UN-- 2 1 --
+ attr: name (v)
+ edges (vertex names):
A--B
>>> g.get_edgelist()
[(0, 1)]
>>> g2 = Graph.Formula("A-----------B")
>>> g2.isomorphic(g)
True
>>> g = Graph.Formula("A  --->  B") # directed graph
>>> g.vs["name"]
['A', 'B']
>>> print(g)
IGRAPH DN-- 2 1 --
+ attr: name (v)
+ edges (vertex names):
A->B

如果你有许多不连接的组件,你可以用逗号将它们分开。你也可以指定孤立的顶点:

>>> g = Graph.Formula("A--B, C--D, E--F, G--H, I, J, K")
>>> print(", ".join(g.vs["name"]))
A, B, C, D, E, F, G, H, I, J, K
>>> g.connected_components().membership
[0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6]

冒号 (:) 运算符可用于指定顶点集。如果边运算符连接两个顶点集,则第一个顶点集中的每个顶点都将连接到第二个顶点集中的每个顶点:

>>> g = Graph.Formula("A:B:C:D --- E:F:G")
>>> g.isomorphic(Graph.Full_Bipartite(4, 3))
True

请注意,如果顶点名称包含空格或特殊字符,您必须引用它们:

>>> g = Graph.Formula('"this is" +- "a silly" -+ "graph here"')
>>> g.vs["name"]
['this is', 'a silly', 'graph here']
参数
cls未记录
formula公式本身
attr存储顶点名称的顶点属性名称
simplify是否简化构建的图
返回
构建的图:
def generate_edges(formula): (source)

从给定公式部分的头部解析边缘规范,并生成以下内容:

  • startpoint(s) of the edge by vertex names
  • endpoint(s) of the edge by names or an empty list if the vertices are isolated
  • a pair of bools to denote whether we had arrowheads at the start and end vertices