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.Thu, 11 Oct 2012 11:53:29 +0200Adding a loop to graphhttps://ask.sagemath.org/question/9413/adding-a-loop-to-graph/I have a for loop that adds edges to a graph `g`, but I also want the graph to add loops. How can I do this, for a general graph whose structure I do not know (that is, I want to add the loop to a graph, rather than make a new graph)?
I've tried using the `g.add_edge(1,1)` function, but that doesn't work, i.e. no loop is added. I also tried using `g.add_edge` three times, then `g.merge_vertices` to contract the cycle to a loop, but also to no avail. Sage justs simplifies the graph.
Any approach to this would be appreciated, direct or indirect. Thu, 11 Oct 2012 10:30:53 +0200https://ask.sagemath.org/question/9413/adding-a-loop-to-graph/Answer by jlv for <p>I have a for loop that adds edges to a graph <code>g</code>, but I also want the graph to add loops. How can I do this, for a general graph whose structure I do not know (that is, I want to add the loop to a graph, rather than make a new graph)?</p>
<p>I've tried using the <code>g.add_edge(1,1)</code> function, but that doesn't work, i.e. no loop is added. I also tried using <code>g.add_edge</code> three times, then <code>g.merge_vertices</code> to contract the cycle to a loop, but also to no avail. Sage justs simplifies the graph.</p>
<p>Any approach to this would be appreciated, direct or indirect. </p>
https://ask.sagemath.org/question/9413/adding-a-loop-to-graph/?answer=14134#post-id-14134Figured it out myself.
My iteration starts with a graph of just one edge, then adds different types of edges to it. Initially, I had
G=Graph({0:[1],1:[]})
I checked in detail through the Sage manual pages on graphs, and found that there are options `multiedges` and `loops` that can be set for a given graph. So now I have
G=Graph(multiedges=True,loops=True)
G.add_edge(0,1)
This gives me exactly what I want. Now `G.add_edge(0,0)` adds a loop to the graph, and `G.add_edge(0,1)` makes it have a pair of multiple edges.
My problem was that Sage assumes `False` values by default for these options, meaning that a graph by default is simple.Thu, 11 Oct 2012 11:53:29 +0200https://ask.sagemath.org/question/9413/adding-a-loop-to-graph/?answer=14134#post-id-14134