Note: This article is an improved version of the previous article "C Language Algorithm 1.3: Advanced Improved Version of Connectivity Problem [Video Analysis]" , it is recommended to read the previous article before reading this article.
1. Source
Program 1.4 from the book "Algorithms: Implementation in C (Parts 1-4) 3rd Edition".
Second, the difference between the two versions
In version 1.3, if the two nodes are already connected, no processing will be done.
In version 1.4, if the two nodes are already connected, processing will still be done to reduce the depth of the tree, so that the number of searches to find the root node will be reduced.
3. Performance test results
Number of nodes | Version 1.3 takes time (milliseconds) | Version 1.4 takes time (milliseconds) | time-consuming ratio |
---|---|---|---|
1000000 | 32 | 19 | 1.68 |
10 million | 257 | 134 | 1.91 |
- The higher the number of nodes, the higher the performance improvement.
- The upper limit of the boost is 2 times.
4. Source code
There are many source files, so I won’t post them in the article. The links are here: github , gitee
5. Schematic diagram of the process
end of text