Class: LightCurrencyConverter::Graph
- Inherits:
-
Object
- Object
- LightCurrencyConverter::Graph
- Defined in:
- lib/light_currency_convert/graph.rb
Instance Attribute Summary collapse
-
#graph ⇒ Object
Returns the value of attribute graph.
Instance Method Summary collapse
- #add_edge(a, b) ⇒ Object
-
#initialize {|_self| ... } ⇒ Graph
constructor
A new instance of Graph.
- #minimize_result(results) ⇒ Object
- #search_path(a, b, result, lr) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Graph
Returns a new instance of Graph.
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
#graph ⇒ Object
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 |