Collections.sort( sorted,
new Comparator() {
public int compare(final Object o1,
final Object o2) {
final BaseVertex v1 = (BaseVertex) o1;
final BaseVertex v2 = (BaseVertex) o2;
int v1OutDegree = v1.getSourceConnections().size();
int v2OutDegree = v2.getSourceConnections().size();
if ( v1OutDegree < v2OutDegree ) {
return 1;
}
if ( v1OutDegree > v2OutDegree ) {
return -1;
}
return 0;
}
} );
final LinkedList optimized = new LinkedList();
boolean front = false;
for ( final Iterator vertexIter = sorted.iterator(); vertexIter.hasNext(); ) {
final BaseVertex vertex = (BaseVertex) vertexIter.next();
if ( front ) {
optimized.addFirst( vertex );
} else {
optimized.addLast( vertex );