Connectivity problems-algorithms in C Reading Notes

Source: Internet
Author: User

I recently read the book algorithms in C. At the beginning, I read the English version. I feel that the author's description is not easy to understand. I just found a Chinese version, I found that the English version is better. I have read most of the sections in the first chapter, but I haven't summarized them yet. My feeling is that you only need to find a correct Algorithm for a small problem, we do not want to consider the efficiency and performance of algorithms. The advantages and disadvantages of algorithms can be reflected only when solving some large practical problems. In addition, increasing machine performance is far less important than improving algorithm performance.

The first chapter provides an example of connectivity. The author guides us step by step to improve the algorithm so that it can be used in practical problems. The problem is described as follows:

Question Description: enter two integers to represent the two nodes. If the two integers do not establish a connection (including direct connection and other node connection), we will establish a connection between the two nodes, otherwise, enter the next node

The four algorithms are gradually improved as follows:
// Algorithm 1 # include <stdio. h> # define n 10int main (void) {int ID [N]; int T, I, p, q; // You must initialize it for (I = 0; I <n; I ++) {ID [I] = I ;}while (scanf ("% d", & P, & Q) = 2) {If (ID [p] = ID [Q]) continue; t = ID [p]; for (I = 0; I <n; I ++) {If (ID [I] = T) {ID [I] = ID [Q] ;}} for (I = 0; I <n; I ++) {printf ("% d \ t", Id [I]);} printf ("\ n");} return 0 ;}

The data structures used by these four algorithms are arrays. The first algorithm is to assign values to the array elements corresponding to the integers connected (including direct and indirect connections) to the same value.


// Algorithm 2 # include <stdio. h> # define n 10int main (void) {int ID [N]; int I, j, p, q; for (I = 0; I <n; I ++) {ID [I] = I;} while (scanf ("% d", & P, & Q) = 2) {// The following two cycles must be used, otherwise, be careful to fall into an endless loop for (I = P; Id [I]! = I; I = ID [I]); For (j = Q; Id [J]! = J; j = ID [J]); if (I = J) continue; Id [I] = J; for (I = 0; I <N; I ++) {printf ("% d \ t", Id [I]);} printf ("\ n");} return 0 ;}

The data structure used by algorithm 2 is still an array, but the logic is indeed the tree structure. We first find the root node of the tree where the two nodes are located based on the two integers entered, and then check whether the root nodes of the two nodes are the same. If they are the same, they are the same tree, otherwise, connect the two root nodes.


// Algorithm 3 # include <stdio. h> # define n 10int main (void) {int ID [N], SZ [N]; int p, q, I, j; for (I = 0; I <n; I ++) {ID [I] = I; SZ [I] = 1;} while (scanf ("% d", & P, & Q) = 2) {for (I = P; Id [I]! = I; I = ID [I]); For (j = Q; Id [J]! = J; j = ID [J]); If (SZ [I] <SZ [J]) {ID [I] = J; SZ [J] + = SZ [I];} else {ID [J] = I; SZ [I] + = SZ [J];} printf ("I = % d \ nj = % d \ n", I, j); for (I = 0; I <n; I ++) {printf ("% d \ t", SZ [I]);} printf ("\ n"); for (I = 0; I <n; I ++) {printf ("% d \ t", Id [I]);} printf ("\ n");} return 0 ;}

Algorithm 3 is improved based on algorithm 2, but it adds a data structure that records the number of elements in the tree with each node as the root. Through this data structure, each time a small tree is connected to a big tree to prevent the depth of the tree from being too deep.


// Algorithm 4 # include <stdio. h> # define n 10int main (void) {int ID [N]; int SZ [N]; int P, Q, I, J; // initialize for (I = 0; I <n; I ++) {ID [I] = I; SZ [I] = 1 ;} while (scanf ("% d", & P, & Q) = 2) {for (I = P; Id [I]! = I; I = ID [I]) {ID [I] = ID [ID [I];} For (j = Q; Id [J]! = J; j = ID [J]) {ID [J] = ID [ID [J];} If (SZ [I] <SZ [J]) {ID [I] = J; SZ [J] + = SZ [I];} else {ID [J] = I; SZ [I] + = SZ [J];} printf ("I = % d \ nj = % d \ n", I, j); for (I = 0; I <n; I ++) {printf ("% d \ t", SZ [I]);} printf ("\ n"); for (I = 0; I <n; I ++) {printf ("% d \ t", Id [I]);} printf ("\ n");} return 0 ;}

Algorithm 4 is further improved based on algorithm 3, further narrowing down the depth of the tree.

For the part of Chapter 2 algorithm analysis, I am going to leave it for later reading. Now I have no deep experience. For the next part, I am going to start reading the data structure.

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.