Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,036 people online right now. Registration is fast and FREE... Join Now!




finding reachable nodes in a graph

 
Reply to this topicStart new topic

finding reachable nodes in a graph

jeffd
post 26 Mar, 2006 - 05:03 PM
Post #1


New D.I.C Head

*
Joined: 26 Mar, 2006
Posts: 1


My Contributions


Hello all,

I am having difficulties implementing a function checking if the path exists from one node to another in a graph. In my way of thinking it should simply be a modification of either a breadth or depth first traversal where the City keys (yes, traveling salesman problem) are passed in. I typecast the city objects to Vertex objects in order to hopefully allow for comparison. The isReachable is called from main by

CODE

           for(i = 1; i <= g.size(); i ++)
           {
             for(j = 1; j <= g.size(); j++)
             {
                 try
                 {
                 g.isReachable(City(j, " "), City(i, " "));
                 wt = 1;
                 }
                 catch(GraphException e)
                 {
               wt = 0;
                 }
                 cout << setw (4) << wt << " ";
             }
             cout<<endl;
           }
           break;

*********************************************
CODE

bool Graph<T>::isReachable(T fromKey, T toKey) const
{
  Vertex<T>* walkPtr;
  Vertex<T>* toPtr;
  Queue<Vertex<T>*> queue;
  Edge<T>* edgeWalk;  
  Vertex<T>* tmp;
 
  if (isEmpty())
     return 0;
   
   toPtr = (Vertex<T> *) &toKey;
   walkPtr = (Vertex<T> *) &fromKey;
     
  walkPtr = first;
  while (walkPtr != NULL)
  {
     walkPtr->processed = 0;
     walkPtr = walkPtr->pNextVertex;
  }
  walkPtr = first;
  while (walkPtr != NULL)
  {
     if (walkPtr->processed < 2)
     {
        if (walkPtr->processed < 1)
        {
           queue.enqueue(walkPtr);
           walkPtr->processed = 1;
        }
     }
     while (!queue.isEmpty())
     {
        try
        {
           tmp = queue.dequeue();
           //visit(tmp->data);
           tmp->processed = 2;
           edgeWalk = tmp->pEdge;
           while (edgeWalk != NULL)
           {
              toPtr = edgeWalk->destination;
              if(toPtr == walkPtr)
               return 1;
              if (toPtr->processed == 0)
              {
                 toPtr->processed = 1;
                 queue.enqueue(toPtr);
              }
              edgeWalk = edgeWalk->pNextEdge;
           }
        }
        catch(QueueException e)
        {
           cerr<<e.what();
           exit(1);
        }
     }
     walkPtr = walkPtr->pNextVertex;
  }

The code compiles properly but I am unable to generate any paths. I use Totalview as a debugger and it seems that everything is going through properly.

Any suggestions would be appreciated.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month