Saturday, June 10, 2017

Java implementation of Floyd -Warshall algorithm for all pair shortest path problem by dynamic programming approach


Source Code:


package dynamic;

public class Floyd {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Program is running...");


int [][]w={
{0,4,11},
{6,0,2},
{3,Integer.MAX_VALUE,0}
};



int [][] d=floydShortestPath(w,3);

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(d[i][j]+"   ");
}
System.out.println();
}


}

public static int [][] floydShortestPath(int [][] w,int n){

int [][] d=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){

d[i][j]=w[i][j];

}
}

for(int k=0;k<n;k++){

for(int i=0;i<n;i++){
for(int j=0;j<n;j++){

if(d[i][j]>d[i][k]+d[k][j])
d[i][j]=d[i][k]+d[k][j];
}
}
}


return d;
}



}

No comments:

Post a Comment

Socket Programming in Java

Source Code: client Side: package learning; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event....