I’m building code for harmonious labeling on Hanoi H_2 graph.
when I try the code there is a repetition of edge labels and vertex labels used are not from 0 to 11 even though the requirement of harmonic labeling is that vertices can be labeled from 0 to q-1 (where q is the number of graph edges, which means the number of Hanoi H_2 graph edges is 12). Is there something wrong with my code?
from random import sample
H2 = nx.Graph()
H2.add_edges_from([(1 ,2), (2 ,3), (3 ,1), (2 ,4), (4 ,5), (5 ,6), (6 ,4),
(6 ,8), (3 ,7), (7 ,8), (8 ,9), (9 ,7)])
plt.figure()
# set random node
node_labels = {node: i for i, node in enumerate(sample(list(H2), H2.number_of_nodes()))}
nx.set_node_attributes(H2, node_labels, 'label')
pos = nx.spring_layout(H2)
nx.draw(H2, pos, edge_color='black', width=1, linewidths=2,
node_size=500, node_color='#add8e6', alpha=1, labels=node_labels)
# Harmonious label for the edges
edge_labels = {(u, v): (node_labels[u] + node_labels[v]) % (H2.number_of_edges()) for u, v in H2.edges}
nx.set_edge_attributes(H2, edge_labels, 'label')
this is my code
New contributor
Gisyaa_9 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.