Question on dependency execution
Hi,
I'm completely new to java. I'm trying the tackle this following scenario. For e.g. I've a list of “Tasks” and their dependencies. E.g. Task D depends on (Task A and Task B) , Task A depends on Task B, Task B depends on Task C.
I'm trying to write a program to print out the sequence of these tasks (For example, using the case above, it should be C, B, A, D).
1. What is the best way to approach this ?
2. What possible ways I can test this design?
3. How can I make sure that there's no circular dependency?
Any pointers will be highly appreciated.
-Thanks
Re: Question on dependency execution
You can use a graph/map data structure with nodes being tasks and the paths being the dependencies. If from any node you can get back to the starting node, you will have circular dependencies (some kind of variation on Dijkstra's Algorithm should work).
If you cannot, then there are no circular dependencies. At this point just pick a node and try to recursively resolve it's dependencies first (mark them off as they get done), then resolve that node and mark it as being done.. You're done when all jobs have been marked done.
edit: A second alternative to resolving tasks is via a Priority Queue. However, I think to use a Priority Queue you must first check to make sure there are no circular dependencies first.
Re: Question on dependency execution
Thanks helloworld...I was looking into digraphs algorithm....will explore your suggestion and post my findings