Class: BipartiteGraph::CompleteGraph

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/bipartite_graph/complete_graph.rb

Defined Under Namespace

Classes: FakeNode

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ CompleteGraph

Returns a new instance of CompleteGraph.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bipartite_graph/complete_graph.rb', line 7

def initialize(graph)
  @original_graph = graph
  @complete_graph = Graph.new

  sinks   = graph.sinks.dup
  sink_count = sinks.length
  sources = graph.sources.dup
  source_count = sources.length

  if sink_count > source_count
    (sink_count - source_count).times { sources << FakeNode.new }
  elsif source_count > sink_count
    (source_count - sink_count).times { sinks << FakeNode.new }
  end

  sources.each do |source|
    sinks.each do |sink|
      edge = graph.edge_between(source, sink)
      weight = edge ? edge.weight : 0
      @complete_graph.add_edge(source, sink, weight)
    end
  end

  super(@complete_graph)
end