Class: LightCurrencyConverter::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Graph

Returns a new instance of Graph.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
# File 'lib/light_currency_convert/graph.rb', line 5

def initialize(&block)
  @graph = Hash.new { |hash, key| hash[key] = [] }
  yield self if block_given?
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



3
4
5
# File 'lib/light_currency_convert/graph.rb', line 3

def graph
  @graph
end

Instance Method Details

#add_edge(a, b) ⇒ Object



10
11
12
13
# File 'lib/light_currency_convert/graph.rb', line 10

def add_edge(a, b)
  @graph[a] << b
  @graph[b] << a
end

#minimize_result(results) ⇒ Object



15
16
17
18
19
# File 'lib/light_currency_convert/graph.rb', line 15

def minimize_result(results)
  results.reduce(Array.new(1000)) do |end_array, element|
    (element.size < end_array.size) ? element : end_array
  end
end

#search_path(a, b, result, lr) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/light_currency_convert/graph.rb', line 21

def search_path(a, b, result, lr)
  result += [a]
  lr << result if a == b
  @graph[a].each do |v|
    search_path(v, b, result, lr) unless result.include?(v)
  end
end