dijkstra_gem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dijkstra_gem.rb +83 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5899d8ec631e0010f0f9cf81aa9f339321e2506a
4
+ data.tar.gz: 1ad81af26af9a6c54245028cd0ff01b76bdeed81
5
+ SHA512:
6
+ metadata.gz: 3e33e21b0cf0cde96dee7ef4bc7e5d6a4440846c7fc60a6eeff3e79c567987dd622905072b409b6edb5c96c754f4611796ee4006b3d476893242b89a74f6d302
7
+ data.tar.gz: f735b7b5a1bea33c0c925990c62a992b0ece1216b12aa6841816bfe4870e864fcbba9d2e8d8ce48c48242d6034063e0bf70a3612cd480dc115e8c62eb497764e
@@ -0,0 +1,83 @@
1
+ class DijkstraGem
2
+
3
+ def initialize(nodos)
4
+ @nodos = {}
5
+ @nodes_visited = {}
6
+ @best_way = []
7
+ @best_way_test = []
8
+ @final = 0
9
+ @way = []
10
+ (1..nodos).each do |i|
11
+ @nodos[i] = []
12
+ @nodes_visited[i] = i == 1
13
+ end
14
+ end
15
+
16
+
17
+ def add_distance nodo, points = []
18
+ points.each do |pnt|
19
+ return nil unless pnt.class == Array
20
+ self.distance nodo, pnt[0], pnt[1]
21
+ end
22
+ end
23
+
24
+ def search_best_way a , b
25
+ @final = b
26
+ @way << a
27
+ @best_way = [a,0]
28
+ @best_way_test = [a,0]
29
+ dijkstra(@nodos[a])
30
+ @way.join(" - ")
31
+ end
32
+
33
+ protected
34
+
35
+ def distance nodo, point, dist
36
+ @nodos[nodo] << [point, dist]
37
+ end
38
+
39
+ def dijkstra nodo
40
+ return true if nodo == @nodos[@final]
41
+ begin
42
+ best_nodo = {}
43
+ nodo.each {|n| best_nodo[n[1]] = n[0] unless @nodes_visited[n[0]]}
44
+ _best_nodo = good_way(best_nodo.sort)
45
+ _nodo = _best_nodo[1]
46
+ _dist = _best_nodo[0]
47
+ @nodes_visited[@best_way[0]] = true
48
+ @best_way = [_nodo, _dist]
49
+ @way << _nodo
50
+ dijkstra(@nodos[_nodo])
51
+ rescue Exception => e
52
+ return false
53
+ end
54
+ end
55
+
56
+ def good_way best_nodo
57
+ bn = []
58
+ best_nodo.each do |bst|
59
+ if test_dijkstra(@nodos[bst[1]])
60
+ bn = bst
61
+ break
62
+ end
63
+ end
64
+ @best_way_test = []
65
+ bn
66
+ end
67
+
68
+ def test_dijkstra nodo
69
+ return true if nodo == @nodos[@final]
70
+ begin
71
+ best_nodo = {}
72
+ nodo.each {|n| best_nodo[n[1]] = n[0] unless @nodes_visited[n[0]]}
73
+ best_nodo = best_nodo.sort.to_h
74
+ _nodo = best_nodo.values[0]
75
+ _dist = best_nodo.keys[0]
76
+ @best_way_test = [_nodo, _dist]
77
+ test_dijkstra(@nodos[_nodo])
78
+ rescue Exception => e
79
+ return false
80
+ end
81
+ end
82
+
83
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dijkstra_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - José Núñez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: dijkstra algorithm
14
+ email:
15
+ - josnunezg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dijkstra_gem.rb
21
+ homepage: https://github.com/josnunezg/DijkstraGem.git
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.12
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: My first gem
44
+ test_files: []