1

Given this dependency graph: What's a "good" approach to iterate through it from bottom to top?

My expected results for each "cycle" are:

Iteration step "1": Project B, Project D, Project Z, Project O 
Iteration step "2": Project C, Project W, Project V, Project Q
Iteration step "3": Project A, Project M
Iteration step "4": Start X // End

Brainstorming

// PSEUDO CODE: Find and return "next projects fixes" to perform. 
// -> All projects with no or already fixed dependencies. 
FUNC FindNextDependciesToFix ( NODE StartNode, BYREF LIST<NODE> RefNextProjectsToFix )
{    
 ... // Algorithm ?
}

Reason why "Depth-first search" does not work:

DO
{
 FindNextDependciesToFix (StartX, FixNextList);
 CallASYNCAndWaitForEndOfFix (FixNextList);
 // <- Wait till end of project fix (async...) 
} WHILE ( FixNextList.IsEmpty() ); 

Algorithm

I really don't want to reinvent the wheel: So is there already an algorithm which solve this problem or does anyone have a "clever" approach?

Chris P.
  • 13
  • 1
  • 3

2 Answers2

1

You probably want topological sort to go through the graph of dependencies. You can do this with DFS (depth-first search) and BFS (breath-first search) as well -- both mentioned in pseudocode on the wikipedia link. Both are linear in input size.

Larry
  • 4,491
  • 2
  • 24
  • 16
0

Do a topological sort which gives nodes in order of depth. Then if you want to find the boundaries between different depths, use a dynamic programming algorithm. This solution is linear in the size of the graph, O(|V| + |E|).

rlibby
  • 5,931
  • 20
  • 25
  • Thank you for you answer. "use a dynamic programming algorithm" - What do you exactly mean by that? – Chris P. Mar 04 '11 at 22:10
  • @Chris P., topological sort gives you nodes in order of depth, but doesn't actually give you their depth! So if you want to know the depth you still need to compute it. There are lots of ways to do this but in particular there's a O(|V| + |E|) dynamic programming algorithm for it. See [here](http://stackoverflow.com/questions/5004973/algorithm-for-computing-partial-orderings-of-dependency-graphs/5057093#5057093). – rlibby Mar 04 '11 at 23:44