disjoint_set 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in disjoint_set.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 tsmsogn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # DisjointSet
2
+
3
+ [![Build Status](https://travis-ci.org/tsmsogn/disjoint_set.svg?branch=master)](https://travis-ci.org/tsmsogn/disjoint_set)
4
+
5
+ Disjoint-set data structure library.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'disjoint_set'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install disjoint_set
22
+
23
+ ## Usage
24
+
25
+ initialize:
26
+
27
+ d = DisjointSet::DisjointSet.new [1, 2, 3, 4]
28
+
29
+ [![Graph](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++1%0D%0A++2%0D%0A++3%0D%0A++4%0D%0A%7D%0D%0A&cht=gv)](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++1%0D%0A++2%0D%0A++3%0D%0A++4%0D%0A%7D%0D%0A&cht=gv)
30
+
31
+ make_set:
32
+
33
+ d.make_set 5
34
+
35
+ [![Graph](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++1%0D%0A++2%0D%0A++3%0D%0A++4%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++1%0D%0A++2%0D%0A++3%0D%0A++4%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)
36
+
37
+ union:
38
+
39
+ d.union 1, 2
40
+
41
+ [![Graph](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++3%0D%0A++4%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++3%0D%0A++4%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)
42
+
43
+ d.union 3, 4
44
+ d.union 1, 3
45
+
46
+ [![Graph](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++4+-%3E+3+-%3E+1%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++4+-%3E+3+-%3E+1%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)
47
+
48
+ find:
49
+
50
+ d.find 1
51
+ => 1
52
+ d.find 2
53
+ => 1
54
+ d.find 4
55
+ => 1
56
+
57
+ [![Graph](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++3+-%3E+1%0D%0A++4+-%3E+1%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)](https://chart.googleapis.com/chart?chl=digraph+g%7B%0D%0A++2+-%3E+1%0D%0A++3+-%3E+1%0D%0A++4+-%3E+1%0D%0A++5%0D%0A%7D%0D%0A&cht=gv)
58
+
59
+ same?:
60
+
61
+ d.same? 1, 2
62
+ => true
63
+ d.same? 1, 5
64
+ => false
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/[my-github-username]/disjoint_set/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'disjoint_set/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "disjoint_set"
8
+ spec.version = DisjointSet::VERSION
9
+ spec.authors = ["tsmsogn"]
10
+ spec.email = ["tsmsogn@gmail.com"]
11
+ spec.summary = %q{Disjoint-set data structure library.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/tsmsogn/disjoint_set"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ require "disjoint_set/version"
2
+ require "disjoint_set/disjoint_set"
3
+
@@ -0,0 +1,45 @@
1
+ module DisjointSet
2
+ # DisjointSet
3
+ class DisjointSet
4
+ attr_reader :parent, :rank
5
+
6
+ def initialize(ids = [])
7
+ @parent, @rank = {}, {}
8
+ ids.each do |id|
9
+ @parent[id] = id
10
+ @rank[id] = 0
11
+ end
12
+ end
13
+
14
+ def make_set(x)
15
+ @parent[x] = x
16
+ @rank[x] = 0
17
+ end
18
+
19
+ def find(x)
20
+ return x if (@parent[x] == x)
21
+ @parent[x] = find(@parent[x])
22
+ @parent[x]
23
+ end
24
+
25
+ def union(x, y)
26
+ x, y, rank_x, rank_y =
27
+ find(x), find(y), @rank[x], @rank[y]
28
+
29
+ return if (x == y)
30
+
31
+ if (rank_x > rank_y)
32
+ @parent[y] = x
33
+ elsif (rank_x < rank_y)
34
+ @parent[x] = y
35
+ else
36
+ @parent[y] = x
37
+ @rank[x] += 1
38
+ end
39
+ end
40
+
41
+ def same?(x, y)
42
+ find(x) == find(y)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module DisjointSet
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe DisjointSet::DisjointSet do
4
+ let(:d1) { DisjointSet::DisjointSet.new [1, 2, 3, 4] }
5
+
6
+ it { is_expected.to respond_to(:parent) }
7
+ it { is_expected.to respond_to(:rank) }
8
+
9
+ it { expect(d1.parent).to eq({1 => 1, 2 => 2, 3 => 3, 4 => 4}) }
10
+ it { expect(d1.rank).to eq({1 => 0, 2 => 0, 3 => 0, 4 => 0}) }
11
+
12
+ describe '#make_set' do
13
+ before { d1.make_set 5 }
14
+ it { expect(d1.parent[5]).to eq(5) }
15
+ it { expect(d1.rank[5]).to eq(0) }
16
+ end
17
+
18
+ describe '#union' do
19
+ before { d1.union 1, 2 }
20
+
21
+ it { expect(d1.parent[1]).to eq(1) }
22
+ it { expect(d1.parent[2]).to eq(1) }
23
+ it { expect(d1.rank[1]).to eq(1) }
24
+ it { expect(d1.rank[2]).to eq(0) }
25
+
26
+ describe 'lower rank set is unioned with higher rank set' do
27
+ before { d1.union 3, 1 }
28
+
29
+ it { expect(d1.parent[1]).to eq(1) }
30
+ it { expect(d1.parent[3]).to eq(1) }
31
+ it { expect(d1.rank[1]).to eq(1) }
32
+ it { expect(d1.rank[3]).to eq(0) }
33
+ end
34
+
35
+ describe 'set\'s root is unioned' do
36
+ before do
37
+ d1.union 3, 4
38
+ d1.union 1, 4
39
+ end
40
+
41
+ it { expect(d1.parent[3]).to eq(1) }
42
+ end
43
+ end
44
+
45
+ describe '#find' do
46
+ before { d1.union 1, 2 }
47
+
48
+ it { expect(d1.parent[1]).to eq(1) }
49
+ it { expect(d1.parent[2]).to eq(1) }
50
+
51
+ describe 'when having rebuildable set' do
52
+ before do
53
+ d1.union 3, 4
54
+ d1.union 1, 3
55
+ end
56
+
57
+ it { expect(d1.parent[4]).to eq(3) }
58
+
59
+ describe 'after find' do
60
+ before { d1.find 4 }
61
+
62
+ it { expect(d1.parent[4]).to eq(1) }
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#same?' do
68
+ it { expect(d1.same?(1, 2)).to eq(false) }
69
+
70
+ describe 'after union' do
71
+ before { d1.union 1, 2 }
72
+
73
+ it { expect(d1.same?(1, 2)).to eq(true) }
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'disjoint_set'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: disjoint_set
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tsmsogn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ''
63
+ email:
64
+ - tsmsogn@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - disjoint_set.gemspec
77
+ - lib/disjoint_set.rb
78
+ - lib/disjoint_set/disjoint_set.rb
79
+ - lib/disjoint_set/version.rb
80
+ - spec/disjoint_set_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/tsmsogn/disjoint_set
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 4275339135669443981
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: 4275339135669443981
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.23
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Disjoint-set data structure library.
113
+ test_files:
114
+ - spec/disjoint_set_spec.rb
115
+ - spec/spec_helper.rb