ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 13 May 2022 09:53:49 +0200Looping in graphshttps://ask.sagemath.org/question/62418/looping-in-graphs/ When I run the code:
l=[1,2,3,4,5]
s=graphs.CompleteGraph(1)
for i in l:
t=s.disjoint_union(graphs.CompleteGraph(i+1))
t.order()
I get the output as $7$, which not as the expected $21$. How do I rectify this? How to loop over graphs? Any hints? Thanks beforehand.Fri, 13 May 2022 08:42:20 +0200https://ask.sagemath.org/question/62418/looping-in-graphs/Comment by David Coudert for <p>When I run the code:</p>
<pre><code>l=[1,2,3,4,5]
s=graphs.CompleteGraph(1)
for i in l:
t=s.disjoint_union(graphs.CompleteGraph(i+1))
t.order()
</code></pre>
<p>I get the output as $7$, which not as the expected $21$. How do I rectify this? How to loop over graphs? Any hints? Thanks beforehand.</p>
https://ask.sagemath.org/question/62418/looping-in-graphs/?comment=62419#post-id-62419It's hard to answer without the exact question. Currently your code is equivalent to
t = graphs.CompleteGraph(1).disjoint_union(graphs.CompleteGraph(6))
Indeed, you always take the disjoint union with `s` without modifying `s`.Fri, 13 May 2022 08:58:52 +0200https://ask.sagemath.org/question/62418/looping-in-graphs/?comment=62419#post-id-62419Answer by tmonteil for <p>When I run the code:</p>
<pre><code>l=[1,2,3,4,5]
s=graphs.CompleteGraph(1)
for i in l:
t=s.disjoint_union(graphs.CompleteGraph(i+1))
t.order()
</code></pre>
<p>I get the output as $7$, which not as the expected $21$. How do I rectify this? How to loop over graphs? Any hints? Thanks beforehand.</p>
https://ask.sagemath.org/question/62418/looping-in-graphs/?answer=62421#post-id-62421In your code, at each loop, the graph `t` is reconstructed from the initial `s`, which is never modified. If you want to accumulate your changes, you need to modify `s`:
l=[1,2,3,4,5]
s=graphs.CompleteGraph(1)
for i in l:
s=s.disjoint_union(graphs.CompleteGraph(i+1))
s.order()
By the way, you can replace `l` with `range(1,6)`Fri, 13 May 2022 09:53:49 +0200https://ask.sagemath.org/question/62418/looping-in-graphs/?answer=62421#post-id-62421