The calculation of the number of spanning trees of any graph is a theory kirchhoff, usually called Matrix Tree theorem, which is simple in principle:
Let G is a graph with V (g) ={v1,v2,..., vn},let a={aij}be the adjacentcy matrix of G,and let c={cij}be the N*n matrix, where Cij=deg VI if i=j; Cij=-aij if i!=j; Then the number of spanning trees of G is the Vlaue an any cofactor (cofactor) of C
But the value of the determinant needs to be computed, and here is the trick, so the Lup decomposition method is chosen here to compute.
Package trees; Import matrix.
Determinantcalculator; /** * Calculating the number of spanning trees of any graph is the Kirchhoff theory, usually called Matrix tree theorem * Let G is a graph with V (g) ={v1,v2,..., Vn},let A={aij} Being the adjacentcy matrix of G, * and let c={cij}be the N*n matrix, where Cij=deg VI if i=j; Cij=-aij if i!=j; Then the * Number of spanning trees of G is the Vlaue an any cofactor (cofactor) of C * @author XHW */Public CL Ass Numberofspanningtree {/** * @param args/public static void main (String
[] args) {double a[][]={{0,1,1,0}, {1,0,1,0}, {1,1,0,1},
{0,0,1,0}};
int N=numberofspanningtree (a);
System.out.println ("Numberofspanningtree:" +n); public static int Numberofspanningtree (double[][] a) {double C[][]=generatekirchhoffmatri
X (a); Double Confactor[][]=new double[c.LENGTH-1][C.LENGTH-1];
for (int i=1;i<c.length;i++) {for (int j=1;j<c.length;j++) {
CONFACTOR[I-1][J-1]=C[I][J];
System.out.print (c[i][j]+ "");
}//system.out.println ();
} determinantcalculator dc=new determinantcalculator ();
int n= (int) dc.det (confactor);
return n; }/** * C={cij}be the n*n matrix, where Cij=deg VI if i=j; Cij=-aij if I!=j * More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/* @param A * @retur
n */public static double[][] Generatekirchhoffmatrix (double[][] a) {int length=a.length;
Double C[][]=new Double[length][length];
for (int i=0;i<length;i++) {int deg=0;
for (int j=0;j<length;j++) {deg+=a[i][j]; C[I][J]=-A[I][J];
} c[i][i]=deg;
return C;
} package matrix; /** * matrices and determinants that can be used to compute matrices quickly, because det (A) = det (L) det (U), and the determinant of a triangular matrix is the product of a diagonal element.
If L is required to be a unit triangular matrix, the product of the UII * then the same method can also be applied to lup decomposition, just multiply the p determinant, that is, the corresponding permutation of the symbolic difference.
* @author XHW * * * */public class Determinantcalculator {private Lupdecomposition lu; /** * @param args */public static void main (string[] args) {//TODO auto-generated method stub} public Determinantcalculator () {this.lu=new Lupdec
Omposition ();
Public double det (double a[][]) {a=lu.decomposition (a);
if (A==null) return 0;
Double d=1;
for (int i=0;i<a.length;i++) {d=d*a[i][i]; } int N=lu.getexChangetimes ();
if (n%2==0) return D;
else return-d;
} package matrix; /** * Lup Decomposition * More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/* p is the permutation matrix, L is the lower triangular matrix, and the diagonal is 1,u is the upper triangular matrix P is mainly in order to select the main element, in the selection of ADE non-diagonal elements of the main element to avoid the divisor is 0, * In addition, the value of the divisor can not be too small, otherwise resulting in the calculation of the value instability, so the selected principal is a larger value.
See the algorithm Introduction P461 * @author XHW * */public class lupdecomposition{private int p[];
private int exchangetimes=0; /** * @param args */public static void main (string[] args) {//TODO auto-generated method
stub} public double[][] decomposition (double a[][]) {int length=a.length;
P is the permutation matrix, and the P[I]=J description P of line I of Row J is listed as 1 p=new int [length];
for (int i=0;i<length;i++) {p[i]=i; for (int k=0;k<length;k++) {double max=0;
int maxk=0;
for (int i=k;i<length;i++) {if (Math.Abs (a[i][k)) >max) {
Max=math.abs (A[i][k]);
Maxk=i;
} if (max==0) {System.out.println ("singular matrix");
return null;
} if (K!=MAXK) {//Exchange K and Maxk line Exchange (P,K,MAXK);
exchangetimes++;
for (int i=0;i<length;i++) {double temp=a[k][i];
A[k][i]=a[maxk][i];
A[maxk][i]=temp; The upper part of matrix A is U and the lower part is L for (int i=k+1;i<length;i+
+) {A[i][k]=a[i][k]/a[k][k];
for (int j=k+1;j<length;j++) {A[I][J]=A[I][J]-A[I][K]*A[K][J];
}} return A;
public void Exchange (int p[],int k,int maxk) {int temp=p[k];
P[K]=P[MAXK];
P[maxk]=temp;
Int[] Getp () {return p;
} public void Setp (int[] p) {THIS.P = P;
public int Getexchangetimes () {return exchangetimes;
The public void setexchangetimes (int exchangetimes) {this.exchangetimes = Exchangetimes; }
}