找到顶点的 3 环邻域的最快方法?

问题描述

我有以下问题:我得到了一个顶点索引 int v一个顶点到顶点的连通性 std::vector<std::vector<int>> v2v(对于每个顶点都是 1 环邻居),我正在寻找最快的方法找到 3 环邻居(所有顶点索引,包括距离 3 内的 v,还有 2 和 1)。

这就是我想出来的,但也许有更好的方法,使用其他容器(我知之甚少),或者更快的方法?我知道随着环序的增长,会需要更多的时间,因为有很多回溯。非常感谢您提供见解。

#include <vector>
#include <algorithm>

// recursively add the current vertex to the patch and repeat

inline void compute_vertex_patch_impl(int v,const std::vector<std::vector<int>>& v2v,int order,std::vector<int>& patch) {
  // add v to the patch
  patch.push_back(v);

  // for each neighbor vv if the current neighbor order is not zero add it and repeat
  for (auto vv : v2v[v]) 
  {
    if (order > 0) compute_vertex_patch_impl(vv,v2v,order - 1,patch);
  }
}

inline std::vector<int> compute_vertex_patch(int v,int order) {
  std::vector<int> patch;

  compute_vertex_patch_impl(v,order,patch);

  std::sort(patch.begin(),patch.end());
  auto end = std::unique(patch.begin(),patch.end());
  return {patch.begin(),end};
}; 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)