Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package floydalgorithm;
- import java.util.Scanner;
- /**
- *
- * @author rickygwapo
- */
- public class Algorithm {
- private static int size;
- private static int[][] matrix;
- private static final String infinity = "โ";
- Algorithm(int size){
- matrix = new int[size][size];
- this.size = size;
- }
- public void floyd(int level ){
- int sum;
- if(level==size){
- return;
- }
- for(int row =0;row<size;row++){
- if(row == level){
- continue;
- }
- for(int col =0;col<size;col++){
- if(col==level){
- continue;
- }
- sum = matrix[row][level] + matrix[level][col];
- // System.out.println("matrix["+row+"]["+level+"] : "+matrix[row][level]);
- // System.out.println("matrix["+level+"]["+col+"] : "+matrix[level][col]);
- // System.out.println("sum "+sum);
- // System.out.println("to switch out "+matrix[row][col]);
- if(sum<matrix[row][col]){
- matrix[row][col] = sum;
- }
- }
- }
- printMatrix();
- floyd(level+1);
- }
- public void inputMatrix(){
- Scanner input = new Scanner(System.in);
- System.out.println("type minus(-) sign if infinity");
- for(int row= 0; row<size;row++){
- for(int col = 0; col<size;col++){
- boolean correct = true;
- if(row!=col){
- while(correct){
- System.out.print("Row "+(row+1)+" Column "+(col+1)+": ");
- String inputString = input.next();
- try{
- int num = Integer.parseInt(inputString);
- correct = false;
- matrix[row][col] = num;
- }catch(NumberFormatException e){
- if(inputString.equals("-")){
- matrix[row][col] = 999999;
- correct = false;
- }
- }
- }
- }else{
- System.out.println("Row "+(row+1)+" Column "+(col+1)+": 0");
- matrix[row][col] = 0;
- }
- }
- }
- }
- public void printMatrix(){
- System.out.println("");
- System.out.print("-------------------------------");
- for(int row= 0; row<size;row++){
- System.out.println("");
- for(int col = 0; col<size;col++){
- if(matrix[row][col]>999099){
- System.out.print(infinity+" \t");
- }else{
- System.out.print(matrix[row][col]+" \t");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment