# Decimal Format Java

Added on - Sep 2019

• 7

Pages

• 3099

Words

• 381

Views

• Save

Share

Trusted by 2+ million users,
1000+ happy students everyday
Showing pages 1 to 3 of 7 pages
importjava.awt.*;importjava.text.DecimalFormat;importjava.util.*;importjava.lang.*;importjava.io.*;importjava.util.List;classShortestPath {finalstaticintINF= 99999,V= 6;inti,j,k;String[]str=null;DecimalFormatdf=newDecimalFormat("#.##");ListfloydWarshall(doublegraph[][]) {doublehop[][] =newdouble[V][V];doubledistance[][] =newdouble[V][V];Stringpath[][] =newString[V][V];// String path[][] = {// {"1","1","1","1","1","1"},// {1,"1","1","1","1","1"},// {1,1,0,1,1,1},// {1,1,1,0,1,1},// {1,1,1,1,0,1},// {1,1,1,1,1,0}//// };/* Initialize the solution matrix same as input graph matrix.Or we can say the initial values of shortest distancesare based on shortest paths considering no intermediatevertex. */for(i= 0;i<V;i++)for(j= 0;j<V;j++){distance[i][j] =graph[i][j];if(i!=j){hop[i][j]=1;path[i][j] ="("+ (i+ 1) +","+ (j+ 1) +")";}elsepath[i][j]=""+(j+1)+"";}/* Add all vertices one by one to the set of intermediatevertices.---> Before start of a iteration, we have shortestdistances between all pairs of vertices such thatthe shortest distances consider only the vertices inset {0, 1, 2, .. k-1} as intermediate vertices.----> After the end of a iteration, vertex no. k is addedto the set of intermediate vertices and the setbecomes {0, 1, 2, .. k} */
for(k= 0;k<V;k++) {// Pick all vertices as source one by onefor(i= 0;i<V;i++) {// Pick all vertices as destination for the// above picked sourcefor(j= 0;j<V;j++) {if(path[i][j] ==null&&i!=j) {path[i][j] ="("+ (i+ 1) +","+ (j+ 1) +")";//hop[i][j]=1;//path[i][j] = "("+(i+1)+","+(j+1)+")";}// If vertex k is on the shortest path from// i to j, then update the value ofdist[i][j]if(distance[i][k] +distance[k][j] <distance[i][j]) {distance[i][j] =distance[i][k] +distance[k][j];//hop[i][j]++;// if (path[i][j]!= null)path[i][j] =path[i][k] +path[k][j];hop[i][j] =hop[i][k] +hop[k][j];/*if (graph[i][k]!= 99999) Loud[i][k]=Loud[i][k]+Flow[i][j];if (graph[k][j]!= 99999) Loud[k][j]=Loud[k][j]+Flow[i][j];*///if( graph[i][j]!= 99999) Loud[i][k]=Flow[i][j];//path[i][j] = "("+(i+1)+","+(k+1)+","+(j+1)+")"+path[i][j];// else path[i][j] = "("+(i+1)+","+(k+1)+","+(j+1)+")";}else{if(hop[i][j] == 0 &&i!=j) {//hop[i][j]=1;//path[i][j] = "("+(i+1)+","+(j+1)+")";}}}}}List<Object>list=newArrayList<Object>();for(i= 0;i<V;i++) {for(j= 0;j<V;j++) {distance[i][j] = (double)Math.round(distance[i][j] * 1000d) / 1000d ;}}list.add(distance);list.add(hop);list.add(path);returnlist;}ListLoadandGFunction(doublegraph[][],doubleFlow[][],double[][]Capacity,String[][]path) {doubleLoad[][] =newdouble[V][V];doubleG[][]=newdouble[V][V];
doublefinal_path[][] =newdouble[V][V];for(i= 0;i<V;i++) {for(j= 0;j<V;j++) {if(graph[i][j] ==INF)Load[i][j] =INF;elseif(i==j) {Load[i][j] = 0;}elseif(!path[i][j].equals("("+ (i+ 1) +","+ (j+ 1) +")")) {//System.out.print(i + 1 + "\t" + (j + 1) + "\t" + path[i][j] + "\n");Load[i][j] = 0;}elseif(path[i][j].equals("("+ (i+ 1) +","+ (j+ 1) +")")) {for(k= 0;k<V;k++) {for(intl= 0;l<V;l++) {if(path[k][l] !=null)if(path[k][l].contains(path[i][j])) {//System.out.print("i new: " + i + " j new : " + j + '\t' + "knew: " + k + " l new: " + l + '\n');Load[i][j] =Load[i][j] +Flow[k][l];//System.out.print("i new: "+ i + " j new : " + j + '\t' + "knew: "+ k + " l new: " + l + '\n');}}}}}}for(i= 0;i<V;i++) {for(j= 0;j<V;j++) {if(Load[i][j] <=Capacity[i][j]) {if(graph[i][j] ==INF)G[i][j] =INF;elseG[i][j] = ((Capacity[i][j] + 1) / (Capacity[i][j] + 1 -Load[i][j])) *graph[i][j];}elseG[i][j] =INF;//System.out.print("i "+ " j " + G[i][j]+'\n');}}for(i= 0;i<V;i++) {for(j= 0;j<V;j++) {if(i!=j) {str=path[i][j].replaceAll("[^-?0-9]+"," ").split(" ");ArrayList<Integer>integerList=newArrayList<Integer>();floatvalue= 0; 