title: How to Sort a Dependency Graph in Java?
body: <p>I've been given a task to process several RPM package names from a file, uninstall them (if necessary) with a 'cascading delete' option and then install in the reverse order. For example, if there're packages <code>a</code>, <code>b</code>, and <code>c</code>, where <code>c</code> depends on <code>b</code> and <code>b</code> depends on <code>a</code>, the uninstallation order should be <code>c, b, a</code> and installation order <code>a, b, c</code>.</p>
  
  <p>After some thinking, it appears that one way to do this is to build a dependency graph and then sort on the degree of a vertex. So far, I've found 2 libraries, <a href="http://jgrapht.org" rel="nofollow noreferrer">JGraphT</a> and <a href="http://www.i3s.unice.fr/~hogie/grph" rel="nofollow noreferrer">Grph</a>, both promising but with pitiful or non-existent code samples. The former has a <code>org.jgrapht.alg.util.VertexDegreeComparator</code> and the later <code>grph.algo.sort. OutDegreeSorter</code>. Before I dig into the source code and try to figure out how to use those, I'm wondering if there are better ways to do this (including algorithm and libraries)? The number of packages is not going to be huge (&lt; 100), so performance is not a big concern. Maintenance of the code I'll write, is.</p>
  
  <p><em>Hasty duplicate call out alert</em>: I read this <a href="https://stackoverflow.com/questions/5604475/how-should-i-represent-a-dependency-graph-in-java">thread</a> and it is not what I'm looking for. The poster there is asking how to build a dependency graph, which is not my question.</p>