Class: BipartiteGraphSets

Inherits:
Object
  • Object
show all
Defined in:
lib/bipartite_graph_sets.rb

Class Method Summary collapse

Class Method Details

.get_perfect_match(requirement_hash) ⇒ Object



5
6
7
8
9
# File 'lib/bipartite_graph_sets.rb', line 5

def self.get_perfect_match requirement_hash
  left_vertices, right_vertices, edges = get_vertices_and_edges requirement_hash
  problem_set_matching = BipartiteGraph.match left_vertices, right_vertices, edges
  matched_solution problem_set_matching
end

.get_vertices_and_edges(requirement_hash) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bipartite_graph_sets.rb', line 11

def self.get_vertices_and_edges requirement_hash
  left_vertices = []
  right_vertices = []
  edges = {}
  requirement_hash.each do |key, value|
     left_vertices << value[:options]
     value[:options].each do |v|
      edges[v] ||= {}
      (1..value[:selection]).each do |n|
        key_name = "#{key}_#{n}"
        right_vertices << key_name
        edges[v][key_name] = 0
      end
    end
  end
  [ left_vertices.flatten.uniq.shuffle, right_vertices.flatten.uniq.shuffle, edges ]
end

.matched_solution(problem_set_matching) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/bipartite_graph_sets.rb', line 29

def self.matched_solution problem_set_matching
  solution_hash = {}
  problem_set_matching.to_h.each do |k,v|
    key = v.to_s.split('_').first
    solution_hash[key] ||= []
    solution_hash[key] << k
  end
  solution_hash
end