# Ticket #7184: trac_7184-try2.patch

File trac_7184-try2.patch, 3.5 KB (added by AJonsson, 11 years ago)

built against sage 4.1.2, extra typecheck

• ## sage/graphs/graph.py

```# HG changeset patch
# User Anders Jonsson <anders.jonsson@norsjonet.se>
# Date 1255979815 -7200
# Node ID 3a43b671d0f22d63ed0be1447b8b79a956562f7b
# Parent  3f1642b98bbffdeaa3c740e3d1516411c235798f
Count spanning trees. Now string-named vertices work as well.

diff -r 3f1642b98bbf -r 3a43b671d0f2 sage/graphs/graph.py```
 a return self._backend.num_edges(self._directed) num_edges = size def spanning_trees_count(self, root_vertex=None): """ Returns the number of spanning trees in a graph. In the case of a digraph, couts the number of spanning out-trees rooted in ``root_vertex``. Default is to set first vertex as root. This computation uses Kirchhoff's Matrix Tree Theorem [1] to calculate the number of spanning trees. For complete graphs on `n` vertices the result can also be reached using Cayley's formula: the number of spanning trees are `n^(n-2)`. For digraphs, the augmented Kirchhoff Matrix as defined in [2] is used for calculations. Here the result is the number of out-trees rooted at a specific vertex. INPUT: - ``root_vertex`` -- integer (default: the first vertex) This is the vertex that will be used as root for all spanning out-trees if the graph is a directed graph. This argument is ignored if the graph is not a digraph. REFERENCES: - [1] http://mathworld.wolfram.com/MatrixTreeTheorem.html - [2] Lih-Hsing Hsu, Cheng-Kuan Lin, "Graph Theory and Interconnection Networks" AUTHORS: - Anders Jonsson (2009-10-10) EXAMPLES:: sage: G = graphs.PetersenGraph() sage: G.spanning_trees_count() 2000 :: sage: n = 11 sage: G = graphs.CompleteGraph(n) sage: ST = G.spanning_trees_count() sage: ST == n^(n-2) True :: sage: M=matrix(3,3,[0,1,0,0,0,1,1,1,0]) sage: D=DiGraph(M) sage: D.spanning_trees_count() 1 sage: D.spanning_trees_count(0) 1 sage: D.spanning_trees_count(2) 2 """ if self.is_directed() == False: M=self.kirchhoff_matrix() M.subdivide(1,1) M2 = M.subdivision(1,1) return abs(M2.determinant()) else: G=self.copy() if (type(G.vertices()[G.order()-1]) is str): n = G.vertices()[G.order()-1] + "a" else: n = G.vertices()[G.order()-1] + 1 if root_vertex == None: root_vertex=G.vertex_iterator().next() if root_vertex not in G.vertices(): raise ValueError, ("Vertex (%s) not in the graph."%root_vertex) G.add_vertex(n) G.add_edge(n,root_vertex) M=G.kirchhoff_matrix() j=0 for i in G.vertices(): M[j,j]=G.in_degree(i) j= j + 1 M.subdivide(G.order()-1,G.order()-1) M2=M.subdivision(0,0) return abs(M2.determinant()) ### Planarity