Ask Your Question
0

Adding a loop to graph

asked 2012-10-11 10:30:53 +0100

updated 2012-10-11 10:32:59 +0100

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-10-11 11:53:29 +0100

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],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.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-10-11 10:30:53 +0100

Seen: 1,063 times

Last updated: Oct 11 '12