使用 networkx 反转邻接矩阵
Invert adjacency matrix using networkx
我有很大的无向图(大约 200k 个节点)。我想反转它的邻接矩阵。因此,如果 A 和 B 在第一张图中连接,则它们不应在新图中连接。我该如何处理?
def invert_graph(G, v_num = v_num):
g_2 = generate_empty_graph(v_num)
for i in range(1, v_num + 1):
if i%100 == 0:
print("Inverting ", i)
non_neighbors = nx.non_neighbors(G, i)
for neighbor in non_neighbors:
g_2.add_edge(i, neighbor)
您似乎在寻找 Complement Graph。您在 NetworkX 中有一个函数可以做到这一点。这是一个例子:
G = nx.Graph()
G.add_edges_from(((1,2),(1,3),(2,4),(4,5),(5,1),(8,2)))
plt.figure(figsize=(12, 5))
nx.draw(G, with_labels=True, node_color='lightgreen',node_size=500)
H = nx.complement(G)
plt.figure(figsize=(12, 5))
nx.draw(H, with_labels=True, node_color='lightgreen',node_size=500)
我有很大的无向图(大约 200k 个节点)。我想反转它的邻接矩阵。因此,如果 A 和 B 在第一张图中连接,则它们不应在新图中连接。我该如何处理?
def invert_graph(G, v_num = v_num):
g_2 = generate_empty_graph(v_num)
for i in range(1, v_num + 1):
if i%100 == 0:
print("Inverting ", i)
non_neighbors = nx.non_neighbors(G, i)
for neighbor in non_neighbors:
g_2.add_edge(i, neighbor)
您似乎在寻找 Complement Graph。您在 NetworkX 中有一个函数可以做到这一点。这是一个例子:
G = nx.Graph()
G.add_edges_from(((1,2),(1,3),(2,4),(4,5),(5,1),(8,2)))
plt.figure(figsize=(12, 5))
nx.draw(G, with_labels=True, node_color='lightgreen',node_size=500)
H = nx.complement(G)
plt.figure(figsize=(12, 5))
nx.draw(H, with_labels=True, node_color='lightgreen',node_size=500)