Dijkstra
This page provides introduction to Dijkstra Algorithm.
Overview
Dijkstra's is single source shortest path algorithm. It works on both direct and undirected graph but doesn't work if graph has negative weight.
Let's apply dijkstra's algorithm on below undirected graph.
Suppose we want to calculate the distance from node . Initially, we set the distance from to all other nodes to the maximum possible value, except for itself, and store these distances in a hashmap. We also initialize a priority queue to store the distance of each specific node from .
We poll the element with the shortest distance from the priority queue() and visit all its neighbor(). If the current distance of a neighbor node from in the hashmap is greater than the sum of the current node's distance from and the distance to that neighbor, we update the distance in the hashmap.
For example, the current distance of node in hashmap is . We override it's distance in distance hashmap to which is sum of (distance of current node from ) and (distance between current node and it's neighbor ). Similarly, for we override the distance for .
Nodes marked in gray represent polled nodes, while those marked in green are newly added nodes. A node is added to the priority queue only if a shorter distance to reach that node is found compared to the one currently stored in the distance hashmap.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Notice we did not add node back into in the priority queue, as it's new distance through current path is greater than existing distance, which was found through another path.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Notice how we readded node to priority queue when we found better path to reach and also updated distance hashmap, as show in above image. Removing existing distance of node from priority queue is an option but it takes time complexity.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Poll node which has shortest distance in priority queue and visit all it's neighbors.
Priority Queue has no more element, you have shortest distance of node from all the other nodes in the graph.
- Java
class Edge {
char ch;
int distance;
Edge(char ch, int distance) {
this.ch = ch;
this.distance = distance
}
@Override
public int compareTo(Edge other) {
// ascending order by distance
return Integer.compare(this.distance, other.distance);
}
}
public HashMap<Character, Integer> dijkstra(HashMap<Character, List<Edge>> graph) {
HashMap<Character, Integer> distance = new HashMap<>();
PriorityQueue<Edge> pq = new PriorityQueue<>();
for(Character ch: graph.keySet()) distance.put(ch, Integer.MAX_VALUE);
distance.put('A', 0);
pq.offer(new Edge('A', 0));
while(!pq.isEmpty()) {
// poll the node with shortest distance
Edge e = pq.poll();
// visit all children of current node
for(Edge child: graph.getOrDefault(e.ch, new ArrayList<>())) {
int oldDistance = distance.get(child.ch);
int newDistance = distance.get(e.ch) + child.distance;
if(newDistance < oldDistance) {
pq.offer(new Edge(child.ch, newDistance));
distance.put(child.ch, newDistance);
}
}
}
}
Complexity
Let's say there are vertices and edges in a graph.
Time Complexity
Priority queue takes time for both poll and add operation, given that there are elements in the priority queue at any given time. We need to perform priority queue operations at most times as there are total edges.
The total time complexity will be as below:
Space Complexity
The algorithm uses space to store the distances of all vertices in a hashmap and stores the vertices in a priority queue as it iterates through the edges.
The total space complexity will be as below: