Python Forum

Full Version: Graph coloring problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using networkx to implement the edge coloring algorithm. Each edge should have a set of colors.
Here i'm creating the graph:

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
The set of colors is z=[0,1,2,3,4,5,6]. Each edge should have all the available colors as follows:

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


i tried with:

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


but only one color to each edge is added (the last one of the list. A better solution?