簡體   English   中英

將圖表示為 unordered_map 時的拓撲排序錯誤<string, vector<string> &gt;

[英]Error in topological sort when representing the graph as an unordered_map<string, vector<string>>

當我嘗試使用代表圖形的unordered_map<string, vector<string>>在 C++ 中實現拓撲排序時,我遇到了無法解釋的錯誤(來自我的部分)。 具體來說,只有當被訪問的“當前節點”不作為unordered_map的鍵存在時才會發生這種情況(即,它沒有傳出邊)。 它沒有返回“正確”的順序,而是完全終止函數調用topSort並僅返回順序的一小部分。

代碼返回: ML, AML, DL

相反,可能的正確解決方案可能是: LA, MT, MA, PT, ML, AML, DL

誰能解釋為什么會發生這種情況?

以下是出現問題的一小段代碼片段:

// 0 -> white (node has not been visited)
// 1 -> grey (node is currently being visited)
// 2 -> black (node is completely explored)
bool topSortVisit(unordered_map<string, vector<string>>& graph,
        unordered_map<string, int>& visited, string node, vector<string>& result){

    if(visited[node] == 1) return false;
    if(visited[node] == 2) return true;

    // Mark current node as being visited.
    visited[node] = 1;
    // node might not have outgoing edges and therefore not in the
    // unordered_map (graph) as a key.
    for(auto neighbor : graph[node]){
        if(!topSortVisit(graph, visited, neighbor, result)) return false;
    }

    result.push_back(node);
    visited[node] = 2;
    return true;
}

vector<string> topSort(unordered_map<string, vector<string>>& graph){

    unordered_map<string, int> visited;
    vector<string> result;

    // Should visit all nodes with outgoing edges in the graph.
    for(auto elem : graph){
        string node = elem.first;
        bool acyclic = topSortVisit(graph, visited, node, result);
        if(!acyclic){
            cout << "cycle detected\n";
            return vector<string>{};
        }

    }

    reverse(result.begin(), result.end());
    return result;
}

這是重現所有內容的代碼:

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>

using namespace std;

bool topSortVisit(unordered_map<string, vector<string>>& graph,
        unordered_map<string, int>& visited, string node, vector<string>& result){

    if(visited[node] == 1) return false;
    if(visited[node] == 2) return true;

    visited[node] = 1;
    for(auto neighbor : graph[node]){
        if(!topSortVisit(graph, visited, neighbor, result)) return false;
    }

    result.push_back(node);
    visited[node] = 2;
    return true;
}

vector<string> topSort(unordered_map<string, vector<string>>& graph){

    unordered_map<string, int> visited;
    vector<string> result;

    for(auto elem : graph){
        string node = elem.first;
        bool acyclic = topSortVisit(graph, visited, node, result);
        if(!acyclic){
            cout << "cycle detected\n";
            return vector<string>{};
        }

    }

    return result;
}


unordered_map<string, vector<string>> makeGraph(vector<pair<string, string>> courses){
    unordered_map<string, vector<string>> graph;

    for(auto p : courses){
        graph[p.first].push_back(p.second);
    }
    return graph;
}

int main(){

    vector<pair<string, string>> pairs;
    pairs.push_back(make_pair("LA", "ML"));
    pairs.push_back(make_pair("MT", "ML"));
    pairs.push_back(make_pair("MA", "PT"));
    pairs.push_back(make_pair("PT", "ML"));
    pairs.push_back(make_pair("ML", "DL"));
    pairs.push_back(make_pair("ML", "AML"));

    auto graph = makeGraph(pairs);
    vector<string> result = topSort(graph); // ML, AML, DL
    // A possible correct solution could be: LA, MT, MA, PT, ML, AML, DL


    for(string s : result){
        cout << s << " ";
    }
    cout << "\n";
}

插入unordered_map會使映射中的迭代器無效( 如果它重新散列) 這會用auto elem : graph打破你的循環(順便說一下,它復制了你的vector<string>對象;改用auto &elem )。 將您的圖形作為const&傳遞以避免此類惡作劇; 然后編譯器會溫和地建議您使用at而不是operator[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM