Class: Doodl::BellmanFord

Inherits:
Object
  • Object
show all
Includes:
SingleSourceShortestPath
Defined in:
lib/shortest_path/bellman_ford.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SingleSourceShortestPath

#edge_path_to, #node_path_to

Constructor Details

#initialize(graph, source, weight = nil) ⇒ BellmanFord

Returns a new instance of BellmanFord.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shortest_path/bellman_ford.rb', line 12

def initialize(graph, source, weight = nil)
  raise ArgumentError unless graph.is_a?(Graph)
  raise ArgumentError unless graph.contains_node?(source)
  @dist, @prev = {}, {}
  @graph, @source, = graph, source
  if weight
    @weight = weight
  else
    @weight = Hash.new(1)
  end
  init_maps
  main_algorithm
  check_for_negative_cyles
end

Instance Attribute Details

#distObject (readonly)

Returns the value of attribute dist.



11
12
13
# File 'lib/shortest_path/bellman_ford.rb', line 11

def dist
  @dist
end

#prevObject (readonly)

Returns the value of attribute prev.



11
12
13
# File 'lib/shortest_path/bellman_ford.rb', line 11

def prev
  @prev
end