# Combine two graphs by merging a vertex 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) l.show()

edit retag close merge delete

Sort by » oldest newest most voted

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

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]) g.show()

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.

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.