Number of spanning trees for undirected Connected Graphs

Source: Internet
Author: User

We know that each undirected connected graph has its own spanning tree. However, we are more familiar with the undirected graph least spanning tree (MST) algorithm. This article aims to discuss how to calculate the number of spanning trees in a undirected connected graph with the time complexity of O (N3. Another time-efficient recursive method is provided with links at the end of the article.

We can use the Matrix to obtain the number of spanning trees of a undirected connected graph in O (N3) time. For an undirected connected graph, we can list a matrix m according to the following rules:

  1. The M (I, I) value on the primary diagonal is the degree of the I node.
  2. The value of a [I, j] is the opposite number of parallel edges of point I to Point J. Obviously, if I and j are not connected, M (I, j) = 0.

With such a rule, a matrix is established within the time of O (n2.

Take Figure 1 as an example.

In this way, we get the matrix m:

Reference Code (init ):

begin  fillchar(ma,sizeof(ma),0);  readln(n,m);    for i:=1 to n do      for j:=1 to n do          begin            read(x);            if x=1 then               begin                 ma[i,i]:=ma[i,i]+1;                 ma[i,j]:=-1;              end;        end;end.

Well, what is the use of this matrix?

[Theorem]Delete the information of any node in the matrix and obtain the value of the determining factor of the remaining (n-1) * (n-1) matrix. This value is the number of spanning trees of the undirected connected graph.

 

The proof of the theorem will not be repeated here (in fact, I don't know how to prove it). If you want to know the detailed proof process, you can refer to the references attached at the end of the document or view them online.

After the deletion operation, we get the following matrix m ':

However, how can we find the value of the determinant?

We know that the manual method of determining the value is to lower the order of the determining factor by using the following theorem:

[Theorem]The value of D = of the Third-Order Determinant is equal to the sum of all the elements of any row (column) and their corresponding algebraic remainder product.

That is:

Expressed:

When the order is very high, This method requires recursion and stack pressure, not only occupying a large amount of memory, but also the Code complexity is very high (I have not played it yet *. * | ). Here, we use the Gaussian element elimination idea to convert the deciding factor into a lower triangle deciding factor, and use the following properties to obtain the value of the determining factor.

[Nature]The value of the triangle determinant is equal to the product of the elements on the main diagonal.

In this way, we only need to remove the element of the determinant and then obtain the product of the elements on the main diagonal line.

Reference Code (make ):

Begin ans: = 1; for K: = 1 to n do begin ans: = ans * A [K, K]; // records the diagonal product for I: = k + 1 to n do if a [I, K] <> 0 then begin ZOOM: =-A [K, K]/A [I, K]; ans: = ANS/zoom; // ans also need to zoom for J: = K to n do a [I, j]: = A [K, J] + A [I, j] * zoom; // the end; writeln (round (ANS); end;

The following is an example.

 [Problem description]
The night tutor is very strict ...... Well, the tutor is very strict. But as the saying goes, "some birds cannot be shut down ". Xiaoye lives on a small island in the center of a large lake. There are n small islands in the lake, and the islands are connected by bridges. There is at least one path between any two islands. She hasn't gone out for a long time. She wants to play on the shore ...... But before she went to play, she wanted to say hello to her grandparents, uncles, uncles, aunts, big sisters, and younger siblings. So she had to run all the islands in the lake at least once (and of course her own house ), but at night, there was a very evil father who was not allowed to go out to play at night. Dad had enough manpower to control the bridge connecting to the island. After hearing about the night from the informant, at every moment, Dad decided to add a ticket to control a bridge that was not yet under his control. As long as the night passes through these bridges ......
The night was almost caught on the road several times. She found this was a problem, so she turned on her mobile phone and asked for help. Because more and more bridges are controlled by father ye, she thinks that the fewer bridges, the better, so she wants to know how many solutions there are currently available to connect the two islands by selecting n bridges.
[Input format]
The first line of the input file contains two integers, N and M, indicating that the number of islands in the lake except the night house and the night father will gradually send m votes to control the bridge.
Next n + 1 rows, n + 1 integers in each line, the J integer AIJ in line I indicates whether a bridge is directly connected between island I and island J (0 indicates not connected, and 1 indicates connected ).
Next, in the M line, there are two integers A and B in each row. It indicates that he has controlled the bridge directly connected to island A and island B at the current time. (Note: At the beginning, Dad didn't control a bridge .)
[Output format]
The output file has a total of m + 1 rows, each with an integer. Total number of solutions at the current time point.
[Input example]
5 2
0 1 1 1 1 1
1 0 1 1 1 1
1 1 0 1 1 1
1 1 1 0 1 1
1 1 1 1 0 1
4 1
1 3
[Output example]
1296
864
540
[Data Scope]
For 40% of data, 0 <n <13
For 100% of data, 0 <n <26, 0 <m <31
The output data is always within the extended range.

First, note that the solution is not n !. The reason is that the routes are not necessarily chained, but may be tree-like. In this way, the problem is transformed into the problem of finding the number of spanning trees for undirected connected graphs.

Reference code:

Program escape; var A, TA: array [1 .. 30, 1 .. 30] of extended; n, m, x, y, I: integer; ans: extended; Procedure Init; var I, j: integer; begin readln (n, m ); for I: = 1 to n + 1 do for J: = 1 to n + 1 do begin read (x); If x = 1 then begin a [I, I]: = A [I, I] + 1; A [I, j]: =-1; end; TA: = A; end; Procedure make; var I, j, k: integer; ZOOM: extended; begin ans: = 1; for K: = 1 to n do begin ans: = ans * A [K, K]; for I: = k + 1 to n do if a [I, K] <> 0 then begin ZOOM: =-A [K, K]/A [I, K]; ans: = ANS/zoom; for J: = K to n do a [I, j]: = A [K, J] + A [I, j] * zoom; end; end; writeln (round (ANS); end; begin Init; Make; for I: = 1 to M do begin readln (x, y); TA [x, x]: = TA [x, x]-1; TA [y, y]: = TA [y, y]-1; TA [x, y]: = 0; ta [y, X]: = 0; A: = TA; // record the source image make; end.

Reference Links (with recursive methods ):

[1] http://lixu1271985.spaces.live.com/blog/cns! Fcc7dab734a0f517! 290. Entry

[2] http://hi.baidu.com/andimeo/blog/item/2316ff2a24ffe4325343c192.html

 

References:

[1] Gibbons, Alan.Algorithmic graph theory.Cambridge. 1985. 0521288819

 

Address: http://www.cnblogs.com/saltless/archive/2010/11/06/1870778.html

(Saltless original, reprinted please indicate the source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.