implements Dijkstra's Algorithm