【问题标题】:Converting Adjacency Matrix to Abstract Simplicial Complex将邻接矩阵转换为抽象单纯复形
【发布时间】:2018-06-13 09:29:19
【问题描述】:

我有一个由邻接矩阵表示的图,我想将其转换为一个抽象的单纯复形(即所有顶点、边、三角形、四面体的列表......)以进行一些拓扑图上的计算。但是,我在让算法完全正确时遇到了一些麻烦。我在网上看了看,找不到任何可以做到这一点的东西。

换句话说,代码所做的是创建一个所有边的列表(例如,a 和 b 之间的边是 ab,所以列表看起来像[ab, bc, ad...]。然后我们需要找到所有三角形。所以,从一条随机边开始,比如ab 并将其添加到字符串中。然后类似于深度优先搜索,我们将所有其他边的列表添加到队列中,其中包含 a 或 b。我们尝试追加将它们添加到字符串中。如果在 3 次迭代后,字符串包含重复项(即,它看起来像 abbcca),则将 abc 添加到我的三角形列表中,弹出堆栈并重试。

同样,对于 3 维(四面体),我们做类似的事情并查看我们的三角形列表[abc, bcd, bce...]。我们采用abc 并将所有共享abbcac 的三角形添加到我们的队列中,并尝试将这些三角形附加到字符串中。如果在 4 次迭代后我们只有重复项,那么我们知道有一个四面体。

根据需要继续进行尽可能多的维度。

但是,代码没有按预期工作,我真的被卡住了。

现在我只是在二维中工作并尝试获取三角形,并且稍后将简单地添加逻辑以处理更高的问题。

    def DFS(edges, count, node, triangles, tempTriangle):
    print(tempTriangle)
    visited, stack = set(), [node]
    tempTriangle = tempTriangle.strip(" ")
    if count > 2:
        return 
    elif len(tempTriangle) % 3 == 0 and deleteDuplicates(tempTriangle) == "":
        print("Triangle: ", oneTimeLetters(tempTriangle))
        triangles.append(oneTimeLetters(tempTriangle))
        tempTriangle = ""

    neighbors = [x for x in edges if hasIntersection(node, x) == False and strIntersection(tempTriangle, x) != x]

    for y in neighbors:
        if y not in visited:
            visited.add(y)
            tempTriangle = tempTriangle + y
            if count > 2:
                count = 0
                node = (edges - visited)[0]
                DFS(edges, 0, node, triangles, "")
            DFS(edges, count+1, y, triangles, tempTriangle)
            tempTriangle = tempTriangle[:len(tempTriangle)-2]
            visited.pop()

def deleteDuplicates(word):
    letterList = set()
    for c in word:
        if c in letterList:
            word = word.replace(c, "")
        letterList.add(c)
    return word

def oneTimeLetters(word):
    letterList = set()
    for c in word:
        if c in letterList:
            word = word.replace(c, "")
        letterList.add(c)
    return ''.join(letterList)

def hasIntersection(a, b):
        return not set(a).isdisjoint(b)

def strIntersection(s1, s2):
  out = ""
  for c in s1:
    if c in s2 and not c in out:
      out += c
  return out

我正在玩具箱上运行此图,该图有 5 个顶点,由

Edges = ['cd', 'da', 'eb', 'cb', 'dc', 'ea', 'db', 'ac', 'ca', 'bd', 'ba', 'be', 'ad', 'bc', 'ab', 'ae']


Adjacency matrix =
 [[ 0.  1.  1.  1.  1.]
     [ 1.  0.  1.  1.  1.]
     [ 1.  1.  0.  1.  0.]
     [ 1.  1.  1.  0.  0.]
     [ 1.  1.  0.  0.  0.]]

鉴于该输入,它只返回一个空列表,而 tempTriangle 中的打印语句为我提供了一长串内容

dc
dcae
dcaecd
dcaecb
dcaedb
dcaebc
dcaebd
dcba
dcbacd
dcea
dceacd
dceacb
dceadb
dceabc
//...abbreviated the long list 

因此,它不会在应该停止的时候停止,不会添加到三角形列表中,而且所有周围都不起作用。

我们将不胜感激任何和所有的帮助!

【问题讨论】:

  • 你的代码做错了什么?
  • 添加了输入/输出。谢谢!
  • 我看不到 count 最初是如何传递到 AdjacencyToASC() 函数中的,i 是在哪里定义的?
  • i 来自一个愚蠢的错误。我对其进行了更改以反映更准确的代码和测试用例场景。我稍后使用AdjacencyToASC(edges, 0, edges[0], triangles, "") print(triangles) 调用该函数,其中triangles 是一个空列表。

标签: python algorithm numpy graph topology


【解决方案1】:

这是一些工作代码。它保留了您的基本思想,但通过保留和重用前一个度数中每个单纯形的共享邻居列表对其进行了一些改进。

当找到包含单纯形 S 的下一个度单纯形时,我们选择一个随机顶点 V 和子单纯形 S-V。为了找到 S 的邻居,我们只需查找 V 和 S-V 的邻居并取交集。交集中的每个元素 N 给出一个新的单纯形 S+N。

我们利用 set 和 dict 容器进行快速查找、交叉和重复清除。

def find_cliques(edges, max_sz=None):
    make_strings = isinstance(next(iter(edges)), str)
    edges = {frozenset(edge) for edge in edges}
    vertices = {vertex for edge in edges for vertex in edge}
    neighbors = {vtx: frozenset(({vtx} ^ e).pop() for e in edges if vtx in e)
                 for vtx in vertices}
    if max_sz is None:
        max_sz = len(vertices) 

    simplices = [set(), vertices, edges]
    shared_neighbors = {frozenset({vtx}): nb for vtx, nb in neighbors.items()}
    for j in range(2, max_sz):
        nxt_deg = set()
        for smplx in simplices[-1]:
            # split off random vertex
            rem = set(smplx)
            rv = rem.pop()
            rem = frozenset(rem)
            # find shared neighbors
            shrd_nb = shared_neighbors[rem] & neighbors[rv]
            shared_neighbors[smplx] = shrd_nb
            # and build containing simplices
            nxt_deg.update(smplx|{vtx} for vtx in shrd_nb)
        if not nxt_deg:
            break
        simplices.append(nxt_deg)
    if make_strings:
        for j in range(2, len(simplices)):
            simplices[j] = {*map(''.join, map(sorted, simplices[j]))}
    return simplices

# demo
from itertools import combinations
edges = set(map(''.join, combinations('abcde', 2)))
random_missing_edge = edges.pop()
simplices = find_cliques(edges)

from pprint import pprint
pprint(random_missing_edge)
pprint(simplices)

样本输出:

'ae'
[set(),
 {'d', 'a', 'e', 'c', 'b'},
 {'be', 'ab', 'cd', 'bd', 'ad', 'ac', 'ce', 'bc', 'de'},
 {'bce', 'abc', 'acd', 'bcd', 'cde', 'abd', 'bde'},
 {'abcd', 'bcde'}]

【讨论】:

  • 天哪!太感谢了!!你是最好的!这真的很聪明。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多