bipartite_graph 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a66f77f8c4a20067ac92fc48800665accb90811
4
- data.tar.gz: b4bab5f71d800b0da8fc8bff0c347b39e70eb1e4
3
+ metadata.gz: f94717ca0e980c145bc9eff819518bfc01054c2f
4
+ data.tar.gz: c88a438ba62df2581510b302298b004e69251d9b
5
5
  SHA512:
6
- metadata.gz: 8f448ee0be1644f62e2365cfcdfc433f45309a8393cef8986491dc9129f4c795ca2757bd0aa059c9c652fa6ea08cc2d792287a515e3784d96a824d6b696c42a2
7
- data.tar.gz: e32b1adc8829db74a08d1ac12c4fb54eaf56d4c7d3830991fc8031abe4712456e23380accb4304069ece23a269a1bac52363aad6ab5ec7e6d97e766fb7f92db1
6
+ metadata.gz: 0f886ec5b380f849f11b2c9da2e24e052124a7e69e30794700baf3eb032c853e1d5d97bd3a943a2ce77fc2a066eede3e051e93080798f78398bb312370fd9c00
7
+ data.tar.gz: e3513a47e120dfdfcbf403478d7d8815fe77c2c4d82c3cdc939b43fecda1b4d8a6b6ecf2fcaa9100665a4657c9a94ab7fb230c3ef87cf53b295d6c7c1a22df5b
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
@@ -53,6 +53,10 @@ module BipartiteGraph
53
53
  @labels[node]
54
54
  end
55
55
 
56
+ def total
57
+ graph.nodes.inject(0) {|sum, node| sum + label_for(node) }
58
+ end
59
+
56
60
  def recalculate_equality_graph
57
61
  equality_graph.clear
58
62
 
@@ -94,6 +98,10 @@ module BipartiteGraph
94
98
  2 * edges.length == graph.nodes.length
95
99
  end
96
100
 
101
+ def weight
102
+ edges.inject(0) {|sum, e| sum + e.weight }
103
+ end
104
+
97
105
  def add_edge(edge)
98
106
  edges << edge
99
107
  end
@@ -110,7 +118,7 @@ module BipartiteGraph
110
118
  end
111
119
 
112
120
  def solution
113
- while !matching.perfect?
121
+ while matching.weight != labelling.total
114
122
  root = (graph.sources - matching.sources).first
115
123
  add_to_matching(root)
116
124
  end
@@ -1,3 +1,3 @@
1
1
  module BipartiteGraph
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,10 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "weighted matchings" do
4
+ let(:graph) { BipartiteGraph.new }
4
5
  describe "simple example 1" do
5
6
  #http://www.cse.ust.hk/~golin/COMP572/Notes/Matching.pdf
6
- let(:graph) { BipartiteGraph.new }
7
-
8
7
  before do
9
8
  [
10
9
  ['x1', 'y1', 1],
@@ -26,7 +25,6 @@ describe "weighted matchings" do
26
25
  end
27
26
 
28
27
  describe "simple example 2" do
29
- let(:graph) { BipartiteGraph.new }
30
28
 
31
29
  before do
32
30
  [
@@ -40,7 +38,7 @@ describe "weighted matchings" do
40
38
  ['x3', 'y3', 8],
41
39
  ['x3', 'y4', 7],
42
40
  ['x4', 'y3', 6],
43
- ['x4', 'y4', 4],
41
+ ['x4', 'y4', 4]
44
42
  ].each { |from, to, weight| graph.add_edge(from, to, weight) }
45
43
  end
46
44
 
@@ -53,4 +51,21 @@ describe "weighted matchings" do
53
51
  end
54
52
  end
55
53
 
54
+ describe "unbalanced example" do
55
+ before do
56
+ [
57
+ ['x1', 'y1', 3],
58
+ ['x1', 'y2', 1],
59
+ ['x1', 'y3', 11]
60
+ ].each { |from, to, weight| graph.add_edge(from, to, weight) }
61
+ end
62
+
63
+ it "finds the solution" do
64
+ matching = graph.max_weight_matching.edges.map {|e| [e.from, e.to] }
65
+
66
+ expect(matching).to match_array([
67
+ ['x1', 'y3']
68
+ ])
69
+ end
70
+ end
56
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bipartite_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Close
@@ -55,12 +55,7 @@ dependencies:
55
55
  description:
56
56
  email:
57
57
  - tom.close@cantab.net
58
- executables:
59
- - bundler
60
- - htmldiff
61
- - ldiff
62
- - rake
63
- - rspec
58
+ executables: []
64
59
  extensions: []
65
60
  extra_rdoc_files: []
66
61
  files: