dijkstra.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Olga Diamanti <olga.diam@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_DIJKSTRA
  9. #define IGL_DIJKSTRA
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. #include <set>
  14. namespace igl {
  15. // Dijstra's algorithm for shortest paths, with multiple targets.
  16. // Adapted from http://rosettacode.org/wiki/Dijkstra%27s_algorithm .
  17. //
  18. // Inputs:
  19. // source index of source vertex
  20. // targets target vector set
  21. // VV #V list of lists of incident vertices (adjacency list), e.g.
  22. // as returned by igl::adjacency_list
  23. //
  24. // Output:
  25. // min_distance #V by 1 list of the minimum distances from source to all vertices
  26. // previous #V by 1 list of the previous visited vertices (for each vertex) - used for backtracking
  27. //
  28. template <typename IndexType, typename DerivedD, typename DerivedP>
  29. IGL_INLINE int dijkstra_compute_paths(const IndexType &source,
  30. const std::set<IndexType> &targets,
  31. const std::vector<std::vector<IndexType> >& VV,
  32. Eigen::PlainObjectBase<DerivedD> &min_distance,
  33. Eigen::PlainObjectBase<DerivedP> &previous);
  34. // Backtracking after Dijstra's algorithm, to find shortest path.
  35. //
  36. // Inputs:
  37. // vertex vertex to which we want the shortest path (from same source as above)
  38. // previous #V by 1 list of the previous visited vertices (for each vertex) - result of Dijkstra's algorithm
  39. //
  40. // Output:
  41. // path #P by 1 list of vertex indices in the shortest path from source to vertex
  42. //
  43. template <typename IndexType, typename DerivedP>
  44. IGL_INLINE void dijkstra_get_shortest_path_to(const IndexType &vertex,
  45. const Eigen::PlainObjectBase<DerivedP> &previous,
  46. std::vector<IndexType> &path);
  47. };
  48. #ifndef IGL_STATIC_LIBRARY
  49. #include "dijkstra.cpp"
  50. #endif
  51. #endif /* defined(IGL_DIJKSTRA) */