kruskal 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd20b4362b6c5fcf9efdcc723244b8df836ec8c8
4
+ data.tar.gz: 11f27c690c65a0b9846267025544fe4cc2ccff95
5
+ SHA512:
6
+ metadata.gz: f87b054d690151fce0bb4dbbb8184c892540de7b13c5e78e6473c693f6eee76f8e7d1a4ae5401e1a240cecde6377fdbb58496e879566201bef5772a8de064b37
7
+ data.tar.gz: 446324a2b11da1deaa56faffe592d8e96276b3d0ccf10eeff47c2fc6077bb5c54d46bf3710d11104af59eb66a8b2a8ed27e89818a14ee2bc1445b298bebcc80d
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ kruskal
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ watch('lib/kruskal.rb') { "spec" }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ronie Uliana
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,29 @@
1
+ # .
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem '.'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install .
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/./fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/kruskal ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require 'kruskal'
3
+
4
+ kruskal = Kruskal::MinimumSpanningTree.new
5
+ reader = Kruskal::JsonIO.new(ARGF)
6
+
7
+ kruskal.run(reader)
8
+
9
+ writer = Kruskal::JsonIO.new(STDOUT)
10
+ kruskal.forest.each do |tree|
11
+ tree.each do |relation|
12
+ writer << relation
13
+ end
14
+ end
data/kruskal.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kruskal/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kruskal'
8
+ spec.version = Kruskal::VERSION
9
+ spec.authors = ['Ronie Uliana']
10
+ spec.email = ['ronie.uliana@gmail.com']
11
+ spec.summary = %q{Kruskal Minimum Spanning Tree algorithm}
12
+ spec.description = %q{Graph Minimum Spanning Tree implementation using a variation of Kruskal algorithm. Intended to be used directly from command line or as a lib.}
13
+ spec.homepage = 'http://github.com/ruliana/kruskal'
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_dependency 'json', '~> 1.8'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
+ spec.add_development_dependency 'guard-rspec', '~> 4.2'
25
+ end
data/lib/..rb ADDED
@@ -0,0 +1,5 @@
1
+ require "./version"
2
+
3
+ module .
4
+ # Your code goes here...
5
+ end
data/lib/kruskal.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'kruskal/tree'
2
+ require 'kruskal/forest'
3
+ require 'kruskal/minimum_spanning_tree'
4
+ require 'kruskal/json_io'
@@ -0,0 +1,40 @@
1
+ module Kruskal
2
+ class Forest
3
+ include Enumerable
4
+
5
+ def initialize
6
+ @trees = {}
7
+ end
8
+
9
+ def index(node, tree)
10
+ @trees[node] = tree
11
+ @cached_to_a = nil
12
+ end
13
+
14
+ def new_tree(value = 0, source, target)
15
+ Tree.new(self, [[value, source, target]])
16
+ end
17
+
18
+ def to_a
19
+ @cached_to_a ||= @trees.values.uniq
20
+ end
21
+
22
+ def find_tree_for(node)
23
+ @trees[node]
24
+ end
25
+
26
+ def [](position)
27
+ to_a[position]
28
+ end
29
+
30
+ def each
31
+ to_a.each do |tree|
32
+ yield tree
33
+ end
34
+ end
35
+
36
+ def size
37
+ to_a.size
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ require 'json'
2
+
3
+ module Kruskal
4
+ class JsonIO
5
+ include Enumerable
6
+
7
+ def initialize(an_io)
8
+ @origin = an_io
9
+ @first = true
10
+ end
11
+
12
+ def each
13
+ @origin.each_line do |it|
14
+ yield JSON.parse(it)
15
+ end
16
+ end
17
+
18
+ def <<(object)
19
+ a_string = if @first
20
+ @first = false
21
+ JSON.generate(object)
22
+ else
23
+ "\n#{JSON.generate(object)}"
24
+ end
25
+ @origin << a_string
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ module Kruskal
2
+ class MinimumSpanningTree
3
+ attr_accessor :forest
4
+
5
+ def initialize
6
+ @forest = Forest.new
7
+ end
8
+
9
+ def add(value = 0, source, target)
10
+ # Here is the full algorithm
11
+ tree_source = find_tree_for(source)
12
+ tree_target = find_tree_for(target)
13
+ if tree_source.nil? && tree_target.nil?
14
+ new_tree(value, source, target)
15
+ elsif tree_source && tree_target.nil?
16
+ tree_source.add(value, source, target)
17
+ elsif tree_source.nil? && tree_target
18
+ tree_target.add(value, source, target)
19
+ elsif tree_source != tree_target
20
+ tree_source.merge!(tree_target)
21
+ tree_source.add(value, source, target)
22
+ end
23
+ end
24
+
25
+ def run(data)
26
+ data.each do |relation|
27
+ add(*relation)
28
+ end
29
+ end
30
+
31
+ private
32
+ def find_tree_for(node)
33
+ @forest.find_tree_for(node)
34
+ end
35
+
36
+ def new_tree(value, source, target)
37
+ @forest.new_tree(value, source, target)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,67 @@
1
+ module Kruskal
2
+ class Tree
3
+ extend Forwardable
4
+ include Enumerable
5
+
6
+ def_delegators :@relations, :each
7
+
8
+ def initialize(index, relations = [])
9
+ @index, @relations = index, relations
10
+ reindex
11
+ end
12
+
13
+ def eql?(other)
14
+ if other.is_a?(self.class)
15
+ other.relations_equal_to(@relations)
16
+ elsif other.is_a?(Array)
17
+ @relations == other
18
+ else
19
+ false
20
+ end
21
+ end
22
+
23
+ alias == eql?
24
+
25
+ def hash
26
+ @relations.hash
27
+ end
28
+
29
+ def add(value, source, target)
30
+ @relations << [value, source, target]
31
+ index(target)
32
+ end
33
+
34
+ def merge!(other_tree)
35
+ other_tree.add_relations_to(@relations)
36
+ other_tree.each do |(v, s, t)|
37
+ index(s, t)
38
+ end
39
+ end
40
+
41
+ def inspect
42
+ "Tree#{@relations.inspect}"
43
+ end
44
+
45
+ protected
46
+ def add_relations_to(an_array)
47
+ an_array.concat(@relations)
48
+ end
49
+
50
+ def relations_equal_to(an_array)
51
+ an_array == @relations
52
+ end
53
+
54
+ private
55
+ def index(*nodes)
56
+ nodes.each do |it|
57
+ @index.index(it, self)
58
+ end
59
+ end
60
+
61
+ def reindex
62
+ @relations.each do |(v, s, t)|
63
+ index(s, t)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Kruskal
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kruskal::JsonIO do
4
+ it 'reads JSON in lines, outputs Ruby objects' do
5
+ input = StringIO.new("[1, 2]\n[3, 4]\n[5, 6]")
6
+ reader = Kruskal::JsonIO.new(input)
7
+ objects = reader.map { |it| it }
8
+ expect(objects.size).to eq 3
9
+ expect(objects.first).to eq [1, 2]
10
+ expect(objects.last).to eq [5, 6]
11
+ end
12
+
13
+ it 'writes object as JSON' do
14
+ output = StringIO.new
15
+ writer = Kruskal::JsonIO.new(output)
16
+ writer << [1, 2]
17
+ writer << [3, 4]
18
+ expect(output.string).to eq "[1,2]\n[3,4]"
19
+ end
20
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kruskal::MinimumSpanningTree do
4
+ let (:kruskal) { Kruskal::MinimumSpanningTree.new }
5
+
6
+ describe '#add' do
7
+ it 'creates a forest' do
8
+ kruskal.add('a', 'b')
9
+ expect(kruskal.forest.size).to eq 1
10
+ expect(kruskal.forest.first).to eq [[0, 'a', 'b']]
11
+ end
12
+
13
+ it 'creates more forests' do
14
+ kruskal.add('a', 'b')
15
+ kruskal.add('c', 'd')
16
+ expect(kruskal.forest.size).to eq 2
17
+ expect(kruskal.forest).to include [[0, 'a', 'b']]
18
+ expect(kruskal.forest).to include [[0, 'c', 'd']]
19
+ end
20
+
21
+ it 'expands a forest' do
22
+ kruskal.add('a', 'b')
23
+ kruskal.add('b', 'c')
24
+ expect(kruskal.forest.size).to eq 1
25
+ expect(kruskal.forest.first).to eq [[0, 'a', 'b'], [0, 'b', 'c']]
26
+ end
27
+
28
+ it 'joins forests' do
29
+ kruskal.add('a', 'b')
30
+ kruskal.add('c', 'd')
31
+ kruskal.add('d', 'a')
32
+ expect(kruskal.forest.size).to eq 1
33
+ tree = kruskal.forest.first
34
+ expect(tree).to include [0, 'a', 'b']
35
+ expect(tree).to include [0, 'c', 'd']
36
+ expect(tree).to include [0, 'd', 'a']
37
+ end
38
+
39
+ it 'ignores connections in the same forest' do
40
+ kruskal.add('a', 'b')
41
+ kruskal.add('c', 'd')
42
+ kruskal.add('d', 'a')
43
+ kruskal.add('a', 'c')
44
+ tree = kruskal.forest.first
45
+ expect(tree).to include [0, 'd', 'a']
46
+ expect(tree).to_not include [0, 'a', 'c']
47
+ end
48
+ end
49
+
50
+ describe '#run' do
51
+ it 'creates a single minimum spanning tree' do
52
+ data = [[10, 'a', 'b'],
53
+ [9, 'c', 'd'],
54
+ [8, 'b', 'c'],
55
+ [7, 'b', 'd']]
56
+ kruskal.run(data)
57
+ expect(kruskal.forest.size).to eq 1
58
+
59
+ tree = kruskal.forest.first
60
+ expect(tree).to include [10, 'a', 'b']
61
+ expect(tree).to include [9, 'c', 'd']
62
+ expect(tree).to include [8, 'b', 'c']
63
+ expect(tree).to_not include [7, 'b', 'd']
64
+ end
65
+
66
+ it 'creates more trees when not nodes do not connecte' do
67
+ data = [[10, 'a', 'b'],
68
+ [9, 'c', 'd'],
69
+ [8, 'e', 'f'],
70
+ [7, 'g', 'h'],
71
+ [6, 'd', 'a'],
72
+ [5, 'e', 'g']]
73
+ kruskal.run(data)
74
+ expect(kruskal.forest.size).to eq 2
75
+
76
+ tree1 = kruskal.forest[0]
77
+ expect(tree1).to include [10, 'a', 'b']
78
+ expect(tree1).to include [9, 'c', 'd']
79
+ expect(tree1).to include [6, 'd', 'a']
80
+ expect(tree1).to_not include [8, 'e', 'f']
81
+ expect(tree1).to_not include [7, 'g', 'h']
82
+ expect(tree1).to_not include [5, 'e', 'g']
83
+
84
+ tree2 = kruskal.forest[1]
85
+ expect(tree2).to_not include [10, 'a', 'b']
86
+ expect(tree2).to_not include [9, 'c', 'd']
87
+ expect(tree2).to_not include [6, 'd', 'a']
88
+ expect(tree2).to include [8, 'e', 'f']
89
+ expect(tree2).to include [7, 'g', 'h']
90
+ expect(tree2).to include [5, 'e', 'g']
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kruskal::Tree do
4
+ let(:forest) { Kruskal::Forest.new }
5
+
6
+ describe '#==' do
7
+ it 'compares content' do
8
+ t1 = forest.new_tree('a', 'b')
9
+ t2 = forest.new_tree('a', 'b')
10
+ t3 = forest.new_tree('a', 'c')
11
+
12
+ expect(t1).to eq t1
13
+ expect(t1).to eq t2
14
+ expect(t1).to_not eq t3
15
+ end
16
+
17
+ it 'compares with Array' do
18
+ t1 = forest.new_tree('a', 'b')
19
+
20
+ expect(t1).to eq [[0, 'a', 'b']]
21
+ end
22
+ end
23
+
24
+ describe '#hash' do
25
+ it 'is the same for same content' do
26
+ t1 = forest.new_tree('a', 'b')
27
+ t2 = forest.new_tree('a', 'b')
28
+ t3 = forest.new_tree('a', 'c')
29
+
30
+ expect(t1.hash).to eq t2.hash
31
+ expect(t1.hash).to_not eq t3.hash
32
+ end
33
+ end
34
+
35
+ describe 'merge!' do
36
+ it 'adds the content of one tree to the other' do
37
+ t1 = forest.new_tree('a', 'b')
38
+ t2 = forest.new_tree('b', 'c')
39
+
40
+ t1.merge!(t2)
41
+
42
+ expect(t1).to include [0, 'a', 'b']
43
+ expect(t1).to include [0, 'b', 'c']
44
+ end
45
+ end
46
+ end
@@ -0,0 +1 @@
1
+ require 'kruskal'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kruskal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ronie Uliana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ description: Graph Minimum Spanning Tree implementation using a variation of Kruskal
56
+ algorithm. Intended to be used directly from command line or as a lib.
57
+ email:
58
+ - ronie.uliana@gmail.com
59
+ executables:
60
+ - kruskal
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".ruby-gemset"
66
+ - ".ruby-version"
67
+ - Gemfile
68
+ - Guardfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/kruskal
73
+ - kruskal.gemspec
74
+ - lib/..rb
75
+ - lib/kruskal.rb
76
+ - lib/kruskal/forest.rb
77
+ - lib/kruskal/json_io.rb
78
+ - lib/kruskal/minimum_spanning_tree.rb
79
+ - lib/kruskal/tree.rb
80
+ - lib/kruskal/version.rb
81
+ - spec/lib/kruskal/json_io_spec.rb
82
+ - spec/lib/kruskal/minimum_spanning_tree_spec.rb
83
+ - spec/lib/kruskal/tree_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: http://github.com/ruliana/kruskal
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Kruskal Minimum Spanning Tree algorithm
109
+ test_files:
110
+ - spec/lib/kruskal/json_io_spec.rb
111
+ - spec/lib/kruskal/minimum_spanning_tree_spec.rb
112
+ - spec/lib/kruskal/tree_spec.rb
113
+ - spec/spec_helper.rb