Algoritmo de Floyd
Posted on May 12th, 2007 in Código, Java |
Implementación del Algoritmo de Floyd para hallar el camino mas corto de un grafo
JAVA:
-
/*
-
John Edgar Congote Calle http://jcongote.blogspot.com
-
*/
-
-
import java.io.BufferedReader;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
-
public final class Grafo {
-
-
private int nnodos;
-
-
private int nodos[][][];
-
-
private char nombres[];
-
-
Grafo(int n) {
-
this.nnodos = n;
-
this.nodos = new int[nnodos][nnodos][2];
-
this.nombres = new char[nnodos];
-
}
-
-
public void ingresarArco(int n1, int n2, int peso) {
-
this.nodos[n1][n2][0] = peso;
-
this.nodos[n2][n1][0] = peso;
-
this.nodos[n1][n2][1] = n1;
-
this.nodos[n2][n1][1] = n2;
-
}
-
-
public void ingresarNombre(int nodo, char letra) {
-
this.nombres[nodo] = letra;
-
}
-
-
public void calcular() {
-
int i, j, k;
-
for (i = 0; i <this.nnodos; i++) {
-
for (j = 0; j <this.nnodos; j++) {
-
for (k = 0; k <this.nnodos; k++) {
-
if (this.nodos[i][k][0] + this.nodos[k][j][0] <this.nodos[i][j][0]) {
-
this.nodos[i][j][0] = this.nodos[i][k][0]
-
+ this.nodos[k][j][0];
-
this.nodos[i][j][1] = k;
-
}
-
}
-
}
-
}
-
}
-
-
public int pesominimo(int org, int des) {
-
return this.nodos[org][des][0];
-
}
-
-
String cam;
-
if (org == des) {
-
cam = "->" + nombres[org];
-
} else {
-
cam = caminocorto(org, this.nodos[org][des][1]) + "->"
-
+ nombres[des];
-
}
-
return cam;
-
}
-
-
public char getNombre(int nodo) {
-
return this.nombres[nodo];
-
}
-
-
Grafo g;
-
-
String temp;
-
int res;
-
-
temp = br.readLine();
-
-
g = new Grafo(res);
-
-
for (int i = 0; i <res; i++) {
-
+ "]:n");
-
temp = br.readLine();
-
g.ingresarNombre(i, temp.charAt(0));
-
}
-
for (int i = 0; i <res; i++) {
-
for (int j = 0; j <res; j++) {
-
if (i <j) {
-
+ " esta conectado con el nodo " + g.getNombre(j)
-
+ " (s/n)n");
-
temp = br.readLine();
-
if (temp.charAt(0) == 's') {
-
int peso;
-
temp = br.readLine();
-
g.ingresarArco(i, j, peso);
-
} else {
-
g.ingresarArco(i, j, 10000);
-
}
-
}
-
}
-
}
-
-
g.calcular();
-
for (int i = 0; i <res; i++) {
-
for (int j = 0; j <res; j++) {
-
if (i> j) {
-
+ g.getNombre(i) + "-" + g.getNombre(j) + " es: n"
-
+ g.caminocorto(i, j) + " y su peso es: "
-
+ g.pesominimo(i, j));
-
}
-
}
-
}
-
}
-
}
Popularidad: 56%

