We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Hola, podrías pasarme el codigo entero por favor estoy estudiando para unas opos y me iria muy bien..

Anonymous • 2 years ago

Yo hice un código en Java, usando matrices, si te interesa:

public class AlgoritmoDijkstra {

public static void caminosMinimos(int[][] grafo, int verticeInicio) {
int row = grafo.length;
if(fueraDeRango(grafo, verticeInicio))
throw new ArrayIndexOutOfBoundsException(
"Valor fuera de rango o Nodo de inicio no tiene vértice de salida");
List<integer> distancias = new LinkedList<>();
List<string> caminos = new LinkedList<>();
List<integer> siguiente = new LinkedList<>();
for(int i = 0; i < row; i++){
distancias.add(2147483647);
caminos.add("");
}
distancias.set(verticeInicio, 0);
caminos.set(verticeInicio, "" + verticeInicio);
int col;
int suma;
int aux = row*2;
for(int z = 0; z < aux; z++) {
col = grafo[verticeInicio].length;
for(int i = verticeInicio; i == verticeInicio; i++)
for(int j = 0; j < col; j++)
if(grafo[i][j] != 0){
if(grafo[i][j] < 0)
throw new ArithmeticException("El grafo tiene valores negativos");
suma = distancias.get(verticeInicio) + grafo[i][j];
if(suma < distancias.get(j)) {
distancias.set(j, suma);
caminos.set(j, caminos.get(verticeInicio) + "->" + j);
}
siguiente.add(j);
}
verticeInicio = siguiente.remove(0);
}
System.out.println("distancia: " + distancias + "\ncamino: " + caminos);
}

private static boolean tieneSalientes(int[][] grafo, int row, int nodoInicio){
int col = grafo[nodoInicio].length;
for(int i = nodoInicio; i == nodoInicio; i++)
for(int j = 0; j < row; j++)
if(grafo[i][j] != 0 && i != j)
return true;
return false;
}

private static boolean fueraDeRango(int[][] grafo, int verticeInicio){
int row = grafo.length;
return verticeInicio < 0 || verticeInicio >= row
|| !tieneSalientes(grafo, row, verticeInicio);
}

}