Ask Your Question
1

Looping in graphs

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

vidyarthi gravatar image

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 flag offensive 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.

David Coudert gravatar imageDavid Coudert ( 2022-05-13 08:58:52 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-13 09:53:49 +0200

tmonteil gravatar image

updated 2022-05-13 09:54:05 +0200

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)

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

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

Seen: 109 times

Last updated: May 13 '22