class RGL::BellmanFordAlgorithm
This class implements {Graph#bellman_ford_shortest_paths}.
def init(source)
def init(source) @visitor.set_source(source) end
def initialize(graph, edge_weights_map, visitor)
Initializes Bellman-Ford algorithm for a _graph_ with provided edges weights map.
def initialize(graph, edge_weights_map, visitor) @graph = graph @edge_weights_map = EdgePropertiesMap.new(edge_weights_map, @graph.directed?) @visitor = visitor end
def relax_edge(u, v)
def relax_edge(u, v) @visitor.handle_examine_edge(u, v) new_v_distance = @visitor.distance_map[u] + @edge_weights_map.edge_property(u, v) if new_v_distance < @visitor.distance_map[v] @visitor.distance_map[v] = new_v_distance @visitor.parents_map[v] = u @visitor.handle_edge_relaxed(u, v) else @visitor.handle_edge_not_relaxed(u, v) end end
def relax_edges
def relax_edges (@graph.size - 1).times do @graph.each_edge do |u, v| relax_edge(u, v) relax_edge(v, u) unless @graph.directed? end end @graph.each_edge do |u, v| if @visitor.distance_map[u] + @edge_weights_map.edge_property(u, v) < @visitor.distance_map[v] @visitor.handle_edge_not_minimized(u, v) else @visitor.handle_edge_minimized(u, v) end end end
def shortest_paths(source)
-
(Hash[Object,Array])
-
def shortest_paths(source) init(source) relax_edges PathBuilder.new(source, @visitor.parents_map).paths(@graph.vertices) end