dijkstra.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Olga Diamanti <[email protected]>
  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. /// Dijkstra's algorithm for vertex-weighted shortest paths, with multiple targets.
  16. /// Adapted from http://rosettacode.org/wiki/Dijkstra%27s_algorithm .
  17. ///
  18. /// @param[in] source index of source vertex
  19. /// @param[in] targets target vector set
  20. /// @param[in] VV #V list of lists of incident vertices (adjacency list), e.g.
  21. /// as returned by igl::adjacency_list
  22. /// @param[in] weights #V list of scalar vertex weights
  23. /// @param[out] min_distance #V by 1 list of the minimum distances from source to all vertices
  24. /// @param[out] previous #V by 1 list of the previous visited vertices (for each vertex) - used for backtracking
  25. ///
  26. template <typename IndexType, typename DerivedD, typename DerivedP>
  27. IGL_INLINE int dijkstra(
  28. const IndexType &source,
  29. const std::set<IndexType> &targets,
  30. const std::vector<std::vector<IndexType> >& VV,
  31. const std::vector<double>& weights,
  32. Eigen::PlainObjectBase<DerivedD> &min_distance,
  33. Eigen::PlainObjectBase<DerivedP> &previous);
  34. /// \overload
  35. template <typename IndexType, typename DerivedV,
  36. typename DerivedD, typename DerivedP>
  37. IGL_INLINE int dijkstra(
  38. const Eigen::MatrixBase<DerivedV> &V,
  39. const std::vector<std::vector<IndexType> >& VV,
  40. const IndexType &source,
  41. const std::set<IndexType> &targets,
  42. Eigen::PlainObjectBase<DerivedD> &min_distance,
  43. Eigen::PlainObjectBase<DerivedP> &previous);
  44. /// \overload
  45. template <typename IndexType, typename DerivedD, typename DerivedP>
  46. IGL_INLINE int dijkstra(
  47. const IndexType &source,
  48. const std::set<IndexType> &targets,
  49. const std::vector<std::vector<IndexType> >& VV,
  50. Eigen::PlainObjectBase<DerivedD> &min_distance,
  51. Eigen::PlainObjectBase<DerivedP> &previous);
  52. /// Backtracking after Dijkstra's algorithm, to find shortest path.
  53. ///
  54. /// @param[in] vertex vertex to which we want the shortest path (from same source as above)
  55. /// @param[in] previous #V by 1 list of the previous visited vertices (for each vertex) - result of Dijkstra's algorithm
  56. /// @param[out] path #P by 1 list of vertex indices in the shortest path from vertex to source
  57. template <typename IndexType, typename DerivedP>
  58. IGL_INLINE void dijkstra(
  59. const IndexType &vertex,
  60. const Eigen::MatrixBase<DerivedP> &previous,
  61. std::vector<IndexType> &path);
  62. }
  63. #ifndef IGL_STATIC_LIBRARY
  64. #include "dijkstra.cpp"
  65. #endif
  66. #endif /* defined(IGL_DIJKSTRA) */