History? provides a powerful scripting language for dependency manipulation, and can be used to solve a wealth of tasks.
This page contains a growing set of examples of its use.
Example 1: we want to upgrade all the packages related to a given package P, and the current tools don't know how to do this. Here is how you can get the job done