Graph search problem (with a first pass at dynamic programming