簡體   English   中英

我如何知道拓撲排序是否有效?

[英]How do I know if a topological sort is valid?

我有這個 1...n 的圖,現在我找到了一個拓撲排序解決方案,我怎么知道它是否是一個有效的解決方案? 例如,對於 n = 5; 而這種連通性 1->2, 2->3, 1->3, 1->5,

我有一個解決方案 4->1->5->2->3 。它有效嗎? 可能是我對 topsort 的想法不清楚。

為方便起見,這是我的代碼

int incoming[n],A[n][n];
priority_queue<int> Q;
while(m--){
    int i,j;
    cin>>i>>j;
    A[i][j] = 1;
    ++incoming[j];
}
for(int i=1;i<=n;++i)
    if(!incoming[i])
        Q.push(i);
vector<int> toplist;
while(!Q.empty()){
    int u = Q.top(); Q.pop();
    toplist.push_back(u);
    for(int i = 1;i<=n;++i)
    {
        if(A[u][i])
        {
            A[u][i] = 0;
            --incoming[i];
            if(!incoming[i])
                Q.push(i);
        }

    }
}
for(int i=0;i<toplist.size();++i)
    cout<<toplist[i]<<" ";

我在進行拓撲排序時遇到了完全相同的問題。 有一個名為 bValidateTopSortResult() 的函數可以驗證結果。 如果從 U 到 V 存在一條邊,則 U 在頂級排序中必須在 V 之前。 重要的是跟蹤所有相鄰的頂點。 我已經存儲在一個列表中。

#include <iostream>
#include <list>
#include <stack>
#include <map>

using namespace std;

struct GRAPH{
    int iVertices;

list<int> *ptrAdj;  //Has structure like ptrAdj[i]={j,k,l}.  This means that
                    //i is from-vertex and j,k,l are to-vertices.
}; 

int iTopSortOrder[10];
map<int, int> mapTopSort;

//
//Recursive function.
//Key is iterating  list<>*ptrAdj.
//
void
vTopSortRecursive( GRAPH* pG, int iVertexID, bool *bVisited, stack<int>& stResult )
{
   bVisited[iVertexID] = true;

   //Goes thru all adjacent vertices. 
   for( auto it : pG->ptrAdj[iVertexID]){
       if(bVisited[it] == false) 
           vTopSortRecursive( pG, it, bVisited, stResult );
   }

    //Pushes the vertex ID after all its children adjacent vertices are already 
   pushed.
   stResult.push(iVertexID);
}

void
vTopSort( GRAPH* ptrGraph )
{
    stack<int> stResult;
    bool bVisited[ptrGraph->iVertices];
    int iTopSortIndex = 0;

    for(int i=0; i < ptrGraph->iVertices; ++i )
        bVisited[i] = false;


    for(int i=0; i < ptrGraph->iVertices; ++i )
        if(bVisited[i] == false )
            vTopSortRecursive( ptrGraph, i, bVisited, stResult );

    cout << "Top sort result" << endl;
    while( !stResult.empty() ){
        int iTop = stResult.top();
        cout << iTop << endl;
        //Keeps track of the order of the vertex top sort.
        //mapTopSort[i] = j means vertex ID 'i' is in jth order in array.
        mapTopSort[iTop] = iTopSortIndex;
        //Keeps the top sort order in an array.
        iTopSortOrder[iTopSortIndex] = stResult.top();;
        stResult.pop();
        iTopSortIndex++;
    }
}
//All parent ID must come befoe children ID in top sort.
//mapTopSort[i] = j;  means that vertex ID 'i' is in jth place in the order.
bool
bValidateTopSortResult( GRAPH* ptrGraph )
{
    bool bValid=true;

    for(int iVertexID=0; iVertexID<ptrGraph->iVertices;iVertexID++)
    {
        for(auto it: ptrGraph->ptrAdj[iVertexID] ){
            if( mapTopSort[iVertexID] > mapTopSort[it] ){

                bValid = false;
                cout << "Invalid sort" << endl;
                cout << iVertexID << " must come before " << mapTopSort[it] << endl;
            }
        }
    }
    return bValid;
}

int
main(void)
{
    GRAPH *ptrGraph = new GRAPH;
    ptrGraph->iVertices = 6;

    ptrGraph->ptrAdj = new list<int>[6];
    ptrGraph->ptrAdj[5].push_back(2); 
    ptrGraph->ptrAdj[5].push_back(0); 
    ptrGraph->ptrAdj[4].push_back(0); 
    ptrGraph->ptrAdj[4].push_back(1); 
    ptrGraph->ptrAdj[2].push_back(3); 
    ptrGraph->ptrAdj[3].push_back(1); 

    vTopSort( ptrGraph );

    if (bValidateTopSortResult( ptrGraph ))
        cout << "Sort is valid";
    else
        cout <<"Sort is INVALID";

    return 0;
}

此功能將幫助您檢查天氣拓撲排序是否有效

int check(int V, vector <int> &res, vector<int> adj[]) {
    
    if(V!=res.size())
    return 0;
    
    vector<int> map(V, -1);
    for (int i = 0; i < V; i++) {
        map[res[i]] = i;
    }
    for (int i = 0; i < V; i++) {
        for (int v : adj[i]) {
            if (map[i] > map[v]) return 0;
        }
    }
    return 1;
}

暫無
暫無

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

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