README.md in union_find-0.0.1 vs README.md in union_find-0.0.2

- old
+ new

@@ -10,14 +10,12 @@ * Pixels in a digital photo * Metallic sites in a composite system Click [here](https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf) for more information. -The running time of this algorithm is linear. +This a Ruby implementation of a modified version of [Robert Sedgewick](http://www.cs.princeton.edu/~rs/)'s and [Kevin Wayne](http://www.cs.princeton.edu/~wayne/contact/)'s [weighted quick-union algorithm with path compression](http://algs4.cs.princeton.edu/15uf/UF.java.html). Credit goes to these two authors of the book [Algorithms](http://www.amazon.com/gp/product/032157351X/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=algs4-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=032157351X) and to the many computer scientists that have contributed to this algorithm in the past decades. -This a Ruby implementation of [Robert Sedgewick](http://www.cs.princeton.edu/~rs/)'s and [Kevin Wayne](http://www.cs.princeton.edu/~wayne/contact/)'s [weighted quick-union algorithm with path compression](http://algs4.cs.princeton.edu/15uf/UF.java.html). Credit goes to these two authors of the book [Algorithms](http://www.amazon.com/gp/product/032157351X/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=algs4-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=032157351X) and to the many computer scientists that have contributed to this algorithm in the past decades. - ## Installation Add this line to your application's Gemfile: gem 'union_find' @@ -30,19 +28,36 @@ $ gem install union_find ## Usage -Create a new instance of `UnionFind` and pass in an array of items: +Create a new instance of `UnionFind` and pass in a `Set` of items: ```ruby -union_find = UnionFind::UnionFind.new(['Grandfather', 'Father', 'Daughter', 'Single Person']) +require 'set' +people = Set.new ['Grandfather', 'Father', 'Daughter', 'Single Person'] +union_find = UnionFind::UnionFind.new(people) ``` +A `Set` is used instead of an `Array` because `Sets` filter out duplicate entries without having to call an expensive `#uniq!` method. If your data comes in form of an `Array`, you can convert it to a `Set` like so: + +```ruby +require 'set' +array = ['Grandfather', 'Father', 'Daughter', 'Single Person'] +set = array.to_set +``` + +Add more items on the fly: + +```ruby +union_find.add('Grandmother') +``` + Connect items (in any order): ```ruby +union_find.union('Grandfather', 'Grandmother') union_find.union('Grandfather', 'Father') union_find.union('Father', 'Daughter') ``` Check whether two items are connected (in any order): @@ -54,15 +69,23 @@ => true union_find.connected?('Grandfather', 'Single Person') => false ``` -Check how many isolated items there are. In this example, there are 2, namely the family the family tree (Grandfather - Father - Daugther) and the Single Person: +Check how many isolated items there are. In this example, there are 2, namely the family (Grandfather - Grandmother - Father - Daugther) and the Single Person: ```ruby union_find.count_isolated_components => 2 ``` + +## Performance + +Initializing a data structure takes constant time: θ(1). + +Afterwards, the `union()` and `connected?()` operations take logarithmic time in the worst case: O(log n). + +The `count_isolated_components()` operation takes constant time: θ(1). ## Contributing 1. Fork it ( https://github.com/[my-github-username]/union_find/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`)