繁体   English   中英

在python中的邻接矩阵中打印图形的所有边

[英]printing all the edges of a graph in an adjacency matrix in python

如何在 python 中打印具有给定邻接矩阵的图形的所有边? 例如,如果 0 与 3 和 8 相邻,它应该不重复地打印:0 3 0 8 我一直在使用 Bfs,但我不知道如何更新队列和当前元素。

到目前为止,这是我的代码

A =  [[0, 1, 0, 0, 0, 1], 
      [1, 0, 0, 0, 0, 1], 
      [0, 0, 0, 1, 1, 0], 
      [0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 0],
      [1, 0, 0, 0, 0, 0]]

def edges(A):
    visited = [False] * len(A)
    queue = []

    s = [0][0]
    queue.append(s)
    visited[s] = True

    while len(queue) > 0:
        s = queue.pop(0)
        print(s)
        for i in range(len(A)):
            print(i)

            for j in range(len(A[0])):
                if A[i][j] == 1 and visited[s]== False:

                    queue.append([i][j])

                    visited[s] = True

print(edges(A))

如果我理解正确,并且考虑到您的示例矩阵A是不对称的,您可以这样做:

A =  [[0, 1, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 1],
      [0, 0, 0, 1, 1, 0],
      [0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 0],
      [1, 0, 0, 0, 0, 0]]

def edges(adj):

    for i, neighbors in enumerate(adj):
        for j, v in enumerate(neighbors):
            if v:
                yield (i, j)


for edge in edges(A):
    print(edge)

输出

(0, 1)
(0, 5)
(1, 0)
(1, 5)
(2, 3)
(2, 4)
(3, 4)
(5, 0)

一个简单的方法是迭代邻接矩阵,并构建一个带有索引的元组列表,其中存在连接:

[(i,j) for i,l in enumerate(A) for j,v in enumerate(l) if v]
# [(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)]

但是,您可以使用networkx轻松做到这networkx 您可以通过创建邻接矩阵图from_numpy_matrix ,并打印使用边列表edges

A =  np.array([[0, 1, 0, 0, 0, 1], 
               [1, 0, 0, 0, 0, 1], 
               [0, 0, 0, 1, 1, 0], 
               [0, 0, 0, 0, 1, 0],
               [0, 0, 0, 0, 0, 0],
               [1, 0, 0, 0, 0, 0]])

import networkx as nx
g = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
g.edges()
# OutEdgeView([(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)])

您可以将矩阵转换为邻接表,然后打印出节点和连接边:

A = [
    [0, 1, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1],
    [0, 0, 0, 1, 1, 0],
    [0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0],
    [1, 0, 0, 0, 0, 0],
]


def matrix_to_list(matrix):
    """Convert adjacency matrix to adjacency list"""
    graph = {}
    for i, node in enumerate(matrix):
        adj = []
        for j, connected in enumerate(node):
            if connected:
                adj.append(j)
        graph[i] = adj
    return graph


adjacency_list = matrix_to_list(A)
print(adjacency_list)
# {0: [1, 5], 1: [0, 5], 2: [3, 4], 3: [4], 4: [], 5: [0]}


connected_edges = [
    (node, edge) for node, edges in adjacency_list.items() for edge in edges
]
print(connected_edges)
# [(0, 1), (0, 5), (1, 0), (1, 5), (2, 3), (2, 4), (3, 4), (5, 0)]

创建一个大小为邻接矩阵的网格:

nodes = np.arange(len(A))
c_grid, r_grid = np.meshgrid(nodes, nodes)
np.stack([r_grid, c_grid, A])

现在你有了一个维度为 [3, len(nodes), len(nodes)] 的立方体,你可以根据 A 对其进行切片:

almost_edges = coords_adj_mat[:, adj_mat]
edges = almost_edges[:2]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM