Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shortest_path_between is slow, generating a lot of database queries #27

Open
mathieujobin opened this issue May 13, 2019 · 0 comments
Open

Comments

@mathieujobin
Copy link

anyone else ever attempted to make shortest_path_between fast?

https://github.com/payrollhero/acts-as-dag/blob/master/lib/dag/edges.rb#L88-L108

    #Finds the shortest path between ancestor and descendant returning as an array
    def shortest_path_between(ancestor, descendant, path=[])
      shortest = []
      ancestor.children.each do |child|
        if child == descendant
          temp = path.clone
          temp << child
          if shortest.blank? || temp.length < shortest.length
            shortest = temp
          end
        elsif self.find_link(child, descendant)
          temp = path.clone
          temp << child
          temp = self.shortest_path_between(child, descendant, temp)
          if shortest.blank? || temp.length < shortest.length
            shortest = temp
          end
        end
      end
      return shortest
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant