LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2011, 05:09 PM   #1
ddebbie90
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Rep: Reputation: 0
Dijkstra's algorithm


hello,
i'm trying to implement the dijkstra's algorithm in c but i face some problems.My algorithm is implemented to follow the edge with the minimum weight. For example, i am in a node that is connected to the node i want to go, but because the edge has more weight than another edge to a node that is not connected to the node i want to go, the algorithm follows the wrong path. I think i need one more condition, but i can figure out one. Somebody to help?
 
Old 09-12-2011, 06:12 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ddebbie90 View Post
For example, i am in a node that is connected to the node i want to go, but because the edge has more weight than another edge to a node that is not connected to the node i want to go, the algorithm follows the wrong path.
If I understand what you just wrote, then that is correct behavior.

Assume you are at node A and your destination is B.

There is an edge of cost 6 from A to B.
There is an edge of cost 2 from A to C.
There is an edge of cost 1 from C to D.
There is an edge of cost 2 from D to B.

So the low cost path from A to B is through C.

When you are at A, you don't know whether that path from C to B exists. So the search algorithm correctly tries C first to see if such a path exists.

If there isn't an optimal path through C, the algorithm will later find the direct path from A to B.

Last edited by johnsfine; 09-12-2011 at 06:27 PM.
 
1 members found this post helpful.
Old 09-12-2011, 10:03 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Are you using a queue of vertices already visited? Every time you encounter a vertex insert it into the queue, prioritized by the distance it took to get there on that particular path. Keep visiting and popping the head vertex in the queue and add the vertices its connected to to the queue. Once a vertex is encountered in the queue it can be marked as "done", i.e. the shortest path to it has been found, and once you encounter your endpoint at the head of the queue you're done.
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need LARGE data sets for testing the Dijkstra's Algorithm which I implemented Aquarius_Girl General 2 07-04-2011 11:01 AM
LXer: Validation in Dijkstra's shunting yard algorithm LXer Syndicated Linux News 0 10-29-2010 02:20 PM
LXer: Parsing object-oriented expressions with Dijkstra's shunting yard algorithm LXer Syndicated Linux News 0 10-04-2010 04:20 PM
Dijkstra Algorithm complexity xeon123 Programming 6 03-22-2010 12:46 AM
token bucket algorithm vs Leaky bucket algorithm xeon123 Linux - Networking 2 03-26-2007 04:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration