apsp 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22e296c98db059ae8f9cf0c54b5f944d5d7f262b
4
- data.tar.gz: 839f0985b87143532a6cd4b05e68f9e4e07d0f44
3
+ metadata.gz: eb77f0a64906e985c6e3fa1ad6534973e6465d86
4
+ data.tar.gz: 0c44f5e66b775fbca76acb795d16a8f5625dbf28
5
5
  SHA512:
6
- metadata.gz: ea1ab4bfa99bcd3617798876ca9d251d44bb4468d4ecfaf05b701e91eba55c9611d0bb376ebc53b7ea21d7fa781d697d3b8cdf3b94eb628e4872db11d17bcbf1
7
- data.tar.gz: 2f028c2c32a76a1f2e2db7214b765d979e4544915e54cb7e724af12be5fbc0db04ccbed50749a168d8cb8fe2db87c8c68f83c0dbb4e14c1d000c95918c276854
6
+ metadata.gz: 3cbea92466b2a879f7e03c36a0b81bf77c6bddfe332c26e49aaeba6c144c1cca944875d6f5ee2ca1df438b0724c29db94c1a3e99db68e67ed377d3063e9b98c5
7
+ data.tar.gz: 1a1229438d61f6f8e775dcf748a7fa739e26108a799f5ccddf912dfe5b10512ba72c313f2892710a6cebc0f3e9d9dde61a2ad1230b68791fb9d3f7c6735e361b
data/lib/apsp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Apsp
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/apsp.rb CHANGED
@@ -1,5 +1,62 @@
1
1
  require "apsp/version"
2
2
 
3
3
  module Apsp
4
- # Your code goes here...
4
+ class ShortestPath
5
+
6
+ def self.shortest_path(from, to)
7
+ @form = form
8
+ @path = 0
9
+ @from_id = from.id.to_i
10
+ @to_id = to.id.to_i
11
+ if (from.id != @to_id)
12
+ from = @form.class.find(@from_id)
13
+ @hash_to_push = {}
14
+ @visiting_queue = []
15
+ @path = []
16
+ @hash_to_push[@from_id.to_s] = 0
17
+ @visiting_queue << @from_id
18
+ find_path(from)
19
+ if @success
20
+ create_path(@to_id)
21
+ @path.reverse!
22
+ return @path
23
+ else
24
+ false
25
+ end
26
+ end
27
+ end
28
+
29
+ def find_path(check_station)
30
+ # p "@hash_to_push", @hash_to_push
31
+ # p "@visiting_queue", @visiting_queue
32
+
33
+ check_station.connections.each do|s|
34
+ if s.connect_id == @to_id
35
+ @hash_to_push[s.connect_id.to_s] = check_station.id
36
+ @success = true
37
+ return
38
+ elsif !@hash_to_push.has_key?(s.connect_id.to_s)
39
+ @hash_to_push[s.connect_id.to_s] = check_station.id
40
+ @visiting_queue << s.connect_id
41
+ end
42
+ end
43
+ @visiting_queue.shift
44
+ if @visiting_queue.length>0
45
+ find_path(@form.class.find(@visiting_queue[0]))
46
+ else
47
+ @success = false
48
+ return
49
+ end
50
+ end
51
+
52
+ def create_path(key)
53
+ @path << @form.class.find(key).name
54
+ if key == @from_id
55
+ return
56
+ else
57
+ create_path(@hash_to_push[key.to_s])
58
+ end
59
+ end
60
+
61
+ end
5
62
  end
@@ -48,7 +48,7 @@ FOO
48
48
  else
49
49
  puts "Unable to open file!"
50
50
  end
51
- model_file_path = "app/models/#{name.underscore}.rb"
51
+ model_file_path = "app/models/#{name.underscore}.rb"
52
52
  m = File.open(model_file_path, &:gets)
53
53
 
54
54
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kolekar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-19 00:00:00.000000000 Z
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler