Instead of depth-first search binary search was implemented