943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2258
  • C++ RSS
Dec 1st, 2008
0

Directed graph

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
steve2up is offline Offline
1 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: LPTSTR
Next Thread in C++ Forum Timeline: Mind-blowing problem here (At least for me)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC