graphos 0.2.1 → 0.3.0
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.
- checksums.yaml +4 -4
- data/lib/graphos/algorithm/prim.rb +8 -8
- data/lib/graphos/version.rb +1 -1
- data/lib/graphos/weighted/graph.rb +1 -0
- data/test/prim_test.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14d4c0ca86eff6a4e8787e26744bbe20fcf26646
|
4
|
+
data.tar.gz: 96d69c71586846335829e530ee9ad82c6f64a2f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1dcc65d651e204dd016dc2d35bed53595fdfd0a4ccead3b3f8456fd0638152fa32483a4ef6419449bd21bc8acff6d3077c0c8383cb84e57db973392e7b743cd
|
7
|
+
data.tar.gz: 50120e8b5d17ff5c0982a586f44f89ca63c36b9d8ddcad50d005e0ee5b5c484cc18f9abc8eef4f09f56d16d3e978cb19a387b03d3253689013d06d1714faf22f
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require "algorithms"
|
2
2
|
module Graphos
|
3
3
|
module Algorithm
|
4
|
-
include Containers
|
5
|
-
|
6
4
|
##
|
7
5
|
# Runs the prim algorithm in order to
|
8
6
|
# find a MST for a given graph.
|
@@ -13,18 +11,20 @@ module Graphos
|
|
13
11
|
costs = Array.new(graph.size, Float::INFINITY)
|
14
12
|
costs[initial] = 0
|
15
13
|
|
16
|
-
heap =
|
17
|
-
|
14
|
+
heap = BinaryHeap.new{|x,y| x.value <=> y.value}
|
15
|
+
heap.push(initial, 0)
|
18
16
|
|
19
17
|
update_cost = -> (idx,cost) do
|
20
18
|
costs[idx] = cost
|
21
|
-
if
|
22
|
-
heap.
|
23
|
-
|
19
|
+
if heap.has_key? idx
|
20
|
+
heap.change idx, cost
|
21
|
+
else
|
22
|
+
heap.push idx, cost
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
|
-
while
|
26
|
+
while keyval=heap.pop
|
27
|
+
idx = keyval.key
|
28
28
|
node = graph[idx]
|
29
29
|
node.edges.each do |edge|
|
30
30
|
if costs[edge.to.index] > edge.weight
|
data/lib/graphos/version.rb
CHANGED
data/test/prim_test.rb
CHANGED
@@ -21,4 +21,13 @@ class PrimTest < MiniTest::Test
|
|
21
21
|
assert_equal(1, mst.edge(0,2).weight)
|
22
22
|
assert_equal(4, mst.edge(2,3).weight)
|
23
23
|
end
|
24
|
+
|
25
|
+
def test_100
|
26
|
+
graph = Graphos::Weighted::TextFactory.read("test/fixtures/grafo_1.txt")
|
27
|
+
|
28
|
+
mst = Graphos::Algorithm.prim graph, 0
|
29
|
+
sum = mst.nodes.map(&:edges).flatten.map(&:weight).reduce(:+)/2
|
30
|
+
|
31
|
+
assert_equal(253, sum)
|
32
|
+
end
|
24
33
|
end
|