Giter Site home page Giter Site logo

deehzee / unionfind Goto Github PK

View Code? Open in Web Editor NEW
62.0 62.0 20.0 8 KB

A union-find disjoint sets data structure implemented in Python with the "Weighted Quick Union with Path Compression" algorithm.

License: MIT License

Jupyter Notebook 49.76% Python 50.24%
algorithms-datastructures union-by-rank-and-path-compression union-find

unionfind's People

Contributors

deehzee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

unionfind's Issues

components and component_mapping methods can be speed up by a factor of len(distinct_roots)

When you group elements into components (in methods components and component_mapping), the time complexity of your current way of doing it is O(N x M) (where N is len(self._elts) and M is len(distinct_roots)).
O(N x M) because looping over distinct roots is O(M) and boolean indexing over elements elts[roots == root] is O(N).
The optimized code below using a defaultdict (hashmap) has time complexity O(N). The higher M is, the bigger is the speed up.

from collections import defaultdict

def components(self):
	dict_components = defaultdict(list)
	for elem in self._elts:
		root = self.find(elem)
		dict_components[root].append(elem)
	return dict_components

pip package support

Hi, thanks for your helpful codes at first. I'm wondering if you are interested in upload your code into a pip installable package. Maybe I can offer help on this.

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.