CTCI 10.5 check if graph has a loop