Ask Your Question
1

Looping around self defined indices

asked 2022-08-12 13:52:50 +0100

vidyarthi gravatar image

updated 2022-08-12 13:53:10 +0100

How can we effectively shorten the following piece of code:

g=graphs.CirculantGraph(8,[1,2])
v=g.vertices()
v1=set(v)-set([0,4])
v2=set(v)-set([1,5])
v3=set(v)-set([2,6])
v4=set(v)-set([3,7])
v5=set(v)-set([0,5])
v6=set(v)-set([1,4])
v7=set(v)-set([2,7])
v8=set(v)-set([3,6])
g1=g.subgraph(v1)
g2=g.subgraph(v2)
g3=g.subgraph(v3)
g4=g.subgraph(v4)
g5=g.subgraph(v5)
g6=g.subgraph(v6)
g7=g.subgraph(v7)
g8=g.subgraph(v8)

Like, I wish to loop on the indices g(i). Instead of writing repeatedly g1=subgraph(v1) and so on, is there a way to loop arounfd the self defined index g(i)? Thanks beforehand.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-08-12 16:05:07 +0100

Juanjo gravatar image

You can use list comprehension:

G = graphs.CirculantGraph(8,[1,2])
v = G.vertices()
u = [[0,4], [1,5], [2,6], [3,7], [0,5], [1,4], [2,7], [3,6]]
g = [G.subgraph(set(v) - set(s)) for s in u]

You can refer to the $i$-th subgraph by g[i]. You can loop around as usual in Python. For example,

for sg in g:
    show(sg)

or

for i in range(len(g)):
    show(g[i])
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-08-12 13:52:50 +0100

Seen: 208 times

Last updated: Aug 12 '22