Some useful functions for handling graphs in Java