Processing math: 100%
Ask Your Question
1

Looping in graphs

asked 2 years ago

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.

Preview: (hide)

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 ( 2 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 2 years ago

tmonteil gravatar image

updated 2 years ago

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)

Preview: (hide)
link

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: 2 years ago

Seen: 180 times

Last updated: May 13 '22