Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
added matching_polynomial method to BipartiteGraph class
Browse files Browse the repository at this point in the history
  • Loading branch information
pernici committed Jan 8, 2015
1 parent a60cc74 commit e4359bb
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/sage/graphs/bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,66 @@ def plot(self, *args, **kwds):
kwds["pos"] = pos
return Graph.plot(self, *args, **kwds)

def matching_polynomial(self, algorithm="Godsil", complement=True, name=None):
r"""
Computes the matching polynomial.
INPUT:
- ``algorithm`` - a string which must be either "Godsil" (default)
or "rook"; "rook" is usually faster for larger graphs.
- ``complement`` - Boolean (default: ``True``) whether to compute the
rook vector from its complement; it is used in the "rook" algorithm.
- ``name`` - optional string for the variable name in the polynomial.
EXAMPLE::
sage: BipartiteGraph(graphs.CubeGraph(3)).matching_polynomial()
x^8 - 12*x^6 + 42*x^4 - 44*x^2 + 9
::
sage: x = polygen(ZZ)
sage: g = BipartiteGraph(graphs.CompleteBipartiteGraph(16, 16))
sage: factorial(16)*laguerre(16,x^2) == g.matching_polynomial(algorithm='rook')
True
Compute the matching polynomial of a line with `60` vertices::
sage: from sage.functions.orthogonal_polys import chebyshev_U
sage: g = graphs.trees(60).next()
sage: chebyshev_U(60, x/2) == BipartiteGraph(g).matching_polynomial(algorithm='rook')
True
The matching polynomial of a tree graphs is equal to its characteristic
polynomial::
sage: g = graphs.RandomTree(20)
sage: p = g.characteristic_polynomial()
sage: p == BipartiteGraph(g).matching_polynomial(algorithm='rook')
True
"""
if algorithm == "Godsil":
return Graph.matching_polynomial(self, complement=False, name=name)
elif algorithm == "rook":
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
A = self.reduced_adjacency_matrix()
a = A.rook_vector(complement=complement)
m = A.nrows()
n = A.ncols()
b = [0]*(m + n + 1)
for i in range(min(m, n) + 1):
b[m + n - 2*i] = a[i]*(-1)**i
if name is None:
name = 'x'
K = PolynomialRing(A.base_ring(), name)
p = K(b)
return p
else:
raise ValueError('algorithm must be one of "Godsil" or "rook".')

def load_afile(self, fname):
r"""
Loads into the current object the bipartite graph specified in the
Expand Down

0 comments on commit e4359bb

Please sign in to comment.