Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Algorithm � Find all the available paths in a graph

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I need to find all the available paths from a root object.
Since in java objects can have references in the two directions, we are talking on a graph.

I want to get a reference to a class type and return a list of all the available paths from this object type.
If I have a reference from A->B->C->D and also A->D I need to have the two paths.

Performance is not important because this process run in offline. Simplicity is much important.

Can you please advice on the suitable algorithm for this?

Thank you
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you comfortable with recursion? You can make a "brute force" routine to try every path and report the ones that get to "D". Off the top of my head, no verification, something like ...

See if you can make that run in Java, show us your code, and we'll talk about some optimizations.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is DFS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic