Ask Your Question

Combine two graphs by merging a vertex

asked 2017-04-09 08:23:49 +0200

Deepak Sarma gravatar image

I need to draw a connected graph whose blocks are either complete graphs or cycles. Can I draw my graph by drawing the individual blocks first and then merging the appropriate vertices? For example if I need a connected graph with 10 vertives, where the first 5 vertices form a clique and the last 6 vertices form a cycle. I tried the following, but that didn't work.

g=graphs.CompleteGraph(5) h=graphs.CycleGraph(6) l=g.set_vertex(4, h)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-04-09 17:25:24 +0200

tmonteil gravatar image

You can do the disjoint union and then identify some vertices:

sage: I = g.disjoint_union(h)
sage: I.merge_vertices([(0, 0),(1, 0)])
sage: I.plot()
edit flag offensive delete link more


Thank you. I can get my graph in this process, but my vertices are not in serial order and for larger graph i need to merge vertices again and again. For example consider the following one.

g=Graph() for i in [4,3,5,4,5,6]: g=g+graphs.CompleteGraph(i) g.merge_vertices([3,6,26]) g.merge_vertices([5,7]) g.merge_vertices([11,15]) g.merge_vertices([9,18])

Can the commands of merge_vertices can be combined to a single command? Besides how can I get my vertices in serial order? Is there any way to draw the first complete graph and then to one of its vertices I will attach another complete graph and then to the resulting graph I will attach another complete graph to one of its vertices and so on.... If that is possible, then vertex labeling will be in serial order.

Deepak Sarma gravatar imageDeepak Sarma ( 2017-04-10 06:55:20 +0200 )edit

Regarding your first question, you can write a loop:

sage: to_be_merged = [[3,6,26], [5,7], [11,15], [9,18]]
sage: for s in to_be_merged:
....:     g.merge_vertices(s)

For your second question, you can have a look at the relabel method.

tmonteil gravatar imagetmonteil ( 2017-04-19 18:25:06 +0200 )edit

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


Asked: 2017-04-09 08:23:49 +0200

Seen: 1,210 times

Last updated: Apr 09 '17