dijkstra.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Dijkstra's algorithm for vertex-weighted 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. // weights #V list of scalar vertex weights
  24. //
  25. // Output:
  26. // min_distance #V by 1 list of the minimum distances from source to all vertices
  27. // previous #V by 1 list of the previous visited vertices (for each vertex) - used for backtracking
  28. //
  29. template <typename IndexType, typename DerivedD, typename DerivedP>
  30. IGL_INLINE int dijkstra(
  31. const IndexType &source,
  32. const std::set<IndexType> &targets,
  33. const std::vector<std::vector<IndexType> >& VV,
  34. const std::vector<double>& weights,
  35. Eigen::PlainObjectBase<DerivedD> &min_distance,
  36. Eigen::PlainObjectBase<DerivedP> &previous);
  37. // Dijkstra's algorithm for shortest paths, with multiple targets.
  38. // Adapted from http://rosettacode.org/wiki/Dijkstra%27s_algorithm .
  39. //
  40. // Inputs:
  41. // source index of source vertex
  42. // targets target vector set
  43. // VV #V list of lists of incident vertices (adjacency list), e.g.
  44. // as returned by igl::adjacency_list
  45. //
  46. // Output:
  47. // min_distance #V by 1 list of the minimum distances from source to all vertices
  48. // previous #V by 1 list of the previous visited vertices (for each vertex) - used for backtracking
  49. //
  50. template <typename IndexType, typename DerivedD, typename DerivedP>
  51. IGL_INLINE int dijkstra(
  52. const IndexType &source,
  53. const std::set<IndexType> &targets,
  54. const std::vector<std::vector<IndexType> >& VV,
  55. Eigen::PlainObjectBase<DerivedD> &min_distance,
  56. Eigen::PlainObjectBase<DerivedP> &previous);
  57. // Backtracking after Dijkstra's algorithm, to find shortest path.
  58. //
  59. // Inputs:
  60. // vertex vertex to which we want the shortest path (from same source as above)
  61. // previous #V by 1 list of the previous visited vertices (for each vertex) - result of Dijkstra's algorithm
  62. //
  63. // Output:
  64. // path #P by 1 list of vertex indices in the shortest path from source to vertex
  65. //
  66. template <typename IndexType, typename DerivedP>
  67. IGL_INLINE void dijkstra(
  68. const IndexType &vertex,
  69. const Eigen::MatrixBase<DerivedP> &previous,
  70. std::vector<IndexType> &path);
  71. };
  72. #ifndef IGL_STATIC_LIBRARY
  73. #include "dijkstra.cpp"
  74. #endif
  75. #endif /* defined(IGL_DIJKSTRA) */