Ho creato il grafo ed aggiunto nodi e archi.
Codice: Seleziona tutto
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
Codice: Seleziona tutto
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
......
cioè ad ogni arco vanno associati tutti i colori possibili, dove u e v sono i nodi e z è il colore. Questo è ciò che ho implementato:
[code]for u,v in G.edges():
for z in Z:
G[u][v]['color'] = z