uf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b053aef6b5d34bf73ffa0c21d4072b50248043c2773a51399dea7ad7066e9cf7
4
+ data.tar.gz: 55a4e0a312ff7edb0d85555eaffd8f920679553ec9635356956b70b403fbbbac
5
+ SHA512:
6
+ metadata.gz: 87716b024ea9aef01aab44ac7b7dd63498f1ac15c5d0bc30a351520800c01b81f05b81af5aafdbe4ac1556fe1dc30f64e483b893257fdcc8b8824e9437444c39
7
+ data.tar.gz: a9add13ba149578187621835312d0e480a5a001bf4d2d7a8f24781bbd3a6f91a94a06af0c715ae92cc1697e6f8636b9fb8815f450ef9e3cde5f682483764f1de
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nchikkam@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in uf.gemspec
8
+ gemspec
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Uf
2
+
3
+ Union-Find is an algorithm that uses disjointsets with few operations to create:
4
+
5
+ - A union of two give elements forming a new set with the two elements
6
+ - finding weather two elements are with in a same set (in graph terminology, connected? or not)
7
+ - rank or depth of the element with in the tree (from the given element to the leaf) or end of the tree
8
+
9
+
10
+ ## Installation
11
+
12
+ To install the Uf gem add below to your Gemfile
13
+
14
+ ```ruby
15
+ gem 'uf'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install uf
25
+
26
+ ## Usage
27
+ ```
28
+ uf = Uf::DisjointSet.new([1, 2, 3, 4, 5])
29
+
30
+ uf.union(1,2)
31
+
32
+ uf.connected?(1,2)
33
+ ```
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'uf'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/lib/uf/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uf
4
+ VERSION = '0.1.0'
5
+ end
data/lib/uf.rb ADDED
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uf/version'
4
+
5
+ module Uf
6
+ class Error < StandardError; end
7
+ # A hash based representation of Disjoint Sets
8
+ class DisjointSet
9
+ attr_reader :parent, :depth
10
+
11
+ alias rank depth
12
+
13
+ def initialize(elements)
14
+ @parent = {}
15
+ @depth = {}
16
+ create_disjoint_sets(elements)
17
+ end
18
+
19
+ def create_disjoint_sets(elements)
20
+ elements.each do |x|
21
+ parent[x] = x
22
+ depth[x] = 0
23
+ end
24
+ end
25
+
26
+ def find_root(element)
27
+ if parent[element] != element
28
+ parent[element] = find_root(parent[element]) # path compression
29
+ end
30
+ parent[element]
31
+ end
32
+
33
+ def connected?(el_a, el_b)
34
+ find_root(el_a) == find_root(el_b)
35
+ end
36
+
37
+ def union(el_a, el_b)
38
+ root_a = find_root(el_a)
39
+ root_b = find_root(el_b)
40
+ return if root_a == root_b
41
+
42
+ update_depths(root_a, root_b)
43
+ end
44
+
45
+ # make bigger tree set child of small
46
+ def update_depths(root_a, root_b)
47
+ if depth[root_a] > depth[root_b]
48
+ parent[root_b] = root_a
49
+ elsif depth[root_a] < depth[root_b]
50
+ parent[root_a] = root_b
51
+ else
52
+ parent[root_a] = root_b
53
+ depth[root_b] += 1
54
+ end
55
+ end
56
+ end
57
+ end
data/uf.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'uf/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'uf'
9
+ spec.version = Uf::VERSION
10
+ spec.authors = ['narayana']
11
+ spec.email = ['nchikkam@gmail.com']
12
+
13
+ spec.summary = 'A Union Find algorithm with DisjointSet data structure'
14
+ spec.description = 'An efficient way to determine connectedness in graphs\
15
+ Inspiration: R.Sedgewicks link'
16
+ spec.homepage = 'https://github.com'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either
20
+ # set the 'allowed_push_host'
21
+ # to allow pushing to a single host or delete this section to allow
22
+ # pushing to any host.
23
+ if spec.respond_to?(:metadata)
24
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
25
+
26
+ spec.metadata['homepage_uri'] = spec.homepage
27
+ spec.metadata['source_code_uri'] = 'https://github.com'
28
+ spec.metadata['changelog_uri'] = 'https://github.com'
29
+ else
30
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
31
+ 'public gem pushes.'
32
+ end
33
+
34
+ # Specify which files should be added to the gem when it is released.
35
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
36
+ # into git.
37
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
38
+ `git ls-files -z`.split("\x0").reject do |f|
39
+ f.match(%r{^(test|spec|features)/})
40
+ end
41
+ end
42
+ spec.bindir = 'exe'
43
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
44
+ spec.require_paths = ['lib']
45
+
46
+ spec.add_development_dependency 'bundler', '~> 1.17'
47
+ spec.add_development_dependency 'rake', '~> 10.0'
48
+ spec.add_development_dependency 'rspec', '~> 3.0'
49
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - narayana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ An efficient way to determine connectedness in graphs\
57
+ Inspiration: R.Sedgewicks link
58
+ email:
59
+ - nchikkam@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/uf.rb
74
+ - lib/uf/version.rb
75
+ - uf.gemspec
76
+ homepage: https://github.com
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ homepage_uri: https://github.com
82
+ source_code_uri: https://github.com
83
+ changelog_uri: https://github.com
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.0.8
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A Union Find algorithm with DisjointSet data structure
103
+ test_files: []