dijkstra.h 3.5 KB

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