Precalculate the entire graph

Do all dependency ordering from a single graph calculated at the
begining of the traversal. This is equivalent to what we were doing
before - precalculating the cleanup dependencies and obtaining the
update dependencies from the current template - but makes the code much
simpler (and easier to understand). The actual relationships remain
stored in the individual resources, to reduce contention.