graphos 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphos/algorithm/prim.rb +3 -1
- data/lib/graphos/binary_heap.rb +12 -6
- data/lib/graphos/version.rb +1 -1
- data/test/binary_heap_test.rb +53 -0
- data/test/prim_test.rb +12 -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: 1ba60b53a39cf6c34299ed5a55f7372618c1a4c2
|
4
|
+
data.tar.gz: 89cb915d41ae66525516851b699187cff654caa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1ba13a86d3c753a036db4fcbf844ade4959b28a5c12cbc1a2ddb568990adf2f04a51b21db188e147979e695145f8d0fa5880e80b203a2b48dcb7d0f7e578a73
|
7
|
+
data.tar.gz: 50f395bccfe7da8e2cb0d41b4fba2ff05ff11967228fc65109a8f1e240d922763589e951cc74cc18f7f8facd78e40c4215610344a0836d6423ec74179a28f1f1
|
@@ -12,6 +12,9 @@ module Graphos
|
|
12
12
|
|
13
13
|
heap = BinaryHeap.new{|x,y| x.value <=> y.value}
|
14
14
|
heap.push(initial, 0)
|
15
|
+
costs.each_with_index do |v,i|
|
16
|
+
heap.push(i,v)
|
17
|
+
end
|
15
18
|
|
16
19
|
visited = Array.new(graph.size, false)
|
17
20
|
|
@@ -40,7 +43,6 @@ module Graphos
|
|
40
43
|
fathers.each_with_index do |f,c|
|
41
44
|
if f
|
42
45
|
result.add_edge(f, c, costs[c])
|
43
|
-
count += 1
|
44
46
|
end
|
45
47
|
end
|
46
48
|
result
|
data/lib/graphos/binary_heap.rb
CHANGED
@@ -26,8 +26,9 @@ module Graphos
|
|
26
26
|
def change key, new_value
|
27
27
|
if has_key? key
|
28
28
|
@values[key] = new_value
|
29
|
+
parent_val = @values[parent(@indexes[key])]
|
29
30
|
move_up key
|
30
|
-
|
31
|
+
heapify key
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -42,6 +43,7 @@ module Graphos
|
|
42
43
|
if size > 0
|
43
44
|
@keys[0] = last
|
44
45
|
@indexes[last] = 0
|
46
|
+
heapify last
|
45
47
|
end
|
46
48
|
|
47
49
|
result
|
@@ -51,6 +53,9 @@ module Graphos
|
|
51
53
|
@keys.size
|
52
54
|
end
|
53
55
|
|
56
|
+
def ordered
|
57
|
+
end
|
58
|
+
|
54
59
|
private
|
55
60
|
KeyVal = Struct.new(:key, :value)
|
56
61
|
|
@@ -66,19 +71,20 @@ module Graphos
|
|
66
71
|
move_up key
|
67
72
|
end
|
68
73
|
|
69
|
-
def
|
74
|
+
def heapify key
|
70
75
|
left_key = @keys[left(@indexes[key])]
|
71
76
|
right_key = @keys[right(@indexes[key])]
|
77
|
+
|
72
78
|
return if !left_key && !right_key
|
73
79
|
|
74
|
-
|
80
|
+
min_key = [key, left_key, right_key].select{|x| !!x}.sort do |x,y|
|
75
81
|
@compare.call(key_val(x), key_val(y))
|
76
82
|
end.first
|
77
83
|
|
78
|
-
return if
|
84
|
+
return if min_key == key
|
79
85
|
|
80
|
-
swap(@indexes[key], @indexes[
|
81
|
-
|
86
|
+
swap(@indexes[key], @indexes[min_key])
|
87
|
+
heapify key
|
82
88
|
end
|
83
89
|
|
84
90
|
def move_up key
|
data/lib/graphos/version.rb
CHANGED
data/test/binary_heap_test.rb
CHANGED
@@ -29,4 +29,57 @@ class BinaryHeapTest < MiniTest::Test
|
|
29
29
|
bh.change(99,0)
|
30
30
|
assert_equal(99,bh.next.key)
|
31
31
|
end
|
32
|
+
|
33
|
+
def test_infinity
|
34
|
+
bh = Graphos::BinaryHeap.new{|x,y| x.value <=> y.value}
|
35
|
+
bh.extend(Simulator)
|
36
|
+
costs = Array.new(30, Float::INFINITY)
|
37
|
+
[0,3,7,16,22,28].each do |i|
|
38
|
+
costs[i] = i
|
39
|
+
end
|
40
|
+
|
41
|
+
costs.each_with_index do |v,i|
|
42
|
+
bh.push(i,v)
|
43
|
+
end
|
44
|
+
|
45
|
+
last = -1
|
46
|
+
while pop = bh.pop
|
47
|
+
assert_operator last, :<=, pop.value
|
48
|
+
last = pop.value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
module Simulator
|
55
|
+
def get_vals
|
56
|
+
@keys.map{|k| @values[k]}
|
57
|
+
end
|
58
|
+
def simulate index=0, &block
|
59
|
+
return if index >= size
|
60
|
+
block.call(@values[@keys[index]])
|
61
|
+
l = left(index)
|
62
|
+
r = right(index)
|
63
|
+
if(r < size)
|
64
|
+
if(smaller(key_val(@keys[l]), key_val(@keys[r])))
|
65
|
+
simulate l, &block
|
66
|
+
simulate r, &block
|
67
|
+
else
|
68
|
+
simulate r, &block
|
69
|
+
simulate l, &block
|
70
|
+
end
|
71
|
+
elsif(l < size)
|
72
|
+
simulate l, &block
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def assert_ordered bh
|
78
|
+
last = -1
|
79
|
+
bh.simulate do |v|
|
80
|
+
puts "#{v}"
|
81
|
+
assert_operator last, :<=, v
|
82
|
+
last = v
|
83
|
+
end
|
84
|
+
end
|
32
85
|
end
|
data/test/prim_test.rb
CHANGED
@@ -22,12 +22,23 @@ class PrimTest < MiniTest::Test
|
|
22
22
|
assert_equal(4, mst.edge(2,3).weight)
|
23
23
|
end
|
24
24
|
|
25
|
+
def test_line
|
26
|
+
graph = Graphos::Weighted::Graph.new 4
|
27
|
+
graph.add_edge 0, 1, 3
|
28
|
+
graph.add_edge 1, 2, 1
|
29
|
+
graph.add_edge 2, 3, 2
|
30
|
+
|
31
|
+
mst = Graphos::Algorithm.prim graph, 0
|
32
|
+
|
33
|
+
refute_equal(nil, mst.edge(0,1))
|
34
|
+
end
|
35
|
+
|
25
36
|
def test_100
|
26
37
|
graph = Graphos::Weighted::TextFactory.read("test/fixtures/grafo_1.txt")
|
27
38
|
|
28
39
|
mst = Graphos::Algorithm.prim graph, 0
|
29
40
|
sum = mst.nodes.map(&:edges).flatten.map(&:weight).reduce(:+)/2
|
30
41
|
|
31
|
-
assert_equal(
|
42
|
+
assert_equal(336, sum)
|
32
43
|
end
|
33
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernardo Amorim
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-11-
|
12
|
+
date: 2014-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|