Ask Your Question

# 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.

edit retag close merge delete

## 1 answer

Sort by » oldest newest most voted

Figured 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:[]})


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.

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2012-10-11 03:30:53 -0500

Seen: 332 times

Last updated: Oct 11 '12