Graph Theory learning-sequence?

Source: Internet
Author: User

(Havel-Hakimi theorem)

Non-incrementing sequence s consisting of non-negative integers: D1, D2 ,..., DN (n ≥ 2, d1 ≥ 1) is sequential, and only when the sequence
S1: D2-1, D3-1 ,..., DD1 + 1-1, DD1 + 2 ,..., DN
Yes. There are n-1 non-negative integers in sequence S1, and the first D1 degrees (D2 ~ DD1 + 1) after 1 minus
The number of the first D1 in S1.

Ungraphs:

(1) After sorting the remaining sequence, the maximum degree (set to D1) exceeds the number of remaining vertices;

(2) after each of the D1 degrees following the maximum degree is reduced by 1, a negative number is displayed.

For example:

(1) sequence S: 7, 7, 4, 3, 3, 3, 2, the maximum degree is 7, and after the maximum degree is 7, sequence S1: 7, 4, 3, 3, 2, there are only 6 elements, less than 7, not graphs.

(2) sequence S: 7,7, 4,3, 3,3, the maximum degree is 7, sequence s when and only when S1: 6,3, 0 then, s1 limit is only used when S2:, 0,-1 limit, it is clear that S2 has a negative number, S2 cannot be a graph, so s cannot be a graph.

Application:

Frogs'
Neighborhood)
Description:
There are n Lake L1, L2,..., Ln (including the lake without name) in the vicinity of Lake without name. One frog lives in Li of each lake.
FI (1 ≤ I ≤ n ). If there is a waterway between the lake Li and LJ, the frog Fi and FJ are known as neighbors. It is now known that
Number of neighbors x1, x2,..., xn, please give the connection relationship between each two lakes.
Input description:
The first line is the number of test data groups T (0 ≤ T ≤ 20 ). Each data group contains two rows. The first row is an integer N (2 ≤ n ≤ 10 ),
The second row is n integers, x1, x2,..., xn (0 ≤ xi <n ).
Output description:
For each group of input test data, if there is no possible connection, output "no ". Otherwise, "yes" is output and n × n is used.
Indicates the adjacent relationship between lakes. If lake I is connected to Lake J by water, the J number in line I is
1. Otherwise, it is 0. A space is output between two numbers. If there are multiple possibilities, you only need to provide a situation that meets the conditions.
An empty row is output between two adjacent groups of test data.
Sample input:
2
7
4 3 1 5 4 2 1
6
4 3 1 4 2 0

Sample output:

Yes
0 1 1 1 0 0
1 0 0 1 1 0 0
1 0 0 0 0 0 0
1 1 0 0 1 1 1
1 1 0 1 0 1 0
0 0 0 1 1 0 0
0 0 0 1 0 0 0
No


Obviously, the sequence given by this question is the degree of Lake, which can be applied to the HAVEL-HAKIMI theorem.


C ++ code:

# Include <iostream> # include <string> # include <sstream> # define n 15 using namespace STD; // This structure includes the number and degree of each lake. struct node {unsigned int index; int degree;} nodes [N]; int edge [N] [N]; // int CMP (const void * a, const void * B) {return (node *) B)-> degree-(node *)) -> degree;} int main () {int ntest, nlake; //, number of tests, number of lakes bool flag; CIN >>> ntest; string line; while (ntest --) {CIN> nlake; Getline (CIN, line); // jump to the next row Getline (CIN, line); istringstream SS (line ); // initialize for (INT I = 0; I <nlake; I ++) {SS> nodes [I]. degree; nodes [I]. index = I;} memset (edge, 0, sizeof (edge); flag = true; For (INT I = 0; I <nlake & flag; I ++) {// sort the n-I elements after the nodes array in a non-incrementing order. qsort (nodes + I, nlake-I, sizeof (node), CMP ); int maxdegree = nodes [I]. degree; unsigned int Index = nodes [I]. index; // after sorting the remaining sequence, the maximum degree (set to D1) exceeds the number of remaining vertices; If (maxdegree> nLake-i-1) {flag = false; break;} For (int K = 1; k <= maxdegree & flag; ++ K) {Int J = I + k; If (nodes [J]. degree <0) {flag = false; break;} nodes [J]. degree --; edge [Index] [nodes [J]. index] = edge [nodes [J]. index] [Index] = 1 ;}}if (FLAG) {cout <"yes" <Endl; For (INT I = 0; I <nlake; ++ I) {for (Int J = 0; j <nlake; ++ J) {cout <edge [I] [J] <";}cout <Endl ;}} else {cout <"no" <Endl ;}} char c = getchar ();}


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.