将 Networkx 图变成字典
turning a Networkx graph into a dictionary
我使用以下函数生成一个 networkx 图:
import networkx as nx
import matplotlib.pyplot as plt
from itertools import combinations, groupby
import random
def gnp_random_connected_graph(n, p):
"""
Generates a random undirected graph, similarly to an Erdős-Rényi
graph, but enforcing that the resulting graph is connected
"""
edges = combinations(range(n), 2)
G = nx.Graph()
G.add_nodes_from(range(n))
if p <= 0:
return G
if p >= 1:
return nx.complete_graph(n, create_using=G)
for _, node_edges in groupby(edges, key=lambda x: x[0]):
node_edges = list(node_edges)
random_edge = random.choice(node_edges)
G.add_edge(*random_edge)
for e in node_edges:
if random.random() < p:
G.add_edge(*e)
return G
然后我想使用以下函数为创建的图形着色:
def coloring(adj, V):
result = [-1] * V
result[0] = 0
available = [False] * V
for y in range(0, V):
for x in adj[y]:
if y not in adj[x]:
adj[x].append(y)
for u in range(1, V):
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = True
cr = 0
while cr < V:
if (available[cr] == False):
break
cr += 1
result[u] = cr
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = False
for u in range(V):
print("Vertec", u, " ---> Color", result[u])
问题是我需要将 networkx 图作为字典,然后通过执行以下操作将其插入 coloring
:
coloring([node for node in g.values()], 5)
其中 g
是作为字典的图
那么如何将 networkx 图变成一个字典,其中节点是键,每个节点连接到的节点是它的关联值?有没有办法在不将 networkx 图转换为字典的情况下做到这一点?
节点:图的平面度不用介意,后面再处理
不确定这是否是您要查找的内容,但使用字典理解可能会有所帮助:
# this creates a dictionary where each node maps to list of neighbours
G_dict = {node: list(G[node]) for node in G}
我使用以下函数生成一个 networkx 图:
import networkx as nx
import matplotlib.pyplot as plt
from itertools import combinations, groupby
import random
def gnp_random_connected_graph(n, p):
"""
Generates a random undirected graph, similarly to an Erdős-Rényi
graph, but enforcing that the resulting graph is connected
"""
edges = combinations(range(n), 2)
G = nx.Graph()
G.add_nodes_from(range(n))
if p <= 0:
return G
if p >= 1:
return nx.complete_graph(n, create_using=G)
for _, node_edges in groupby(edges, key=lambda x: x[0]):
node_edges = list(node_edges)
random_edge = random.choice(node_edges)
G.add_edge(*random_edge)
for e in node_edges:
if random.random() < p:
G.add_edge(*e)
return G
然后我想使用以下函数为创建的图形着色:
def coloring(adj, V):
result = [-1] * V
result[0] = 0
available = [False] * V
for y in range(0, V):
for x in adj[y]:
if y not in adj[x]:
adj[x].append(y)
for u in range(1, V):
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = True
cr = 0
while cr < V:
if (available[cr] == False):
break
cr += 1
result[u] = cr
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = False
for u in range(V):
print("Vertec", u, " ---> Color", result[u])
问题是我需要将 networkx 图作为字典,然后通过执行以下操作将其插入 coloring
:
coloring([node for node in g.values()], 5)
其中 g
是作为字典的图
那么如何将 networkx 图变成一个字典,其中节点是键,每个节点连接到的节点是它的关联值?有没有办法在不将 networkx 图转换为字典的情况下做到这一点?
节点:图的平面度不用介意,后面再处理
不确定这是否是您要查找的内容,但使用字典理解可能会有所帮助:
# this creates a dictionary where each node maps to list of neighbours
G_dict = {node: list(G[node]) for node in G}