strikero

leonard

Mar 13th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package floydalgorithm;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author rickygwapo
  13. */
  14. public class Algorithm {
  15.  
  16.  
  17. private static int size;
  18. private static int[][] matrix;
  19. private static final String infinity = "โˆž";
  20.  
  21. Algorithm(int size){
  22. matrix = new int[size][size];
  23. this.size = size;
  24.  
  25. }
  26.  
  27. public void floyd(int level ){
  28.  
  29. int sum;
  30. if(level==size){
  31.  
  32. return;
  33. }
  34. for(int row =0;row<size;row++){
  35. if(row == level){
  36. continue;
  37. }
  38. for(int col =0;col<size;col++){
  39. if(col==level){
  40. continue;
  41. }
  42. sum = matrix[row][level] + matrix[level][col];
  43. // System.out.println("matrix["+row+"]["+level+"] : "+matrix[row][level]);
  44. // System.out.println("matrix["+level+"]["+col+"] : "+matrix[level][col]);
  45. // System.out.println("sum "+sum);
  46. // System.out.println("to switch out "+matrix[row][col]);
  47. if(sum<matrix[row][col]){
  48. matrix[row][col] = sum;
  49. }
  50. }
  51. }
  52. printMatrix();
  53. floyd(level+1);
  54. }
  55.  
  56.  
  57. public void inputMatrix(){
  58. Scanner input = new Scanner(System.in);
  59.  
  60. System.out.println("type minus(-) sign if infinity");
  61. for(int row= 0; row<size;row++){
  62.  
  63. for(int col = 0; col<size;col++){
  64. boolean correct = true;
  65. if(row!=col){
  66. while(correct){
  67. System.out.print("Row "+(row+1)+" Column "+(col+1)+": ");
  68. String inputString = input.next();
  69. try{
  70. int num = Integer.parseInt(inputString);
  71. correct = false;
  72. matrix[row][col] = num;
  73. }catch(NumberFormatException e){
  74. if(inputString.equals("-")){
  75. matrix[row][col] = 999999;
  76. correct = false;
  77. }
  78. }
  79. }
  80. }else{
  81. System.out.println("Row "+(row+1)+" Column "+(col+1)+": 0");
  82. matrix[row][col] = 0;
  83. }
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. public void printMatrix(){
  91.  
  92. System.out.println("");
  93. System.out.print("-------------------------------");
  94. for(int row= 0; row<size;row++){
  95. System.out.println("");
  96. for(int col = 0; col<size;col++){
  97. if(matrix[row][col]>999099){
  98. System.out.print(infinity+" \t");
  99. }else{
  100. System.out.print(matrix[row][col]+" \t");
  101. }
  102. }
  103.  
  104. }
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111. }
Add Comment
Please, Sign In to add comment