繁体   English   中英

C ++:std :: map,查找周期,算法

[英]C++: std::map, find cycles, algorithm

假设以下数据结构:

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

其中val由图形的顶点序列表示,键为序列的第一个顶点。 例如

{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);
}

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

谢谢你的帮助...

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

然后,声明失败

  • 结束节点在数值上小于开始节点(可以是一个周期,但不是规范的,因此我们不会打印此偏移版本)
  • 在路径的主表中找不到末端节点

这导致了一个解决方案:

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

我的第一个裂缝:

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条以上路径的循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM