DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Directed graph (http://www.daniweb.com/forums/thread160349.html)

steve2up Dec 1st, 2008 10:40 pm
Directed graph
 
Hi everyone, I am a U.student from China, I would like ask a question about my assignment.

For the directed graph, I would like to perform topological sort for sorting up nodes, but I encountered some problem in queue which is able to be removed or added with certain elements.

My question is how I can perform queue in topological sort function? my algorithm is here
// The topological sort
void DGraph::topologicalSort(){
        if(isTPSorted==false){
                int j=0;
                Queue ts;
                while(ts.size()!=0)
                        /*dequeue()  dequeue for queue*/
                for(int i=0;i<50;i++){
                        if (visitedTable[i]>=0 && inDegTable[i] == 0)
                                /*enqueue(i)  enqueue for queue*/
                }
                while(ts.size()>0){                         
                        predTable[j]=dequeue();/*putting into predTable*/
                        int a=predTable[j];
                        j++;
                        for(int k=0;k<50;k++){
                                if(adjMatrix[a][k]==1){
                                        inDegTable[k]--;
                                        if (inDegTable[k] == 0){
                                            /*enqueue(k) enqueue for queue*/
                                        }
                                }
                               
                        }
                }
                isTPSorted = true;
        }
        return;
}

my professor gave a hint to perform queue:

In the implementation of the topological sort, a queue data structure is required. he said I can use the queue from the standard template library. Here are some short descriptions of the functions provided by the STL queue.

empty : true if the queue has no elements
front pop: returns a reference to the first
push : element of a queue removes the first element of a queue adds an element to the end of the queue

For example, include header <queue> and declare my queue as std::queue <int> Queue. And use push as Queue.push(int).

I could find some information in http://www.sgi.com/tech/stl/queue.html. but I don't catch up the point.

Thank you very much


All times are GMT -4. The time now is 5:07 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC