Selasa, 27 Desember 2016

Floyd Warshall dalam JAVA






package floydwarshall;
import java.util.Random;

public class Warshall {
int n=5;
int [][]dist=new int[n][n]; 


public static void main(String[]args) {
    Warshall xx=new Warshall();
    xx.play();

}

void play(){
    Warshall xx=new Warshall();
    int i, j,k;
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j){
dist[i][j]=xx.getRandomNumberInRange(1,9);
                        System.out.print(dist[i][j]+" ");
                }
                 System.out.println("");
        }
//xx.floyd_warshall();
//=================================================================
for (k = 0; k < n; ++k) {
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
/* If i and j are different nodes and if 
the paths between i and k and between
k and j exist, do */
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
/* See if you can't get a shorter path
between i and j by interspacing
k somewhere along the current
path */
if ((dist[i][k] + dist[k][j] < dist[i][j]) ||(dist[i][j] == 0))
dist[i][j] = dist[i][k] + dist[k][j];
}
        
        
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++");       
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j){
                        System.out.print(dist[i][j]+" ");
                }
                 System.out.println("");
        }

}


int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
       
int c=r.nextInt((max - min) + 1) + min;
        return c;
}

}


nilai 1000 diasumsikan nilai tak hingga(~) yang berarti arah ke lokasi tsb tidak tersedia....


Download Source Code

Tidak ada komentar:

Posting Komentar