简体   繁体   中英

Optimization on Algorithm to check “Whether for a given directed graph there is only one way to sort the graph using topological sort or not”

My algorithm for this is :

  1. First to calculate Indegree of every node , ie count of how many edges are there for which this node is sink for them.
  2. Now, will push only those in queue which have indegree==0 because these will be the first to appear in topological sorted list of graph.
  3. If now starting size of Queue is zero. that means "Graph can't be sorted" .
  4. else we start Sorting method.
  5. If we encounter that more than 2 vertices are present in queue at any time that means that "Multiple sequences are possible"
  6. But there may be a case where further Sequence might not be possible.
  7. So I keep track of the node that popped(deleted from Front) from Queue. and Count of them too.
  8. If lastly count==number of nodes.and flag for multiple sequence is unset then sequence is possible "Graph can be sorted".
  9. or if count==number of nodes and flag for Mutiple sequence was set . we say "Multiple Sequence is Possible"
  10. if count!=number of nodes. then "Graph can't be sorted"

Here is My implementation of my Idea

vector<vector<int> >list(10000); // Graph is represented as Adjacency list
void topological_sort()
{
    int i,l,item,j;
    k=0;    
    queue<int>q; // Queue
    vector<int>:: iterator it; 
    for(i=1;i<=n;i++) // Pushing nodes those who have indegree=0
    {       
        if(indegree[i]==0) 
            q.push(i);  
    }
    l=q.size();
    if(l==0)
    {
        flag=2; // means no sequence is possible
        return; 
    }
    while(q.empty()==0)
    {
        l=q.size();
        if(l>1)
            flag=1;     // multiple sequence possible for sure but check whether this is fully possible or not
        item=q.front();
        q.pop();
        ans[k++]=item;
        for(it=list[item].begin();it!=list[item].end();it++)
        {
            j=*it;
            indegree[j]--;
            if(indegree[j]==0)
                q.push(j);

        }
    }
    if(k!=n)
        flag=2; // no sequence is possible.
}

This algorithm is too slow! or just a naive implementation. What further Improvements are possible for this. or how can i use toplogical sort using DFS for this ?

Theorem:
A directed graph has a unique topological sort if and only if it is a directed chain.

Proof left as exercise for you


To determine if a directed graph has a unique topological sort, all you have to do is:

  1. Find any node with in-degree 0
    • If there isn't one, the graph has a cycle and does not have a unique topological sort
  2. Repeatedly follow the first out-edge of the current node, adding the visited nodes to a set as you go
  3. Stop when:
    • You reach a node you have already visited - the graph has a cycle, so it does not have a unique topological sort
    • You reach a dead end - the graph has a unique topological sort if and only if the set of visited nodes contains all of the nodes in the graph

Your code doesn't seem to follow this simple approach. (Correct me if I'm wrong about that!) So, rather than trying to figure out if your code is correct or not, I would suggest simply using the algorithm outline above.

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