How to add edges while keeping the original graph unchanged
I see that the commands for adding vertices and edges in Sage will alter the original graph. So I write the following code which makes the origin graph unchanged.
def addedge(g, edges_to_add):
G1 = g.copy()
G1.add_edge(edges_to_add)
return G1
I need to sequentially add one edge from the complement of the graph to observe their properties, such as Hamiltonicity.
def addedge(g, edges_to_add):
G1 = g.copy()
G1.add_edge(edges_to_add)
return G1
s = 'J~f[MN@PQg?'
G = Graph(s, sparse=True);
complement_edges = G.complement().edges(labels=False, sort=False)
for edge in complement_edges:
G1=addedge(G,edge)
if not G1.is_hamiltonian():
print("The modified graph is non-Hamiltonian with the added edge:", edge)
else:
print()
#print("The modified graph is Hamiltonian with the added edge:",edge)
I am not sure if this edge addition is highly efficient because, as we discussed in previous issues (see common in the answer )(calculate-the-toughness-of-a-graph, it's best to avoid copy()
if it's being repeatedly used.