图形着色问题:向边缘添加颜色

时间:2018-10-19 12:34:30

标签: python networkx graph-theory graph-coloring

我正在使用networkx来实现边缘着色算法

G = nx.Graph()
G.add_nodes_from ([1,2,3,4,5])

G.add_edge(1,2)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(2,3)
G.add_edge(4,2)
G.add_edge(3,1)
G.add_edge(5,4)
G.add_edge(3,5)

# list of nodes
U = list(G.nodes)

# variable with number of edges
K = G.number_of_edges()
Z = []

# creating a set of available colors. We assume that K = {0, 1, . . . , K − 1}and K ≤ |E|
def nrofCol():
Z.clear()
z = 0
while z < K - 1:
    Z.append(z)
    z = z+1
return Z

现在我必须向代表颜色的每个边缘添加一个变量。每个边缘应具有以下所有可用颜色:

G[u][v][z] = 1,2,0

G[u][v][z] = 1,2,1
G[u][v][z] = 1,2,2
G[u][v][z] = 1,2,3
G[u][v][z] = 1,2,4
G[u][v][z] = 1,2,5
G[u][v][z] = 1,2,6
G[u][v][z] = 1,4,0
G[u][v][z] = 1,4,1
......
where u and v are nodes and z is the color

我尝试过:

for u,v in G.edges():
for z in Z:
    G[u][v]['color'] = z

,但每个边缘仅添加一种颜色。更好的解决方案?

0 个答案:

没有答案