depth-first ivy algorithm