optimized path algorithm