Ask Your Question

# 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.

edit retag close merge delete

## Comments

It'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.

( 2022-05-13 08:58:52 +0200 )edit

## 1 Answer

Sort by ยป oldest newest most voted

In 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)

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2022-05-13 08:42:20 +0200

Seen: 132 times

Last updated: May 13 '22