如何生成无向图的所有有向排列?

时间:2021-03-31 13:00:53

标签: python networkx graph-theory

我正在寻找一种从无向模板生成所有可能的有向图的方法。例如,给定这个图“模板”:

Undirected template graph. A triangle missing an edge

我想生成所有这六个定向版本:

All versions of the graph with directed edges

换句话说,对于模板中的每条边,为结果边选择 LEFT、RIGHT 或 BOTH 方向。

即使是小图也有大量输出,因为有 3^E 个有效排列(其中 E 是模板图中的边数),但其中许多是重复的(具体来说,它们是自守的到另一个输出)。以这两个为例:

Two isomorphic options

我只需要一个。


我首先很好奇:这个操作有术语吗?这一定是一个正式且易于理解的过程吗?

其次,是否有更有效的算法来生成此列表?我当前的代码(Python、NetworkX,尽管这对问题并不重要)看起来像这样,其中有两件事我不知道不喜欢:

  1. 我生成所有排列,即使它们与之前的图同构
  2. 我在最后检查同构,所以它增加了额外的计算成本
Results := Empty List
T := The Template (Undirected Graph)

For i in range(3^E):
    Create an empty directed graph G
    convert i to trinary
    For each nth edge in T:
        If the nth digit of i in trinary is 1:
            Add the edge to G as (A, B)
        If the nth digit of i in trinary is 2:
            Add the edge to G as (B, A)
        If the nth digit of i in trinary is 0:
            Add the reversed AND forward edges to G
   
    For every graph in Results:
        If G is isomorphic to Results, STOP
    Add G to Results

0 个答案:

没有答案
相关问题