monora / rgl

RGL is a framework for graph data structures and algorithms in Ruby.
https://monora.github.io/rgl/
Other
415 stars 59 forks source link

`depth_first_search` Doesn't work #63

Closed oskargargas closed 2 years ago

oskargargas commented 2 years ago

Hi!

I tried to use:

visitor = @rgl_graph.dfs_iterator(@root_vertex_id)
visitor.attach_distance_map(@rgl_edge_weights)
visitor.set_examine_vertex_event_handler { |v|
    puts ">>> Visitor: #{v}"
}
@rgl_graph.depth_first_search(visitor) {
    puts ">>> DFS handler: #{v}"
}

But if fails with an exception saying

undefined method `handle_start_vertex' for #<RGL::DFSIterator:0x000000010f5f08d8 @graph=#<RGL::DirectedAdjacencyGraph:0x000000010f5f1a08 @edgelist_class=Set, @vertices_dict={"1039ea5f-2b61-450a-8f38-16d9443295ca"=>#<Set: {}>, "9fbc9bdb-0270-4f64-8e7a-864d2ce7f17c"=>#<Set: {"1039ea5f-2b61-450a-8f38-16d9443295ca"}>}>, @color_map={"1039ea5f-2b61-450a-8f38-16d9443295ca"=>:GRAY}, @start_vertex="1039ea5f-2b61-450a-8f38-16d9443295ca", @waiting=["1039ea5f-2b61-450a-8f38-16d9443295ca"], @distance_map=[{["9fbc9bdb-0270-4f64-8e7a-864d2ce7f17c", "1039ea5f-2b61-450a-8f38-16d9443295ca"]=>8.73408059691218}], @examine_vertex_event_handler=#<Proc:0x000000010f5f0540

Which leads to: https://github.com/monora/rgl/blob/731c562e45732efe434f58c4f1028fd16bfd863e/lib/rgl/traversal.rb#L182

When I use content of depth_first_search method directly (minus handle_start_vertex part) it looks like it works

    @rgl_graph.each_vertex do |u|
      unless visitor.finished_vertex?(u)
        @rgl_graph.depth_first_visit(u, visitor) { |v|
          puts ">>> DFS handler: #{v}"
        }
      end
    end
oskargargas commented 2 years ago

Nvm... I was using Iterator instead of Visitor. Apparently being used to static typed languages