Implementation of Djikstra's algorithm