package javaCodes;
class allPairShortest{
void shortest_path(int g[][])
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
System.out.print(g[i][j]+" ");
}
System.out.println("\n");
}
System.out.println("\n");
int dis[][] = new int[4][4];
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
dis[i][j] = g[i][j];
}
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
System.out.print(dis[i][j]+" ");
}
System.out.println("\n");
}
System.out.println("\n");
for(int k=0; k<4; k++)
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(dis[i][j] > dis[i][k]+dis[k][j])
{
dis[i][j] = dis[i][k]+dis[k][j];
System.out.print(dis[i][j]);
}
}
}
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
System.out.print(dis[i][j]+" ");
}
System.out.println("\n");
}
}
}
public class floyd_warshal {
private static final int INF = 99999;
public static void main(String args[])
{
allPairShortest path = new allPairShortest();
int graph1[][] = { {0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0}
};
path.shortest_path(graph1);
}
}
No comments:
Post a Comment