dfs.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "dfs.h"
  2. #include "list_to_matrix.h"
  3. #include <vector>
  4. template <
  5. typename AType,
  6. typename DerivedD,
  7. typename DerivedP,
  8. typename DerivedC>
  9. IGL_INLINE void igl::dfs(
  10. const std::vector<std::vector<AType> > & A,
  11. const size_t s,
  12. Eigen::PlainObjectBase<DerivedD> & D,
  13. Eigen::PlainObjectBase<DerivedP> & P,
  14. Eigen::PlainObjectBase<DerivedC> & C)
  15. {
  16. std::vector<typename DerivedD::Scalar> vD;
  17. std::vector<typename DerivedP::Scalar> vP;
  18. std::vector<typename DerivedC::Scalar> vC;
  19. dfs(A,s,vD,vP,vC);
  20. list_to_matrix(vD,D);
  21. list_to_matrix(vP,P);
  22. list_to_matrix(vC,C);
  23. }
  24. template <
  25. typename AType,
  26. typename DType,
  27. typename PType,
  28. typename CType>
  29. IGL_INLINE void igl::dfs(
  30. const std::vector<std::vector<AType> > & A,
  31. const size_t s,
  32. std::vector<DType> & D,
  33. std::vector<PType> & P,
  34. std::vector<CType> & C)
  35. {
  36. // number of nodes
  37. int N = s+1;
  38. for(const auto & Ai : A) for(const auto & a : Ai) N = std::max(N,a+1);
  39. std::vector<bool> seen(N,false);
  40. P.resize(N,-1);
  41. std::function<void(const size_t, const size_t)> dfs_helper;
  42. dfs_helper = [&D,&P,&C,&dfs_helper,&seen,&A](const size_t s, const size_t p)
  43. {
  44. if(seen[s]) return;
  45. seen[s] = true;
  46. D.push_back(s);
  47. P[s] = p;
  48. for(const auto n : A[s]) dfs_helper(n,s);
  49. C.push_back(s);
  50. };
  51. dfs_helper(s,-1);
  52. }
  53. #ifdef IGL_STATIC_LIBRARY
  54. // Explicit template instantiation
  55. // generated by autoexplicit.sh
  56. template void igl::dfs<int, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, const size_t, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  57. #endif