Dijkstra's shortest path algorithm in Java - Tutorial

Version 1.1

02.11.2009

Dijkstra's Shortest Path Algorithm in Java

Dijkstra's Algorithms describes how to find the shortest path from one node to another node in a directed weighted graph. This article presents a Java implementation of this algorithm.

1. The shortest path problem
1.1. Shortest path
1.2. Graph
1.3. Typical graph problems
2. Dijkstra's algorithm
2.1. High level description
2.2. Algorithms Description
3. Model
4. algorithm
4.1. Implementation
4.2. Test
5. Support this website
5.1. Thank you
5.2. Questions and Discussion

1. The shortest path problem

1.1. Shortest path

Finding the shortest path in a network is a commonly encountered problem. For example you want to reach a target in the real world via the shortest path or in a computer network a network package should be efficiently routed through the network.

This tutorial describes the problem modeled as graph and the Dijkstra algorithm to solve the problem.

1.2. Graph

A graph is made out of nodes and directed edges which defines a connection from one node to another node.

A node (or vertex) is a discrete position in a graph. Edges can be directed an undirected. Edges have an associated distance (also called costs or weight). The distance between two nodes a and b is labeled as [a,b].

The mathematical description for graphs is `G= {V,E}`, meaning that a graph is defined by a set of vertexes (V) and a collection of edges.

The order of a graph is the number of nodes. The size of a graph is the number of edges.

1.3. Typical graph problems

Typical graph problems are described in the following list.

• finding the shortest path from a specific node to another node

• finding the maximum possible flow through a network where each edges has a pre-defined maximum capacity (maximum-flow minimum-cut problem)

The following will focus on finding the shortest path from one node to another node in a graph.

2. Dijkstra's algorithm

2.1. High level description

The Dijkstra Algorithm finds the shortest path from a source to all destination in a directed graph (single source shortest path problem). During this process it will also determine a spanning tree for the graph.

2.2. Algorithms Description

The idea of Dijkstra is simple.

Dijkstra partitions all nodes into two distinct sets. Unsettled and settled. Initially all nodes are in the unsettled sets, e.g. they must be still evaluated. A node is moved to the settled set if a shortest path from the source to this node has been found.

Initially the distance of each node to the source is set to a very high value.

First only the source is in the set of unsettledNodes.

The algorithms runs until the unsettledNodes are empty. In earch iteration it selects the node with the lowest distance from the source out the unsettled nodes. If reads all edges which are outgoing from the source and evaluates for each destination node in these edges which is not yet settled if the known distance from the source to this node can be reduced if the selected edge is used. If this can be done then the distance is updated and the node is added to the nodes which need evaluation.

In pseudocode the algorithm can be described as follows. Please note that Dijkstra also determines the pre-successor of each node on its way to the source. I leave that out of the pseudo code to simplify it.

```Foreach node set distance[node] = HIGH
SettledNodes = empty
UnSettledNodes = empty

distance[sourceNode]= 0

while (UnSettledNodes is not empty) {
evaluationNode = getNodeWithLowestDistance(UnSettledNodes)
remove evaluationNode from UnSettledNodes
evaluatedNeighbors(evaluationNode)
}

getNodeWithLowestDistance(UnSettledNodes){
find the node with the lowest distance in UnSettledNodes and return it
}

evaluatedNeighbors(evaluationNode){
Foreach destinationNode which can be reached via an edge from evaluationNode AND which is not in SettledNodes {
edgeDistance = getDistance(edge(evaluationNode, destinationNode))
newDistance = distance[evaluationNode] + edgeDistance
if (distance[destinationNode]  > newDistance) {
distance[destinationNode]  = newDistance
}
}
} ```

3. Model

A graph consists of vertices and edges. These are represented by the following model.

```package de.vogella.algorithms.dijkstra.model;

public class Vertex {
final private String id;
final private String name;

public Vertex(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}

public String getName() {
return name;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

@Override
public String toString() {
return name;
}

} ```

A edge has a source and a destination.

```package de.vogella.algorithms.dijkstra.model;

public class Edge  {
private final String id;
private final Vertex source;
private final Vertex destination;
private final int weight;

public Edge(String id, Vertex source, Vertex destination, int weight) {
this.id = id;
this.source = source;
this.destination = destination;
this.weight = weight;
}

public String getId() {
return id;
}
public Vertex getDestination() {
return destination;
}

public Vertex getSource() {
return source;
}
public int getWeight() {
return weight;
}

@Override
public String toString() {
return source + " " + destination;
}

} ```

Both represent a graph.

```package de.vogella.algorithms.dijkstra.model;

import java.util.List;

public class Graph {
private final List<Vertex> vertexes;
private final List<Edge> edges;

public Graph(List<Vertex> vertexes, List<Edge> edges) {
this.vertexes = vertexes;
this.edges = edges;
}

public List<Vertex> getVertexes() {
return vertexes;
}

public List<Edge> getEdges() {
return edges;
}

} ```

