ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Aug 2015 16:56:11 -0500delete_edge() Won't Delete Edges from Graphhttp://ask.sagemath.org/question/28772/delete_edge-wont-delete-edges-from-graph/ Title says it all, really. The following code only outputs complete graphs and I can't figure out why.
def SR(G):
H = G
for a in G.vertices():
for b in G.vertices():
H.add_edge(a,b)
for v in G.vertices():
for w in G.vertices():
for n in G[v]:
for m in G[w]:
if H.distance(v, m) >= H.distance(v, w):
G.delete_edge(v , w)
elif H.distance(n, w) H.distance(v, w):
G.delete_edge(v , w)
return G
This is supposed to output the *strong resolving graph* of a particular graph $G$, which can be defined to be the graph formed by taking the vertices of $G$ and making a pair $u,v$ adjacent if for all $n \in N(u)$ and all $n' \in N(v)$, we have $d(n, v) \leq d(u, v)$ and $d(u, n') \leq d(u, v)$. In other words we connect vertices if nothing in their neighborhoods in the original graph is farther. Being new to sage I have no idea why my code above wouldn't work. What do I do?Fri, 07 Aug 2015 15:47:11 -0500http://ask.sagemath.org/question/28772/delete_edge-wont-delete-edges-from-graph/Answer by Nathann for <p>Title says it all, really. The following code only outputs complete graphs and I can't figure out why.</p>
<pre><code>def SR(G):
H = G
for a in G.vertices():
for b in G.vertices():
H.add_edge(a,b)
for v in G.vertices():
for w in G.vertices():
for n in G[v]:
for m in G[w]:
if H.distance(v, m) >= H.distance(v, w):
G.delete_edge(v , w)
elif H.distance(n, w) H.distance(v, w):
G.delete_edge(v , w)
return G
</code></pre>
<p>This is supposed to output the <em>strong resolving graph</em> of a particular graph $G$, which can be defined to be the graph formed by taking the vertices of $G$ and making a pair $u,v$ adjacent if for all $n \in N(u)$ and all $n' \in N(v)$, we have $d(n, v) \leq d(u, v)$ and $d(u, n') \leq d(u, v)$. In other words we connect vertices if nothing in their neighborhoods in the original graph is farther. Being new to sage I have no idea why my code above wouldn't work. What do I do?</p>
http://ask.sagemath.org/question/28772/delete_edge-wont-delete-edges-from-graph/?answer=28773#post-id-28773H = G does not make a copy of the graph. G and H are the very same object in memory, and if you modify one the other also contains the modification. It is the very same memory address. If you want to copy your graph, use H = copy(G).
This is a behaviour that you should expect in any object-oriented language. Fri, 07 Aug 2015 16:56:11 -0500http://ask.sagemath.org/question/28772/delete_edge-wont-delete-edges-from-graph/?answer=28773#post-id-28773