943,553 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3100
  • Java RSS
Oct 5th, 2006
0

algorithm problem with recursion

Expand Post »
I have an algorithm problem with recursion:

i have a route from A to D and it looks like this
Java Syntax (Toggle Plain Text)
  1. [A, B, C, D]

the alphabet in the above list is an object of a class, lets call that class Nodes

each of this Nodes object they have some attributes... these are of type set..

the first set will store a bunch of other Nodes (these represent the current nodes neighbour)
the second set will store a bunch of tracks (Integer object representing what tracks the current nodes belong to)

(... bare with me here... )

so now i write up some code that initialise all the nodes... and here is the output...
Java Syntax (Toggle Plain Text)
  1. A: [B, Z, E] // A's next neighbour
  2. A: [2, 1, 3] // A belongs to these tracks
  3. B: [C, A]
  4. B: [1]
  5. C: [B, D]
  6. C: [4, 1]
  7. D: [F, C]
  8. D: [4]

and here is the code: (i removed the System.out... to shorten the code..)

Java Syntax (Toggle Plain Text)
  1. public class Path {
  2. private Nodes a, b, c, d, e, f, z;
  3. private List path = new ArrayList();
  4. public Path() {
  5.  
  6. a = new Nodes("A"); //used nodes
  7. b = new Nodes("B"); //used nodes
  8. c = new Nodes("C"); //used nodes
  9. d = new Nodes("D"); //used nodes
  10. e = new Nodes("E"); // wont be used directly
  11. f = new Nodes("F"); // wont be used directly
  12. z = new Nodes("Z"); // wont be used directly
  13. path.add(a);
  14. path.add(b);
  15. path.add(c);
  16. path.add(d);
  17.  
  18. // setting Nodes a
  19. a.getNext().add(b);
  20. a.getNext().add(e);
  21. a.getNext().add(z);
  22.  
  23. Integer[] iA = {1, 2, 3};
  24. a.getTrackID().addAll(Arrays.asList(iA));
  25.  
  26. // setting Nodes b
  27. b.getNext().add(a);
  28. b.getNext().add(c);
  29. b.getTrackID().add(Integer.valueOf(1));
  30.  
  31. // setting Nodes c
  32. c.getNext().add(b);
  33. c.getNext().add(d);
  34. c.getTrackID().add(Integer.valueOf(1));
  35. c.getTrackID().add(Integer.valueOf(4));
  36.  
  37. // setting Nodes d
  38. d.getNext().add(c);
  39. d.getNext().add(f);
  40. d.getTrackID().add(Integer.valueOf(4));
  41.  
  42. }
  43.  
  44. public static void main(String[] args) { new Path();}
  45. }
  46.  
  47. class Nodes{
  48. private Set next = new HashSet();
  49. private Set trackID = new HashSet();
  50. private String name = "";
  51. Nodes(String name){ this.name = name;}
  52. public Set getNext() { return next;}
  53. public Set getTrackID() { return trackID;}
  54. public String toString() {return this.name;}
  55. }

what i want to do is to come up with some recursive algorithm so that it would print out something like this...

Java Syntax (Toggle Plain Text)
  1. Path = A - B - C - D
  2. A to C track 1
  3. C to D track 4
so i've come up with a pseudo code...

Java Syntax (Toggle Plain Text)
  1. method1(Node startNode, List path){
  2.  
  3. iterate through startNode.getNext set
  4.  
  5.  
  6. if nodes return is in the list path{
  7.  
  8. newNodes = returned nodes;
  9.  
  10. path.remove(startNodes);
  11. if(startNodes.getTrack() == newNodes.getTrack())
  12. { // if startNodes shares the same track as the next nodes
  13. then line = the track;
  14. method1(newNodes, path);
  15. }else{
  16. print startNode "to" newNodes "track" line
  17. if there is still some journey
  18. method(newNodes, path)
  19. }
  20.  
  21.  
  22. }
  23.  
  24. }

i've also come up with an actuall code but for some reason it'll only prints out the first step...

the code for this to follow...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
fdrage is offline Offline
28 posts
since Jun 2005
Oct 6th, 2006
0

Re: algorithm problem with recursion

Is it just me, but I don't get what you're doing at all.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 6th, 2006
0

Re: algorithm problem with recursion

:!: Hey "fdrage",

I think u r not very clear wat r u doing or if I have misunderstood u r not able to say clearly wat do u wanna say actually.

If u dont mine can u just make it more clear and understandable.

Bye,
Live and Let Live!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dark.developer is offline Offline
1 posts
since Sep 2006
Oct 6th, 2006
1

Re: algorithm problem with recursion

I think I may know what you're talking about, as I faced the apparent issue myself just last week. It was quite something. If I've misunderstood and all the following information is unrelated, then I guess I'm just practicing my explanatory skills.

Basically, you have Nodes, and each Node can have some number of links to other Nodes. You want a function to find the quickest rout through the network from one Node to another, by searching for a the second Node recursively, starting at the first Node.

Think of it in terms of traffic:
http://i12.photobucket.com/albums/a2...rums/Paths.gif
Someone is at the Green house, and wants to find their way to the Red house. Each Blue block is an intersection, and they're linked by roads. You want a method to determine the shortest path the person could follow, from which node to which node until they reach their destination. In the example I've provided (I quickly marked it up, excuse the sloppiness) the fastest rout would be Green - A - B - E - Red, right? There are many options, such as ACDE, ACDBE, ABDE, ABDCABE, etc etc etc, but the shortest is ABE.

Now, to get your program to search for the Red house, it will work its way out from the Green house and search up and down streets recursively.

Each House is a Node, and the Blue intersections are Nodes as well. As I've mentioned, Nodes have links, and the link-tree for this network would look like:

Green house: A
Red house: E
A: B, C, Green house
B: A, D, E
C: A, D
D: B, C, E
E: B, D, Red house

The program will work its way out from the Green house, checking its children (its links) for the Red house, and if the Red house is not found, the program moves one step further into the network, checking the Nodes of the Nodes it just checked. Complicated? Yes, and No.

One major issue is that the roads loop back, so theoretically, if the program just checks a Node's children and then checks the grandchildren, and then the great-grandchildren, and so on, it will throw its self into an infinite loop and continue to come back and re-check the same nodes over and over again. We have to prevent this from happening. The way I did this, was that I kept a Binary Hash Tree of the Nodes already checked, and I only considered a Node if it hadn't already been looked at.

Here's what the program would do while searching for the Red house:
(Bold is the children that were returned to be checked. Only those which haven't previously been checked are included)

{Green house} does not include Red house, (take children)
{A} does not include Red house, (take children)
{B, C} does not include Red house, (take children)
{D, E} does not include Red house, (take children)
{Red house} includes Red house!

If you understand the logic by now, then good. Write some new Psudo-Code and think your way through the extensive process of writing the Java. Here are the methods I wrote to help me complete the task. Though I haven't included the code for all of my methods, maybe the following can provide some additional clues:

Java Syntax (Toggle Plain Text)
  1. public Node CheckLevelRecusrive (Node FromNode, Node ToNode);
  2. private Node CheckLevel (Node[] Choices, Node SearchForMe);
  3. private Node[] ReturnLevelChildren (Node[] Parents);
  4.  
  5. // =========================
  6.  
  7. private Object HashLock = new Object (); // Used to lock the Hash table
  8. private BHI HashesCheckedRoot = null; // Prevent search back-tracking/blocking
  9.  
  10. private class BHI { // Binary Hash Item
  11. int HashVal;
  12. BHI LessThanMe, MoreThanMe; // Used in Binary Search
  13. public BHI (int Hash) { HashVal = Hash; }
  14. }
  15.  
  16. private boolean FindOrAddHash (int Hash) {
  17. // Search for an existing hash entry which matches NewHash
  18. // Return true if found, false if not
  19. // If not found, add Hash to HashesCheckedRoot table
  20. // ----------
  21. BHI ParentBHI = null;
  22. BHI CurrentBHI = HashesCheckedRoot;
  23. // ----------
  24. // STEP 1: Search for the Hash record
  25. while (CurrentBHI != null) {
  26. if (CurrentBHI.HashVal == Hash) return true; // Found
  27. ParentBHI = CurrentBHI;
  28. CurrentBHI = (Hash < CurrentBHI.HashVal) ? CurrentBHI.LessThanMe : CurrentBHI.MoreThanMe;
  29. }
  30. // ----------
  31. // STEP 2: Not found, Insert Hash at ParentBHI
  32. if (ParentBHI == null) HashesCheckedRoot = new BHI (Hash);
  33. else if (Hash < ParentBHI.HashVal) ParentBHI.LessThanMe = new BHI (Hash);
  34. else ParentBHI.MoreThanMe = new BHI (Hash);
  35. // -----
  36. return false;
  37. }

I hope I've been at least somewhat helpful :rolleyes:
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005
Oct 7th, 2006
0

Re: algorithm problem with recursion

Click to Expand / Collapse  Quote originally posted by Cudmore ...
I think I may know what you're talking about, as I faced the apparent issue myself just last week. It was quite something. If I've misunderstood and all the following information is unrelated, then I guess I'm just practicing my explanatory skills.

Basically, you have Nodes, and each Node can have some number of links to other Nodes. You want a function to find the quickest rout through the network from one Node to another, by searching for a the second Node recursively, starting at the first Node.

Think of it in terms of traffic:
http://i12.photobucket.com/albums/a2...rums/Paths.gif
Someone is at the Green house, and wants to find their way to the Red house. Each Blue block is an intersection, and they're linked by roads. You want a method to determine the shortest path the person could follow, from which node to which node until they reach their destination. In the example I've provided (I quickly marked it up, excuse the sloppiness) the fastest rout would be Green - A - B - E - Red, right? There are many options, such as ACDE, ACDBE, ABDE, ABDCABE, etc etc etc, but the shortest is ABE.

Now, to get your program to search for the Red house, it will work its way out from the Green house and search up and down streets recursively.

Each House is a Node, and the Blue intersections are Nodes as well. As I've mentioned, Nodes have links, and the link-tree for this network would look like:

Green house: A
Red house: E
A: B, C, Green house
B: A, D, E
C: A, D
D: B, C, E
E: B, D, Red house

The program will work its way out from the Green house, checking its children (its links) for the Red house, and if the Red house is not found, the program moves one step further into the network, checking the Nodes of the Nodes it just checked. Complicated? Yes, and No.

One major issue is that the roads loop back, so theoretically, if the program just checks a Node's children and then checks the grandchildren, and then the great-grandchildren, and so on, it will throw its self into an infinite loop and continue to come back and re-check the same nodes over and over again. We have to prevent this from happening. The way I did this, was that I kept a Binary Hash Tree of the Nodes already checked, and I only considered a Node if it hadn't already been looked at.

Here's what the program would do while searching for the Red house:
(Bold is the children that were returned to be checked. Only those which haven't previously been checked are included)

{Green house} does not include Red house, (take children)
{A} does not include Red house, (take children)
{B, C} does not include Red house, (take children)
{D, E} does not include Red house, (take children)
{Red house} includes Red house!

If you understand the logic by now, then good. Write some new Psudo-Code and think your way through the extensive process of writing the Java. Here are the methods I wrote to help me complete the task. Though I haven't included the code for all of my methods, maybe the following can provide some additional clues:

Java Syntax (Toggle Plain Text)
  1. public Node CheckLevelRecusrive (Node FromNode, Node ToNode);
  2. private Node CheckLevel (Node[] Choices, Node SearchForMe);
  3. private Node[] ReturnLevelChildren (Node[] Parents);
  4.  
  5. // =========================
  6.  
  7. private Object HashLock = new Object (); // Used to lock the Hash table
  8. private BHI HashesCheckedRoot = null; // Prevent search back-tracking/blocking
  9.  
  10. private class BHI { // Binary Hash Item
  11. int HashVal;
  12. BHI LessThanMe, MoreThanMe; // Used in Binary Search
  13. public BHI (int Hash) { HashVal = Hash; }
  14. }
  15.  
  16. private boolean FindOrAddHash (int Hash) {
  17. // Search for an existing hash entry which matches NewHash
  18. // Return true if found, false if not
  19. // If not found, add Hash to HashesCheckedRoot table
  20. // ----------
  21. BHI ParentBHI = null;
  22. BHI CurrentBHI = HashesCheckedRoot;
  23. // ----------
  24. // STEP 1: Search for the Hash record
  25. while (CurrentBHI != null) {
  26. if (CurrentBHI.HashVal == Hash) return true; // Found
  27. ParentBHI = CurrentBHI;
  28. CurrentBHI = (Hash < CurrentBHI.HashVal) ? CurrentBHI.LessThanMe : CurrentBHI.MoreThanMe;
  29. }
  30. // ----------
  31. // STEP 2: Not found, Insert Hash at ParentBHI
  32. if (ParentBHI == null) HashesCheckedRoot = new BHI (Hash);
  33. else if (Hash < ParentBHI.HashVal) ParentBHI.LessThanMe = new BHI (Hash);
  34. else ParentBHI.MoreThanMe = new BHI (Hash);
  35. // -----
  36. return false;
  37. }

I hope I've been at least somewhat helpful :rolleyes:
Ah so basically he/she's trying to find a shortest path algo of which there are many to choose from.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

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 Java Forum Timeline: help with recursive method
Next Thread in Java Forum Timeline: Program help





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


Follow us on Twitter


© 2011 DaniWeb® LLC