algorithm problem with recursion

Reply

Join Date: Jun 2005
Posts: 28
Reputation: fdrage is an unknown quantity at this point 
Solved Threads: 0
fdrage's Avatar
fdrage fdrage is offline Offline
Light Poster

algorithm problem with recursion

 
0
  #1
Oct 5th, 2006
I have an algorithm problem with recursion:

i have a route from A to D and it looks like this
  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...
  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..)

  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...

  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...

  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...
---
Wasps
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: algorithm problem with recursion

 
0
  #2
Oct 6th, 2006
Is it just me, but I don't get what you're doing at all.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 1
Reputation: dark.developer is an unknown quantity at this point 
Solved Threads: 0
dark.developer dark.developer is offline Offline
Newbie Poster

Re: algorithm problem with recursion

 
0
  #3
Oct 6th, 2006
:!: 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!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 74
Reputation: Cudmore is an unknown quantity at this point 
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Re: algorithm problem with recursion

 
1
  #4
Oct 6th, 2006
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:

  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:
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: algorithm problem with recursion

 
0
  #5
Oct 7th, 2006
Originally Posted by Cudmore View Post
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:

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC