unionf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f0c6481177e23e0f76e9485cf4c875823d6a217
4
+ data.tar.gz: ee1eb653f1fdf2a58173578a82a294917f8fc241
5
+ SHA512:
6
+ metadata.gz: 85cb6a548aa1ab36eaac77643158473a33a4c993d962959cc207a4355b92fe623cb980aa92b6f544b178c6e2d9d36eeb24c247836baed53f7c103be9ed59eece
7
+ data.tar.gz: 063d06eb4bf14fbf215aaffd510ccf037d1b55e356f955904472cac65a89193bc8ddd62ef21ab893faee2a2c656740f26fb022080bc9b2ffbbacca47a6cac14f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in unionf.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Marlon Henry Schweigert and Matheus Valenza
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Unionf
2
+
3
+ In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations (bounded by the inverse Ackermann function) to add new sets, to merge existing sets, and to determine whether elements are in the same set. In addition to many other uses (see the Applications section), disjoint-sets play a key role in Kruskal's algorithm for finding the minimum spanning tree of a graph.
4
+
5
+ A disjoint-set forest consists of a number of elements each of which stores an id, a parent pointer, and, in efficient algorithms, a value called the "rank".
6
+
7
+ The parent pointers of elements are arranged to form one or more trees, each representing a set. If an element's parent pointer points to no other element, then the element is the root of a tree and is the representative member of its set. A set may consist of only a single element. However, if the element has a parent, the element is part of whatever set is identified by following the chain of parents upwards until a representative element (one without a parent) is reached at the root of the tree.
8
+
9
+ Forests can be represented compactly in memory as arrays in which parents are indicated by their array index.
10
+
11
+ Disjoint-set data structures model the partitioning of a set, for example to keep track of the connected components of an undirected graph. This model can then be used to determine whether two vertices belong to the same component, or whether adding an edge between them would result in a cycle. The Union–Find algorithm is used in high-performance implementations of unification.
12
+
13
+ This data structure is used by the Boost Graph Library to implement its Incremental Connected Components functionality. It is also a key component in implementing Kruskal's algorithm to find the minimum spanning tree of a graph.
14
+
15
+ Note that the implementation as disjoint-set forests doesn't allow the deletion of edges, even without path compression or the rank heuristic.
16
+
17
+ Sharir and Agarwal report connections between the worst-case behavior of disjoint-sets and the length of Davenport–Schinzel sequences, a combinatorial structure from computational geometry.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'unionf'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install unionf
34
+
35
+ ## Usage
36
+
37
+ Create a new set:
38
+ ```ruby
39
+ require 'unionf'
40
+
41
+ include Unionf
42
+
43
+ a = UnionFind.new [:marlon, :pamella, :matheus]
44
+ ```
45
+
46
+ The return of a union is the size of the new set created.
47
+ The find method for a set tells you what element represents that set.
48
+
49
+ ```ruby
50
+ a.union :marlon, :pamella # => 2
51
+ a.find :marlon # => :pamella
52
+ a.connected? :marlon, :pamella # => true
53
+ a.connected? :marlon, :matheus # => false
54
+ ```
55
+
56
+ You can also find the size of the set, and the subsets.
57
+
58
+ ```ruby
59
+ a.size # => 4
60
+ a.size? :marlon, :pamella # => 2
61
+ ```
62
+
63
+ You can acquire the elements of the set again.
64
+
65
+ ```ruby
66
+ a.elements # => [:marlon, :pamella, :matheus]
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Schweigert/unionf.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unionf"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+
2
+ module Unionf
3
+
4
+ class UnionFind
5
+
6
+ def initialize elements
7
+ @id = {}
8
+ @sz = {}
9
+ @el = []
10
+ elements.each {|n| @id[n] = n; @sz[n] = 1; @el << n}
11
+ end
12
+
13
+ def connected? a, b
14
+ a, b = pair_search a, b
15
+ a == b
16
+ end
17
+
18
+ def union a, b
19
+ a, b = pair_search a, b
20
+
21
+ return if a == b or a.nil? or b.nil?
22
+
23
+ a, b = b, a if @sz[a] > @sz[b]
24
+
25
+ @id[a] = b
26
+ @sz[a] += @sz[b]
27
+ @sz[b] = @sz[a]
28
+ end
29
+
30
+ def find a
31
+ return a if @id[a] == a
32
+ @id[a] = find @id[a]
33
+ end
34
+
35
+ def size
36
+ @id.size
37
+ end
38
+
39
+ def size? a
40
+ a = find a
41
+ @sz[a]
42
+ end
43
+
44
+ def elements
45
+ @el
46
+ end
47
+
48
+ private
49
+
50
+ def pair_search a, b
51
+ a = find a
52
+ b = find b
53
+ [a,b]
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Unionf
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,50 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "unionf/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "unionf"
7
+ spec.version = Unionf::VERSION
8
+ spec.authors = ["Marlon Henry Schweigert", "Matheus Valenza"]
9
+ spec.email = ["fleyhe0@gmail.com"]
10
+
11
+ spec.summary = %q{Union-find algorithm.}
12
+ spec.description = %q{
13
+ In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations (bounded by the inverse Ackermann function) to add new sets, to merge existing sets, and to determine whether elements are in the same set. In addition to many other uses (see the Applications section), disjoint-sets play a key role in Kruskal's algorithm for finding the minimum spanning tree of a graph.
14
+
15
+ A disjoint-set forest consists of a number of elements each of which stores an id, a parent pointer, and, in efficient algorithms, a value called the "rank".
16
+
17
+ The parent pointers of elements are arranged to form one or more trees, each representing a set. If an element's parent pointer points to no other element, then the element is the root of a tree and is the representative member of its set. A set may consist of only a single element. However, if the element has a parent, the element is part of whatever set is identified by following the chain of parents upwards until a representative element (one without a parent) is reached at the root of the tree.
18
+
19
+ Forests can be represented compactly in memory as arrays in which parents are indicated by their array index.
20
+
21
+ Disjoint-set data structures model the partitioning of a set, for example to keep track of the connected components of an undirected graph. This model can then be used to determine whether two vertices belong to the same component, or whether adding an edge between them would result in a cycle. The Union–Find algorithm is used in high-performance implementations of unification.
22
+
23
+ This data structure is used by the Boost Graph Library to implement its Incremental Connected Components functionality. It is also a key component in implementing Kruskal's algorithm to find the minimum spanning tree of a graph.
24
+
25
+ Note that the implementation as disjoint-set forests doesn't allow the deletion of edges, even without path compression or the rank heuristic.
26
+
27
+ Sharir and Agarwal report connections between the worst-case behavior of disjoint-sets and the length of Davenport–Schinzel sequences, a combinatorial structure from computational geometry.
28
+ }
29
+ spec.homepage = "http://www.github.com/Schweigert/unionf"
30
+ spec.license = "MIT"
31
+
32
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
33
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
34
+ if spec.respond_to?(:metadata)
35
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
36
+ else
37
+ raise "RubyGems 2.0 or newer is required to protect against " \
38
+ "public gem pushes."
39
+ end
40
+
41
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
42
+ f.match(%r{^(test|spec|features)/})
43
+ end
44
+ spec.bindir = "exe"
45
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
46
+ spec.require_paths = ["lib"]
47
+
48
+ spec.add_development_dependency "bundler", "~> 1.15"
49
+ spec.add_development_dependency "rake", "~> 10.0"
50
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unionf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marlon Henry Schweigert
8
+ - Matheus Valenza
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-09-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.15'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.15'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ description: "\n In computer science, a disjoint-set data structure, also called
43
+ a union–find data structure or merge–find set, is a data structure that keeps track
44
+ of a set of elements partitioned into a number of disjoint (non-overlapping) subsets.
45
+ It provides near-constant-time operations (bounded by the inverse Ackermann function)
46
+ to add new sets, to merge existing sets, and to determine whether elements are in
47
+ the same set. In addition to many other uses (see the Applications section), disjoint-sets
48
+ play a key role in Kruskal's algorithm for finding the minimum spanning tree of
49
+ a graph.\n\n A disjoint-set forest consists of a number of elements each of which
50
+ stores an id, a parent pointer, and, in efficient algorithms, a value called the
51
+ \"rank\".\n\n The parent pointers of elements are arranged to form one or more
52
+ trees, each representing a set. If an element's parent pointer points to no other
53
+ element, then the element is the root of a tree and is the representative member
54
+ of its set. A set may consist of only a single element. However, if the element
55
+ has a parent, the element is part of whatever set is identified by following the
56
+ chain of parents upwards until a representative element (one without a parent) is
57
+ reached at the root of the tree.\n\n Forests can be represented compactly in
58
+ memory as arrays in which parents are indicated by their array index.\n\n Disjoint-set
59
+ data structures model the partitioning of a set, for example to keep track of the
60
+ connected components of an undirected graph. This model can then be used to determine
61
+ whether two vertices belong to the same component, or whether adding an edge between
62
+ them would result in a cycle. The Union–Find algorithm is used in high-performance
63
+ implementations of unification.\n\n This data structure is used by the Boost
64
+ Graph Library to implement its Incremental Connected Components functionality. It
65
+ is also a key component in implementing Kruskal's algorithm to find the minimum
66
+ spanning tree of a graph.\n\n Note that the implementation as disjoint-set forests
67
+ doesn't allow the deletion of edges, even without path compression or the rank heuristic.\n\n
68
+ \ Sharir and Agarwal report connections between the worst-case behavior of disjoint-sets
69
+ and the length of Davenport–Schinzel sequences, a combinatorial structure from computational
70
+ geometry.\n "
71
+ email:
72
+ - fleyhe0@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/unionf.rb
85
+ - lib/unionf/version.rb
86
+ - unionf.gemspec
87
+ homepage: http://www.github.com/Schweigert/unionf
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org/
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.11
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Union-find algorithm.
112
+ test_files: []