Skip to content

Instantly share code, notes, and snippets.

@caiquecastro
Last active August 29, 2015 14:07
Show Gist options
  • Save caiquecastro/304b98d9b9db374e0dc9 to your computer and use it in GitHub Desktop.
Save caiquecastro/304b98d9b9db374e0dc9 to your computer and use it in GitHub Desktop.
Dijkstra Algorithm to get the shortest path to the target
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* Classe que representa um vértice do grafo,
* implementa uma interface para permitir comparações
*/
class Vertice implements Comparable<Vertice> {
public final String nome;
public Aresta[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertice anterior;
public Vertice(String vNome) {
this.nome = vNome;
}
/**
* Permite "converter" cada objeto da classe em um nome amigável
* @return texto representante do objeto
*/
public String toString() {
return this.nome;
}
public int compareTo(Vertice other) {
return Double.compare(minDistance, other.minDistance);
}
}
class Aresta {
public final Vertice objetivo;
public final double peso;
public Aresta(Vertice eTarget, double eWeight) {
this.objetivo = eTarget;
this.peso = eWeight;
}
}
public class Dijkstra {
public static void encontraDistancias(Vertice origem) {
origem.minDistance = 0.0;
PriorityQueue<Vertice> vertexQueue = new PriorityQueue<Vertice>();
vertexQueue.add(origem);
while (!vertexQueue.isEmpty()) {
Vertice u = vertexQueue.poll();
for (Aresta e : u.adjacencies) {
Vertice v = e.objetivo;
double peso = e.peso;
double distanceThroughU = u.minDistance + peso;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.anterior = u;
vertexQueue.add(v);
}
}
}
}
public static List<Vertice> tracarRota(Vertice target) {
List<Vertice> caminho = new ArrayList<Vertice>();
for (Vertice vertice = target; vertice != null; vertice = vertice.anterior) {
caminho.add(vertice);
}
Collections.reverse(caminho);
return caminho;
}
public static void main(String[] args) {
Vertice[] v = new Vertice[5];
v[0] = new Vertice("Redvile");
v[1] = new Vertice("Blueville");
v[2] = new Vertice("Greenville");
v[3] = new Vertice("Orangeville");
v[4] = new Vertice("Purpleville");
v[0].adjacencies = new Aresta[] {
new Aresta(v[1], 5),
new Aresta(v[2], 10),
new Aresta(v[3], 8)
};
v[1].adjacencies = new Aresta[] {
new Aresta(v[0], 5),
new Aresta(v[2], 3),
new Aresta(v[4], 7)
};
v[2].adjacencies = new Aresta[] {
new Aresta(v[0], 10),
new Aresta(v[1], 3)
};
v[3].adjacencies = new Aresta[] {
new Aresta(v[0], 8),
new Aresta(v[4], 2)
};
v[4].adjacencies = new Aresta[] {
new Aresta(v[1], 7),
new Aresta(v[3], 2)
};
encontraDistancias(v[0]);
System.out.println("Distancia até v[1] " + v[1].minDistance);
List<Vertice> caminho = tracarRota(v[1]);
System.out.println("Caminho: " + caminho);
/*for (Vertice v : vertices) {
System.out.println("Distância até " + v + ": " + v.minDistance);
List<Vertice> caminho = encontraMenorCaminho(v);
System.out.println("Caminho: " + caminho);
}*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment