Topological sort, also gave a feel of how to use Java's HashMap/ArrayList etc.