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 +4 -4
- data/lib/apsp/version.rb +1 -1
- data/lib/apsp.rb +58 -1
- data/lib/generators/apsp/apsp_generator.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb77f0a64906e985c6e3fa1ad6534973e6465d86
|
4
|
+
data.tar.gz: 0c44f5e66b775fbca76acb795d16a8f5625dbf28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cbea92466b2a879f7e03c36a0b81bf77c6bddfe332c26e49aaeba6c144c1cca944875d6f5ee2ca1df438b0724c29db94c1a3e99db68e67ed377d3063e9b98c5
|
7
|
+
data.tar.gz: 1a1229438d61f6f8e775dcf748a7fa739e26108a799f5ccddf912dfe5b10512ba72c313f2892710a6cebc0f3e9d9dde61a2ad1230b68791fb9d3f7c6735e361b
|
data/lib/apsp/version.rb
CHANGED
data/lib/apsp.rb
CHANGED
@@ -1,5 +1,62 @@
|
|
1
1
|
require "apsp/version"
|
2
2
|
|
3
3
|
module Apsp
|
4
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|