Giter Club home page Giter Club logo

Comments (2)

reneargento avatar reneargento commented on August 16, 2024

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.

reneargento avatar reneargento commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.