dijkstraruby 0.0.2

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dijkstraruby.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dijkstraruby (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.0.9)
10
+ diff-lcs (1.2.4)
11
+ method_source (0.8.2)
12
+ pry (0.9.12.2)
13
+ coderay (~> 1.0.5)
14
+ method_source (~> 0.8)
15
+ slop (~> 3.4)
16
+ rake (10.1.0)
17
+ rspec (2.14.1)
18
+ rspec-core (~> 2.14.0)
19
+ rspec-expectations (~> 2.14.0)
20
+ rspec-mocks (~> 2.14.0)
21
+ rspec-core (2.14.6)
22
+ rspec-expectations (2.14.3)
23
+ diff-lcs (>= 1.1.3, < 2.0)
24
+ rspec-mocks (2.14.4)
25
+ slop (3.4.6)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.3)
32
+ dijkstraruby!
33
+ pry
34
+ rake
35
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Heriberto Perez
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
+ # Dijkstraruby
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dijkstraruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dijkstraruby
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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 new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ #require_relative 'graph.rb'
9
+
10
+ #desc "Find shortest path"
11
+ #task(:get_path) do
12
+ #new_graph =
13
+ #[
14
+ #[:a, :b, 7],
15
+ #[:a, :c, 9],
16
+ #[:a, :f, 14],
17
+ #[:b, :c, 10],
18
+ #[:b, :d, 15],
19
+ #[:c, :d, 11],
20
+ #[:c, :f, 2],
21
+ #[:d, :e, 6],
22
+ #[:e, :f, 9]
23
+ #]
24
+
25
+ #graph = Graph.new(new_graph)
26
+ #result = graph.shortest_path(:a, :e)
27
+ #puts "the shorthest cost is: #{result[1]} and the movements are: "
28
+ #puts result[0].join(' -> ')
29
+ #end
@@ -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 'dijkstraruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dijkstraruby"
8
+ spec.version = Dijkstraruby::VERSION
9
+ spec.authors = ["Heriberto Perez"]
10
+ spec.email = ["heriberto.perez@crowdint.com"]
11
+ spec.description = %q{This gem is an implementation of the dijkstra algorithm}
12
+ spec.summary = %q{dijkstra algorithm in ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,6 @@
1
+ require "dijkstraruby/version"
2
+
3
+ module Dijkstraruby
4
+ autoload :Graph, 'dijkstraruby/graph'
5
+ autoload :Vertex, 'dijkstraruby/vertex'
6
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # this class is responsible to calculate the shorthest path
4
+ module Dijkstraruby
5
+ class Graph
6
+
7
+ attr_reader :vertices
8
+
9
+ def initialize(graph)
10
+ initialize_vertices
11
+ @edges = {}
12
+ format_graph_values graph
13
+ @dijkstra_source = nil
14
+ end
15
+
16
+ def dijkstra(source)
17
+ vertex_values = @vertices.values
18
+ @vertices[source].set_zero_for_initial_vertice
19
+
20
+ until vertex_values.empty?
21
+ initial_vertice = get_vertices_distances vertex_values
22
+ break if initial_vertice.distance == Float::INFINITY
23
+ vertex_values.delete(initial_vertice)
24
+ end
25
+
26
+ @dijkstra_source = source
27
+ end
28
+
29
+ def shortest_path(start, finish)
30
+ dijkstra(start) unless @dijkstra_source == start
31
+ path = []
32
+ first_array_element = finish
33
+ while first_array_element
34
+ path.unshift(first_array_element)
35
+ first_array_element = @vertices[first_array_element].prev_vertice
36
+ end
37
+ [path, @vertices[finish].distance]
38
+ end
39
+
40
+ private
41
+
42
+ def get_vertices_distances(vertex_values)
43
+ initial_vertice = vertex_values.min_by { |vertex| vertex.distance }
44
+ initial_vertice.neighbours.each do |v|
45
+ vv = @vertices[v]
46
+ alt = initial_vertice.distance + @edges[[initial_vertice.name, v]]
47
+ if vertex_values.include?(vv) && alt < vv.distance
48
+ vv.change_distance_and_previous alt, initial_vertice
49
+ end
50
+ end
51
+ initial_vertice
52
+ end
53
+
54
+ def format_graph_values(graph)
55
+ graph.each do |(v1, v2, dist)|
56
+ @vertices[v1].neighbours << v2
57
+ @vertices[v2].neighbours << v1
58
+ @edges[[v1, v2]] = @edges[[v2, v1]] = dist
59
+ end
60
+ end
61
+
62
+ def initialize_vertices
63
+ @vertices = Hash.new { |h, k| h[k] = Vertex.new(k, [], Float::INFINITY) }
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Dijkstraruby
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ # class reponsible for vertices
3
+ #
4
+ module Dijkstraruby
5
+ class Vertex
6
+ attr_accessor :name, :neighbours, :distance, :prev_vertice
7
+
8
+ def initialize(name, neighbours, distance, prev = nil)
9
+ @name = name
10
+ @neighbours = neighbours
11
+ @distance = distance
12
+ @prev_vertice = prev
13
+ end
14
+
15
+ def set_zero_for_initial_vertice
16
+ @distance = 0
17
+ end
18
+
19
+ def change_distance_and_previous(distance, previous_vertice)
20
+ @distance = distance
21
+ @prev_vertice = previous_vertice.name
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ module Dijkstraruby
5
+ describe Graph do
6
+
7
+ let(:graph) do
8
+ Graph.new(
9
+ [
10
+ [:a, :b, 7],
11
+ [:a, :c, 9],
12
+ [:a, :f, 14],
13
+ [:b, :c, 10],
14
+ [:b, :d, 15],
15
+ [:c, :d, 11],
16
+ [:c, :f, 2],
17
+ [:d, :e, 6],
18
+ [:e, :f, 9]]
19
+ )
20
+ end
21
+
22
+ let(:graph_result) { graph.shortest_path(:a, :e) }
23
+
24
+ it 'should contain its neighbours for some vertices' do
25
+ expect(graph.vertices[:a].neighbours).to eq([:b, :c, :f ])
26
+ expect(graph.vertices[:b].neighbours).to eq([:a, :c, :d ])
27
+ end
28
+
29
+ it 'should return the shorthest cost value' do
30
+ expect(graph_result[1]).to eq(20)
31
+ end
32
+
33
+ it 'should return shorthesth route nodes ' do
34
+ format_result = graph_result[0].join(' -> ')
35
+ expect(format_result).to eq('a -> c -> f -> e')
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ module Dijkstraruby
5
+ describe Vertex do
6
+
7
+ let(:vertex) { Vertex.new('heriberto', [], 99_999) }
8
+
9
+ it 'should create a vertex class' do
10
+ expect(vertex.class).to eq Vertex
11
+ end
12
+
13
+ it 'should have a getter and setter for name' do
14
+ expect(vertex.name).to eq 'heriberto'
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ $:<< File.realpath('./lib')
2
+
3
+ require 'dijkstraruby'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dijkstraruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Heriberto Perez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-26 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.3'
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.3'
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: '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: '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
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This gem is an implementation of the dijkstra algorithm
79
+ email:
80
+ - heriberto.perez@crowdint.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .rspec
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - dijkstraruby.gemspec
92
+ - lib/dijkstraruby.rb
93
+ - lib/dijkstraruby/graph.rb
94
+ - lib/dijkstraruby/version.rb
95
+ - lib/dijkstraruby/vertex.rb
96
+ - spec/dijkstraruby/graph_spec.rb
97
+ - spec/dijkstraruby/vertex_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: ''
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.23
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: dijkstra algorithm in ruby
124
+ test_files:
125
+ - spec/dijkstraruby/graph_spec.rb
126
+ - spec/dijkstraruby/vertex_spec.rb
127
+ - spec/spec_helper.rb
128
+ has_rdoc: