class RGL::DijkstraAlgorithm

Constants

DEFAULT_DISTANCE_COMBINATOR

Distance combinator is a lambda that accepts the distance (usually from the source) to vertex u and the weight of the edge connecting vertex u to another vertex v and returns the distance to vertex v if it's reached through the vertex u. By default, the distance to vertex u and the edge's weight are summed.

Public Class Methods

new(graph, edge_weights_map, visitor, distance_combinator = nil) click to toggle source

Initializes Dijkstra's algorithm for a graph with provided edges weights map.

   # File lib/rgl/dijkstra.rb
18 def initialize(graph, edge_weights_map, visitor, distance_combinator = nil)
19   @graph               = graph
20   @edge_weights_map    = build_edge_weights_map(edge_weights_map)
21   @visitor             = visitor
22   @distance_combinator = distance_combinator || DEFAULT_DISTANCE_COMBINATOR
23 end

Public Instance Methods

find_shortest_paths(source) click to toggle source

Finds the shortest path from the source to every other vertex.

   # File lib/rgl/dijkstra.rb
47 def find_shortest_paths(source)
48   init(source)
49   relax_edges
50 end
shortest_path(source, target) click to toggle source

Finds the shortest path from the source to the target in the graph.

Returns the shortest path, if it exists, as an Array of vertices. Otherwise, returns nil.

   # File lib/rgl/dijkstra.rb
29 def shortest_path(source, target)
30   init(source)
31   relax_edges(target, true)
32   PathBuilder.new(source, @visitor.parents_map).path(target)
33 end
shortest_paths(source) click to toggle source

Finds the shortest path form the source to every other vertex of the graph and builds shortest paths map.

Returns the shortest paths map that contains the shortest path (if it exists) from the source to any vertex of the graph.

   # File lib/rgl/dijkstra.rb
40 def shortest_paths(source)
41   find_shortest_paths(source)
42   PathBuilder.new(source, @visitor.parents_map).paths(@graph.vertices)
43 end

Private Instance Methods

build_edge_weights_map(edge_weights_map) click to toggle source
    # File lib/rgl/dijkstra.rb
100 def build_edge_weights_map(edge_weights_map)
101   edge_weights_map.is_a?(EdgePropertiesMap) ? edge_weights_map : NonNegativeEdgePropertiesMap.new(edge_weights_map, @graph.directed?)
102 end
init(source) click to toggle source
   # File lib/rgl/dijkstra.rb
54 def init(source)
55   @visitor.set_source(source)
56 
57   @queue = MinPriorityQueue.new
58   @queue.push(source, 0)
59 end
relax_edge(u, v) click to toggle source
   # File lib/rgl/dijkstra.rb
78 def relax_edge(u, v)
79   @visitor.handle_examine_edge(u, v)
80 
81   new_v_distance = @distance_combinator.call(@visitor.distance_map[u], @edge_weights_map.edge_property(u, v))
82 
83   if new_v_distance < @visitor.distance_map[v]
84     @visitor.distance_map[v] = new_v_distance
85     @visitor.parents_map[v]  = u
86 
87     if @visitor.color_map[v] == :WHITE
88       @visitor.color_map[v] = :GRAY
89       @queue.push(v, new_v_distance)
90     elsif @visitor.color_map[v] == :GRAY
91       @queue.decrease_key(v, new_v_distance)
92     end
93 
94     @visitor.handle_edge_relaxed(u, v)
95   else
96     @visitor.handle_edge_not_relaxed(u, v)
97   end
98 end
relax_edges(target = nil, break_on_target = false) click to toggle source
   # File lib/rgl/dijkstra.rb
61 def relax_edges(target = nil, break_on_target = false)
62   until @queue.empty?
63     u = @queue.pop
64 
65     break if break_on_target && u == target
66 
67     @visitor.handle_examine_vertex(u)
68 
69     @graph.each_adjacent(u) do |v|
70       relax_edge(u, v) unless @visitor.finished_vertex?(v)
71     end
72 
73     @visitor.color_map[u] = :BLACK
74     @visitor.handle_finish_vertex(u)
75   end
76 end