Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to add edges while keeping the original graph unchanged

We are aware that the commands for adding vertices and edges in Sage will alter the original graph. For example.

def addedge(g, edges_to_add):
    G1 = g.copy() 
    G1.add_edge(edges_to_add)  
    return G1

I need to add one edge at a time 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.

How to add edges while keeping the original graph unchanged

We are aware I see that the commands for adding vertices and edges in Sage will alter the original graph. For example. 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 add one edge at a time 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. used.

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 at a time 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.