Git Product home page Git Product logo

Comments (5)

pfcoperez avatar pfcoperez commented on June 14, 2024

Hi @lewismj , I was about to open the a PR with the functionality for disjoint sets when I found yours. I wanted to bring into dogs the implementation I published at
https://github.com/pfcoperez/algorithmaday/tree/master/src/main/scala/org/pfcoperez/dailyalgorithm/datastructures/sets

Which is a pure functional version of the structure described at CLRS. I implemented it in Python in 2012 and provided an small summary of the structure described by Thomas Cormen.

Few days ago I adapted it to a pure functional implementation.
In fact I also provided a state monad set of operation thus making it able to be used using for notation in such a way that the state propagation is transparent for the user:

Use of the sate monad

The point is that I was reviewing your implementation and I see it's simple, it implements union by rank heuristic as well. However it seems to lack the path compression heuristic. That is OK for most cases but it can lead to degraded states where lookups can be as slow as O(n), n = max depth. Check the how the tree depth increases in this example where the path compression is not being applied:

Disjoint sets degradation

I hope you don't mind if open a PR with my version so we can discuss the differences.

from cats-collections.

lewismj avatar lewismj commented on June 14, 2024

Looks good, I thought about implementing the path compression (I mentioned it in the code comment), but decided not to in the end. It was partly because I wanted to avoid bringing in a dependency like Cats or roll my own Monad. I wan't sure about bringing in a dependency on Cats of if dogs had to be standalone... I couldn't decide in the end if it was worth doing or not (edit: I think in the end, and it was personal, it was better to keep a slightly simpler codebase)

from cats-collections.

pfcoperez avatar pfcoperez commented on June 14, 2024

Hi @lewismj , the implementation at https://github.com/pfcoperez/algorithmaday/blob/master/src/main/scala/org/pfcoperez/dailyalgorithm/datastructures/sets/DisjointSets.scala doesn't use Cats, it follows a standalone approach as you suggested, it is curious that we share the same criteria in that regards 😄

It has a wrapper to make it "Cats friendly": https://github.com/pfcoperez/algorithmaday/blob/master/src/main/scala/org/pfcoperez/dailyalgorithm/datastructures/sets/MonadicDisjointSets.scala

Not implementing one of the heuristics might be enough, there are some corner cases leading to the need of path compression. Nevertheless, I fear that path compression is required to keep time complexity bounds.

I've already created a PR (#90) with an implementation including path compression.

Nice relatively unknown data structure, isn't it?

from cats-collections.

anicolaspp avatar anicolaspp commented on June 14, 2024

This has been already merged.

from cats-collections.

larsrh avatar larsrh commented on June 14, 2024

Thanks!

from cats-collections.

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.