Opened 7 years ago

Closed 7 years ago

# Graph.spanning_trees does not like loops

Reported by: Owned by: Stefan major sage-6.9 graph theory Nathann Cohen Stefan van Zwam N/A 6a86e23 6a86e239c68f0d66cc182a98e74f72b34df4f961

### Description

```G = Graph({0:{0:'a', 1:'b'}, 1:{2: 'c'}, 2:{0: 'e'}}, loops=True)
S = G.spanning_trees()
```

goes "boom" (recursion depth exceeded). It also destroys G in the process, which shouldn't happen, I think.

### comment:1 Changed 7 years ago by Nathann Cohen

Authors: → Nathann Cohen → u/ncohen/19291 → 6a86e239c68f0d66cc182a98e74f72b34df4f961 new → needs_review

Here is a fix. By the way, if you need this function for some hard computations know that there is room for much improvement in there.

Nathann

New commits:

 ​6a86e23 `trac #19291: Graph.spanning_trees does not like loops`

### comment:2 follow-up:  3 Changed 7 years ago by Stefan

Reviewers: → Stefan van Zwam needs_review → positive_review

I was just exploring some graph methods, I don't actually need to use it for now.

Your change forces a copy of the graph to be made regardless of whether there are loops. That's definitely safer than what happened before, but could be costly in terms of memory for big graphs. Still, I prefer the corresponding increase in safety of the code, so I will approve.

### comment:3 in reply to:  2 Changed 7 years ago by David Coudert

but could be costly in terms of memory for big graphs.

Well, considering such running time, it is likely that no one will use this method for "big graphs".

```sage: g = graphs.GridGraph([2,2,2])
sage: %timeit g.spanning_trees()
10 loops, best of 3: 99.4 ms per loop
sage: g = graphs.GridGraph([2,2,3])
sage: %timeit g.spanning_trees()
1 loops, best of 3: 13.4 s per loop
sage: g.order(),g.size()
(12, 20)
```

### comment:4 Changed 7 years ago by Nathann Cohen

Yeah, precisely. Stefan: to answer your question, I did not mind adding a graph copy to this method because, well, given the way it is written, the amount of wasted time is so high that this copy makes no difference. If you ever need this method "seriously", it will have to be reimplemented.

Nathann

### comment:5 Changed 7 years ago by Volker Braun

Branch: u/ncohen/19291 → 6a86e239c68f0d66cc182a98e74f72b34df4f961 → fixed positive_review → closed
Note: See TracTickets for help on using tickets.