Thread: Path Finding Using Adjacency List.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    Path Finding Using Adjacency List.

    Background: Im not trying to impliment any particular algorithm, but id like to do a little pathfinding. Im storing several things in several places while testing different ideas. Each node object has a list of adjacencies (std::list<Node*>), and the Network object has a list of all nodes, and a map of lists of adjacencies sorted by node name (std::map<std::string, std::list<Node*> >).

    My question: How do i go about taking the current stored information and go about finding paths from one node to another? Right now im simply interested in shortest path implimentations. Im interested in real code here, not abstract imagery. If i wanted that id read a book.

    The current working (although not SHORTEST path) function im using is something like this:
    Code:
    Node* NodeNetwork::FindBestNextHopBetween(Node* Current, Node* Destination, int depth)
    {
    	if(depth>NetworkNodes.size()) { return NULL; }//failure due to routing loop.
    	std::list<Node*>::iterator AdjacenciesIter;
    	for(AdjacenciesIter=Current->Adjacencies.begin();AdjacenciesIter!=Current->Adjacencies.end();AdjacenciesIter++)
    	{
    		if((*AdjacenciesIter)==Destination)
    		{
    			return Current;
    		}
    		Node* NextBestHopTemp = FindBestNextHopBetween((*AdjacenciesIter), Destination, depth+1);
    		if(NextBestHopTemp==(*AdjacenciesIter))
    		{
    			if(depth==0) {return NextBestHopTemp; } //we are at the top, return our next found hop.
    			if(depth!=0) {
    				return Current; //we are deep, go up a level.
    			}
    		}
    	}
    	return NULL;//failure
    }
    Anyways, any imput you could offer would be a great help. Eventually id like to impliment a possible weighting system for adjacencies, however im saving the complicated stuff for another day =P.

    Edit: After a little testing, i believe my code always takes the longest path lol. it will even bounce back and forth until it finds a path exactly as long as the node list. Thats to be expected i suppose. The problem i have with "shortest path" is that to find the shortest path, i have to find all the paths and then pick the shortest one. Seems kinda wasteful of resources. Oh well, ill get it eventually. =P
    Last edited by Geolingo; 05-15-2005 at 08:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM