Graph exploration algorithm