Did depth-first search algorith in Java