简体   繁体   中英

Finding cycles in a directed graph implemented using an unordered multimap

So, I've implemented a directed graph using an unordered multimap. Each pair within the map is made up of two strings: the vertex and its adjacent vertex. Now, I am trying to determine if my graph has a cycle, and if so, how big is the cycle. This is the code I have so far:

int findCycle(const unordered_multimap<string,string> & connectedURLVertices, string y, string key)
{
        string position;

        position=y.find(key);

        if(position!=string::npos)
        {
            return 1;
        }

        auto nodesToCheck=connectedURLVertices.equal_range(key);

        for(auto & node : nodesToCheck)
        {
            int z=findCycle(connectedURLVertices,y+key,node);
        }
}

I've walked through the code on paper and it seems to be logically correct, but I would appreciate it if anyone could take a look and see if I am on the right track or missing anything. Thanks!

要在图形中搜索周期,您必须从某个初始节点通过弧线递归下降,直到到达一个已经访问过的节点(您可以构造一个std::set已访问过的节点或在访问它们时标记这些节点)或用尽所有尚未访问过的节点(不存在周期),可以调整选择弧的标准以更快地找到它,或者可以进行搜索的类型(深度优先,按级别搜索等)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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