implement prev and cost in graph.py