Directed graph

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1
Reputation: steve2up is an unknown quantity at this point 
Solved Threads: 0
steve2up steve2up is offline Offline
Newbie Poster

Directed graph

 
0
  #1
Dec 1st, 2008
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
  1. // The topological sort
  2. void DGraph::topologicalSort(){
  3. if(isTPSorted==false){
  4. int j=0;
  5. Queue ts;
  6. while(ts.size()!=0)
  7. /*dequeue() dequeue for queue*/
  8. for(int i=0;i<50;i++){
  9. if (visitedTable[i]>=0 && inDegTable[i] == 0)
  10. /*enqueue(i) enqueue for queue*/
  11. }
  12. while(ts.size()>0){
  13. predTable[j]=dequeue();/*putting into predTable*/
  14. int a=predTable[j];
  15. j++;
  16. for(int k=0;k<50;k++){
  17. if(adjMatrix[a][k]==1){
  18. inDegTable[k]--;
  19. if (inDegTable[k] == 0){
  20. /*enqueue(k) enqueue for queue*/
  21. }
  22. }
  23.  
  24. }
  25. }
  26. isTPSorted = true;
  27. }
  28. return;
  29. }

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
Last edited by Ancient Dragon; Dec 1st, 2008 at 10:48 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC