graphos 0.2.1 → 0.3.0

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: 9c7f99e440b3123de01dfb8bce9a8192e6a20ec4
4
- data.tar.gz: 7c495f8ef31bf0d277675a1c80590acbf4c654ef
3
+ metadata.gz: 14d4c0ca86eff6a4e8787e26744bbe20fcf26646
4
+ data.tar.gz: 96d69c71586846335829e530ee9ad82c6f64a2f8
5
5
  SHA512:
6
- metadata.gz: 9471f4cecb0bb302cd753e64c289c2a981551aab5705050499f612d5efff678c29d2a7d4504baac0b809af72eda2bed5fbf27b176c24798d3adf02504f229116
7
- data.tar.gz: 03a3fee629b2e299125e6cb0b14a0d69661c57d3013e7e3726696b8e179e6e35ae5a73a19a13410fa0d7ff53e6227ba65abcd6d76235ddfad794325a9376a588
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 = Heap.new{|x,y| (costs[x] <=> costs[y]) == -1}
17
- (0..graph.size-1).each{|i| heap.push(i)}
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(heap.has_key?(idx))
22
- heap.delete(idx)
23
- heap.push(idx)
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 idx=heap.pop
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
@@ -1,3 +1,3 @@
1
1
  module Graphos
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -7,6 +7,7 @@ module Graphos
7
7
  require "graphos/weighted/node"
8
8
  require "graphos/weighted/edge"
9
9
 
10
+ attr_reader :nodes
10
11
 
11
12
  def initialize size
12
13
  @nodes = (0..size-1).map{|i| Node.new i }
@@ -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
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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernardo Amorim