ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 Mar 2016 10:13:58 -0600Changing vertex labels on a randomly generated graphhttp://ask.sagemath.org/question/32702/changing-vertex-labels-on-a-randomly-generated-graph/ I know that if I want to create a graph on 4 vertices with vertex labels 1,2,3,4 then I can define my graph like this:
G = Graph({0: [1, 3], 1: [2, 3,4], 2: [1,4], 3: [], 4: []})
and then when I print in LaTeX using sagetex the labels show up as 1,2,3,4 but when I try to create a graph randomly
graphs.RandomGNM(4,5)).show(figsize=[4,4], graph_border=True)
the vertex labels are 0,1,2,3. How do I change the default vertex labels of a randomly created graph?Thu, 03 Mar 2016 09:06:39 -0600http://ask.sagemath.org/question/32702/changing-vertex-labels-on-a-randomly-generated-graph/Answer by tmonteil for <p>I know that if I want to create a graph on 4 vertices with vertex labels 1,2,3,4 then I can define my graph like this:
G = Graph({0: [1, 3], 1: [2, 3,4], 2: [1,4], 3: [], 4: []})</p>
<p>and then when I print in LaTeX using sagetex the labels show up as 1,2,3,4 but when I try to create a graph randomly
graphs.RandomGNM(4,5)).show(figsize=[4,4], graph_border=True)
the vertex labels are 0,1,2,3. How do I change the default vertex labels of a randomly created graph?</p>
http://ask.sagemath.org/question/32702/changing-vertex-labels-on-a-randomly-generated-graph/?answer=32703#post-id-32703You can give a name to your graph and then use the `relabel` method:
sage: G = graphs.RandomGNM(4,5)
sage: G.vertices()
[0, 1, 2, 3]
sage: G.relabel({0:'zero' , 1:'one', 2:'two', 3:'three'})
sage: G.vertices()
['one', 'three', 'two', 'zero']
sage: G.show(figsize=[4,4], graph_border=True)
Launched png viewer for Graphics object consisting of 11 graphics primitives
Thu, 03 Mar 2016 10:13:58 -0600http://ask.sagemath.org/question/32702/changing-vertex-labels-on-a-randomly-generated-graph/?answer=32703#post-id-32703