Skip to content

Instantly share code, notes, and snippets.

@le-doude
Created August 5, 2013 12:33
Show Gist options
  • Save le-doude/6155579 to your computer and use it in GitHub Desktop.
Save le-doude/6155579 to your computer and use it in GitHub Desktop.
Self study: self contained Dijkstra implementation
import java.util.*;
/**
* Created with IntelliJ IDEA.
* Author: Edouard
*/
public class Dijkstra {
public static class Vertex implements Comparable<Vertex> {
private final String label;
private List<Edge> edges = new ArrayList<Edge>();
private int minDistance = Integer.MAX_VALUE;
private Vertex previous = null;
private Vertex(String label) {
this.label = label;
}
public static Vertex createVertex(String label) {
return new Vertex(label);
}
public String getLabel() {
return label;
}
public int getMinDistance() {
return minDistance;
}
public void setMinDistance(int minDistance) {
this.minDistance = minDistance;
}
public Vertex getPrevious() {
return previous;
}
public void setPrevious(Vertex previous) {
this.previous = previous;
}
public List<Edge> getEdges() {
return edges;
}
public void setEdges(List<Edge> edges) {
this.edges = edges;
}
@Override
public int compareTo(Vertex o) {
return Integer.compare(this.getMinDistance(), o.getMinDistance());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("v{");
return sb.append('\'').append(label).append('\'').append('}').toString();
}
}
public static class Edge {
private final Vertex v;
private final Vertex w;
private final int weigh;
private final boolean directed;
private Edge(Vertex v, Vertex w, int weigh, boolean directed) {
this.directed = directed;
assert v != null && w != null && weigh > 0;
this.v = v;
this.w = w;
this.weigh = weigh;
v.getEdges().add(this);
if (!directed) {
w.getEdges().add(this);
}
}
public static Edge createEdge(Vertex v, Vertex w, int weigh) {
return new Edge(v, w, weigh, false);
}
public static Edge createDirectedEdge(Vertex from, Vertex to, int weigh) {
return new Edge(from, to, weigh, false);
}
public Vertex getV() {
return v;
}
public Vertex getW() {
return w;
}
public int getWeigh() {
return weigh;
}
}
public static void compute(Vertex start) {
start.minDistance = 0;
PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>();
queue.add(start);
Vertex current;
Vertex target;
while (!queue.isEmpty()) {
current = queue.poll();
for (Edge e : current.getEdges()) {
target = (current == e.getW() ? e.getV() : e.getW());
int weighsSoFar = current.getMinDistance() + e.getWeigh();
if (weighsSoFar < target.getMinDistance()) {
queue.remove(target);
target.setMinDistance(weighsSoFar);
target.setPrevious(current);
queue.add(target); //remove and add so that the position is updated in thre priority queue;
}
}
}
}
public static List<Vertex> path(Vertex to) {
List<Vertex> l = new LinkedList<Vertex>();
for (Vertex v = to; v != null; v = v.getPrevious()) {
l.add(v);
}
Collections.reverse(l);
return l;
}
public static void wipe(Vertex[] vs){
for (Vertex v : vs) {
v.setMinDistance(Integer.MAX_VALUE);
v.setPrevious(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment