C++:std::map、查找循环、算法

C++: std::map, find cycles, algorithm

假设数据结构如下:

std::map <int, std::vector<int> > M, 

其中val由图的顶点序列表示,key是序列的第一个顶点。例如

{1} {1, 8, 12, 7}
{4} {4, 3, 5}
{7} {7, 9, 13, 18, 0, 2}
{2} {2, 11, 1}
{5} {5, 17, 10, 4}
{9} {9, 6, 19, 14}
{14} {14, 15, 9} 

如何从段 {}

中找到所有循环(类似的开始和结束顶点)
C1: {1 8 12 7} {7 9 13 18 0 2} {2 11 1}
C2: {4 3 5} {5 17 10 4}
C3: {9 6 19 14} {14, 15, 9} 

以及如何避免段的重复序列,时间复杂度低(地图可能包含数十万个序列)。任何循环都可能包含 n 个段 {},其中 n>=1.

初始化阶段:

std::map <int, std::vector <int> > M;
M[1] = std::vector<int>{ 1, 8, 12, 7 };
M[4] = std::vector<int>{ 4, 3, 5 };
M[7] = std::vector<int>{ 7, 9, 13, 18, 0, 2 };
M[2] = std::vector<int>{ 2, 11, 1 };
M[5] = std::vector<int>{ 5, 17, 10, 4 };
M[9] = std::vector<int>{ 9, 6, 19, 14 };
M[14] = std::vector<int>{ 14, 15, 9 };

算法的草稿

std::vector<std::vector <int> > R;
for (auto im = M.begin(); im != M.end();)
{
    std::vector<int> r, ri = im->second;
    for(;;)
    {
        r.insert(r.end(), ri.begin(), ri.end());
        ri = M[r.back()];
        im = M.erase(M.find(im->first));
        if (r.back() == r.front()) break;
    }
    R.push_back(r);
}

不幸的是,重复删除代表了一个昂贵的操作......我希望有一个更漂亮和高效的解决方案:-)

感谢您的帮助...

我的第一次破解:

for (auto it : M)
{
  if (it.first < it.second.back() && it.second.front() == M[it.second.back()].back())
    std::cout << "Cycle between " << it.first << " and " << it.second.back() << '\n';
}

当然不会找到涉及 3 条以上路径的循环。

首先,您的内部循环需要是一个函数(如果路径不循环怎么办?)

然后,如果有任何一个就宣告失败

  • 结束节点在数值上小于起始节点(可能是一个循环,但不是规范的,所以我们不会打印这个移位版本)
  • 在路径的主 table 中找不到结束节点

这导致了一个解决方案:

bool try_follow(int from, std::vector<int>& result)
{
    int current = from;
    while (true) {
        auto path = M.find(current);
        if (path == M.end()) return false;
        current = path->second.back();
        if (current < from) return false;
        result.insert(result.end(), path->second.begin()+1, path->second.end());
        if (current == from) return true;
    }
}

int main(void)  
{
    for( auto& kvp : M )
    {
        std::vector<int> x;
        if (try_follow(kvp.first, x)) {
            std::cout << kvp.first;
            for( int y : x )
                std::cout << " - " << y;
            std::cout << std::endl;
        }
    }
}

演示:https://rextester.com/DWWZ9457