4. algorithm

4.1. Implementation

The following is a simple implementation of Dijkstra's algorithm. It does not use any performance optimization (e.g. by using a PriorityQueue for the UnSettledNodes of does not cache the result of the target evaluation of the edges) to make the algorihms as simple as possible.

```package de.vogella.algorithms.dijkstra.engine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.vogella.algorithms.dijkstra.model.Edge;
import de.vogella.algorithms.dijkstra.model.Graph;
import de.vogella.algorithms.dijkstra.model.Vertex;

public class DijkstraAlgorithm {

private final List<Vertex> nodes;
private final List<Edge> edges;
private Set<Vertex> settledNodes;
private Set<Vertex> unSettledNodes;
private Map<Vertex, Vertex> predecessors;
private Map<Vertex, Integer> distance;

public DijkstraAlgorithm(Graph graph) {
// create a copy of the array so that we can operate on this array
this.nodes = new ArrayList<Vertex>(graph.getVertexes());
this.edges = new ArrayList<Edge>(graph.getEdges());
}

public void execute(Vertex source) {
settledNodes = new HashSet<Vertex>();
unSettledNodes = new HashSet<Vertex>();
distance = new HashMap<Vertex, Integer>();
predecessors = new HashMap<Vertex, Vertex>();
distance.put(source, 0);
while (unSettledNodes.size() > 0) {
Vertex node = getMinimum(unSettledNodes);
unSettledNodes.remove(node);
findMinimalDistances(node);
}
}

private void findMinimalDistances(Vertex node) {
for (Vertex target : adjacentNodes) {
if (getShortestDistance(target) > getShortestDistance(node)
+ getDistance(node, target)) {
distance.put(target, getShortestDistance(node)
+ getDistance(node, target));
predecessors.put(target, node);
}
}

}

private int getDistance(Vertex node, Vertex target) {
for (Edge edge : edges) {
if (edge.getSource().equals(node)
&& edge.getDestination().equals(target)) {
return edge.getWeight();
}
}
throw new RuntimeException("Should not happen");
}

private List<Vertex> getNeighbors(Vertex node) {
List<Vertex> neighbors = new ArrayList<Vertex>();
for (Edge edge : edges) {
if (edge.getSource().equals(node)
&& !isSettled(edge.getDestination())) {
}
}
return neighbors;
}

private Vertex getMinimum(Set<Vertex> vertexes) {
Vertex minimum = null;
for (Vertex vertex : vertexes) {
if (minimum == null) {
minimum = vertex;
} else {
if (getShortestDistance(vertex) < getShortestDistance(minimum)) {
minimum = vertex;
}
}
}
return minimum;
}

private boolean isSettled(Vertex vertex) {
return settledNodes.contains(vertex);
}

private int getShortestDistance(Vertex destination) {
Integer d = distance.get(destination);
if (d == null) {
return Integer.MAX_VALUE;
} else {
return d;
}
}

/*
* This method returns the path from the source to the selected target and
* NULL if no path exists
*/
Vertex step = target;
// check if a path exists
if (predecessors.get(step) == null) {
return null;
}
while (predecessors.get(step) != null) {
step = predecessors.get(step);
}
// Put it into the correct order
Collections.reverse(path);
return path;
}

} ```

4.2. Test

The following is a small JUnit Test to validate the correctness of the algorithm.

```package de.vogella.algorithms.dijkstra.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import de.vogella.algorithms.dijkstra.engine.DijkstraAlgorithm;
import de.vogella.algorithms.dijkstra.model.Edge;
import de.vogella.algorithms.dijkstra.model.Graph;
import de.vogella.algorithms.dijkstra.model.Vertex;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class TestDijkstraAlgorithm {

private List<Vertex> nodes;
private List<Edge> edges;

@Test
public void testExcute() {
nodes = new ArrayList<Vertex>();
edges = new ArrayList<Edge>();
for (int i = 0; i < 11; i++) {
Vertex location = new Vertex("Node_" + i, "Node_" + i);
}

// Lets check from location Loc_1 to Loc_10
Graph graph = new Graph(nodes, edges);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(graph);
dijkstra.execute(nodes.get(0));

assertNotNull(path);
assertTrue(path.size() > 0);

for (Vertex vertex : path) {
System.out.println(vertex);
}

}

private void addLane(String laneId, int sourceLocNo, int destLocNo,
int duration) {
Edge lane = new Edge(laneId,nodes.get(sourceLocNo), nodes.get(destLocNo), duration);
}
} ```

5. Support this website

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

5.1. Thank you

Please consider a contribution if this article helped you. It will help to maintain our content and our Open Source activities.

5.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.