ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 19 Apr 2017 18:25:06 +0200Combine two graphs by merging a vertexhttps://ask.sagemath.org/question/37238/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()Sun, 09 Apr 2017 08:23:49 +0200https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/Answer by tmonteil for <p>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.</p>
<p>g=graphs.CompleteGraph(5)
h=graphs.CycleGraph(6)
l=g.set_vertex(4, h)
l.show()</p>
https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?answer=37241#post-id-37241You 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()
Sun, 09 Apr 2017 17:25:24 +0200https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?answer=37241#post-id-37241Comment by Deepak Sarma for <p>You can do the disjoint union and then identify some vertices:</p>
<pre><code>sage: I = g.disjoint_union(h)
sage: I.merge_vertices([(0, 0),(1, 0)])
sage: I.plot()
</code></pre>
https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?comment=37244#post-id-37244Thank 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.Mon, 10 Apr 2017 06:55:20 +0200https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?comment=37244#post-id-37244Comment by tmonteil for <p>You can do the disjoint union and then identify some vertices:</p>
<pre><code>sage: I = g.disjoint_union(h)
sage: I.merge_vertices([(0, 0),(1, 0)])
sage: I.plot()
</code></pre>
https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?comment=37341#post-id-37341Regarding 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.Wed, 19 Apr 2017 18:25:06 +0200https://ask.sagemath.org/question/37238/combine-two-graphs-by-merging-a-vertex/?comment=37341#post-id-37341