[英]printing all the edges of a graph in an adjacency matrix in python
How do you print the all the edges of a graph with a given adjacency matrix in python?如何在 python 中打印具有给定邻接矩阵的图形的所有边? for example, if 0 is adjacent to 3 and 8, it should print: 0 3 0 8 without repetition I've been using Bfs but i don't know how to update the queue and current element.例如,如果 0 与 3 和 8 相邻,它应该不重复地打印:0 3 0 8 我一直在使用 Bfs,但我不知道如何更新队列和当前元素。
This is my code so far到目前为止,这是我的代码
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))
If I understood correctly, and given that your example matrix A
is asymmetric, you could do:如果我理解正确,并且考虑到您的示例矩阵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)
Output输出
(0, 1)
(0, 5)
(1, 0)
(1, 5)
(2, 3)
(2, 4)
(3, 4)
(5, 0)
A simple way would be to iterate over the adjacency matrix, and build a list of tuples with indices where a connection exists:一个简单的方法是迭代邻接矩阵,并构建一个带有索引的元组列表,其中存在连接:
[(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)]
However, you can easily do this with networkx
.但是,您可以使用networkx
轻松做到这networkx
。 You can create a graph from the adjacency matrix using from_numpy_matrix
, and print a list with the edges using edges
:您可以通过创建邻接矩阵图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)])
You could convert the matrix to an adjacency list, then print out the nodes and connecting edges:您可以将矩阵转换为邻接表,然后打印出节点和连接边:
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)]
Create a meshgrid the size of the adjacency matrix:创建一个大小为邻接矩阵的网格:
nodes = np.arange(len(A))
c_grid, r_grid = np.meshgrid(nodes, nodes)
np.stack([r_grid, c_grid, A])
Now you have a cube of dimensions [3, len(nodes), len(nodes)], which you can slice according to 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.