Comments (2)
Consider a union of trees A
and B
that form tree C
. Also consider that tree A
is smaller than B
.
With your idea we would have a tree that is more compressed, with all nodes having C
’s root as parent. In the original approach we would have all nodes from B
pointing to C
’s root and the nodes from A
pointing to A
’s original root, which now points to C
’s root. So we have one less level for A
’s nodes, which is good.
On the other hand, to do that, we have to first find both roots (to know their sizes), the same way as the original approach, and then go through all the nodes on tree A
to update their pointers to B
’s root (instead of just updating the parent of A
’s root to B
’s root).
So we end up with a more compressed tree but we also have to do extra operations (up to N / 2) to go over the smallest tree’s nodes and update their parents.
This makes the union()
operation have linear runtime instead of logarithmic runtime, which would only be useful in an application where most of the operations are find()
operations and union()
is not frequently needed.
from algorithms-sedgewick-wayne.
Actually, since the exercise mentions that the combination of weighted quick-union with path compression results in operations with an amortized cost bounded by the inverse Ackermann function
, the resulting tree heights would always be small. So the original approach still seems to be the best option in all cases.
from algorithms-sedgewick-wayne.
Related Issues (20)
- Error in exercise 1.1.19 as values become bigger than int max value HOT 1
- Ex 1.3.36 - Catenation of Queues/Stacks can be done in constant time HOT 1
- Ex_1.4.26_3Collinearity exceptional case: two different lines whihc have the same slope HOT 1
- Ex 1.4.7 - k++ not considerd for tilde approximation HOT 2
- Ex 2.3.1: omit one extra line HOT 1
- The solution of exercise 4.3.32, specified set, seems to be incorrect for me. HOT 2
- Parent K value if pq[] starts from 0 HOT 1
- object error HOT 1
- 4.1.14 what about two stacks? HOT 2
- 4.1.16. single BFS traversal HOT 1
- 2.2.17 beforeLow usefulness HOT 1
- 4.2.29 Minor improvement suggestion HOT 1
- EXERCISE 3.3.24 - worst case for red-black BSTs HOT 12
- Exercise 6.37 - maxflow with sink removed HOT 1
- Error in Double hashing for linear probing HOT 1
- Potential Issue with 4.4.34 HOT 3
- Clarification Required on find() Access Time in Exercises 1.5.4 and 1.5.3 HOT 1
- Question about Chapter 1, Section 3, Exercise 9 HOT 1
- src/chapter4/section1/Exercise30_EulerianHamiltonianCycles.java HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algorithms-sedgewick-wayne.