Ask a question to Desklib · AI bot

Ask NowBETA

Decimal Format Java

Added on -2019-09-13

| 7 pages
| 3099 words
| 419 views

Trusted by 2+ million users,
1000+ happy students everyday

import java.awt.*;import java.text.DecimalFormat;import java.util.*;importjava.lang.*;import java.io.*;import java.util.List;class ShortestPath {finalstaticintINF = 99999, V = 6;inti, j, k; String[] str = null; DecimalFormat df = new DecimalFormat("#.##");List floydWarshall(doublegraph[][]) {doublehop[][] = newdouble[V][V];doubledistance[][] = newdouble[V][V]; String path[][] = new String[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 distances are based on shortest paths considering no intermediate vertex. */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 intermediate vertices. ---> Before start of a iteration, we have shortest distances between all pairs of vertices such that the shortest distances consider only the vertices in set {0, 1, 2, .. k-1} as intermediate vertices. ----> After the end of a iteration, vertex no. k is added to the set of intermediate vertices and the set becomes {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 of dist[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 = new ArrayList<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; }List LoadandGFunction(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' + "k new: " + k + " l new: " + l + '\n');Load[i][j] = Load[i][j] + Flow[k][l];//System.out.print("i new: "+ i + " j new : " + j + '\t' + "k new: "+ 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 = new ArrayList<Integer>();floatvalue = 0;

Found this document preview useful?

You are reading a preview
Upload your documents to download
or
Become a Desklib member to get accesss

Students who viewed this