Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving list from a map mapping to an arraylist

 
Mitesh Parmar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a map datastructure in which keys map to an arraylist. The values of the arraylists elements themselves are keys within the map.

For example:

Map List of previous keys
N ----------------> M
M ----------------> I, L
L ----------------> K
K ----------------> J
J ----------------> D
D ----------------> B
I ----------------> G
G ----------------> F,H
H ----------------> D
F ----------------> C
C ----------------> B
B ----------------> A

So from this we can see that to get from the end: N to the start: A
there are 3 possible routes:

N -> M -> I -> G -> F -> C -> B -> A
N -> M -> I -> G -> H -> D -> B -> A
N -> M -> L -> K -> J -> D -> B -> A

I need the above three routes returned in an ArrayList which are all held in a List : List routeCollections

The way to get this done involves recursion, I have the code written in Java but it does not work beyond creating the first route.



This only works for the first run.

Please help...
[ September 03, 2004: Message edited by: Mitesh Parmar ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example looks like a tree to me.
But perhaps other examples are only (distinct) graphs:

or contain recursions:


And please edit your post and use code-tags.
 
Mitesh Parmar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
Your example looks like a tree to me.
But perhaps other examples are only (distinct) graphs:

or contain recursions:


And please edit your post and use code-tags.


PLease could you tell me how to edit the code and add the code tags.
Thank You...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On top of your posting is a icon-group.
One shows a piece of paper with a pencil.
That's for editing.

Before inserting code, you hit the code-button below the edit-window.
You insert your code between the two tags.
Here I inserted blanks into the tags, to make them visible:
[ CODE] show (example);[ /CODE]
Result without blanks:

You may add these tags manually.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code-tags make it much better readable - don't you think so?

But your question remains unresponded.
I have to correct myself - the data-structure isn't a tree, it's a directed graph.
I thought about it a while and tested some code but it isn't working properly.
To reconstruct your error, I would need the missing code at least.

I could send you my code - 136 rows in java-1.5 notation as private message. (Guess it's a bit much for the whole audience).

I get:

, the 'A' allways missing, and the forking a step off.
But at least three lines.
[ September 03, 2004: Message edited by: Stefan Wagner ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got a working approach - interested?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic