I am trying to solve an algorithmic problem.
First of all the user inputs the coordinates of some nodes.
I have to go to the node with the biggest x( let name it E) and then to return to the start that will always be the node (0,0)( let name it S).
In this cycle i have to pass from all the nodes but there are some restrictions. When i go to the E i should go along x axes and xpreviousxnext.
I have to find the shortest path to do that cycle.
Somewhere i found that i have to use dynamic programming but i am not so good in that and i didn't found many information for that solution.
Can you help me to figure out how to solve that?
Thanks in advance.
As I mentioned in your other thread: http://www.daniweb.com/forums/thread176078.html
you need to articulate the problem more carefully. This is a graphical problem. It's hard enough to visualize with just a written description. It's impossible to visualize with imprecise language:
In this cycle i have to pass from all the nodes
What does this mean? All nodes must be visited once and exactly once?i should go along x axes
So y is equal to 0?
Can x be negative? Can y be negative? Can y be non-zero? This is a paper and pencil exercise first. You need to really nail down the problem, and that is before you even get to the programming part of it. All we know is what you describe, so you must be precise and give examples.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Yes but there are 2^nodes ways to traverse from start to end.
How can i find the correct?
No there aren't. There are (n-1)! ways to go through n nodes, visiting each node once and only once, given a particular starting node as one of the n nodes, as you have in this case. In your case, given your restrictions, the vast majority of these paths are illegal. If no two nodes have the same x value, you have one, count 'em, one, legal path. You need to sit down with paper and pencil and draw it out, because you do not understand the problem.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I thought to make a tree that will have each possible path to go from S to E.
An example is here:
http://www.mypicx.com/03022009/shortest_cycle/
But it doesn't work and i don't know why.
Can you see any mistake?
Do you think that a tree with the right implementation could solve the problem?
A tree will have exactly one path from one node to another. Thus, any problem involving more than one possible path will not involve a tree. Also, only a trivial tree where each node can have only one child (which is a linked list) will visit each node once and only once.
Your code and the "tree" you posted don't take into account the fact that two nodes are connected only if there is no third node with an x value that is between the two nodes' x values. Thus, you can't travel directly from node 2 to node 5 in your graphic, so they need to not be directly connected, as they currently are.
You also have multiple nodes with the same value in the graphic your wrote, so I don't know what it's supposed to represent.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711