Advertisement
jasonpogi1669

Travelling Salesman Problem with Row and Column Minimization, and Penalty Rule using Java

Jun 10th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.   static int cost = 0;
  6.   static String path = "";
  7.  
  8.   // NOTE: *Use only one test case per running of program because they have the same identifier*
  9.   // TEST CASE 1: (Output should 30 from M3S2) (UNCOMMENT below to enable)
  10.   static char[] letters = {'A', 'B', 'C', 'D', 'E'};
  11.   static int val[][] = {{Integer.MAX_VALUE, 7, 6, 8, 4},
  12.                         {7, Integer.MAX_VALUE, 8, 5, 6},
  13.                         {6, 8, Integer.MAX_VALUE, 9, 7},
  14.                         {8, 5, 9, Integer.MAX_VALUE, 8},
  15.                         {4, 6, 7, 8, Integer.MAX_VALUE}};
  16.  
  17.   //     TEST CASE 2: (Output should 17 from TSP EXER1) (UNCOMMENT below to enable)
  18. //  static char[] letters = {'A', 'B', 'C', 'D', 'E'};
  19. //  static int val[][] = {{Integer.MAX_VALUE, 3, 6, 2, 3},
  20. //                        {3, Integer.MAX_VALUE, 5, 2, 3},
  21. //                        {6, 5, Integer.MAX_VALUE, 6, 5},
  22. //                        {2, 2, 6, Integer.MAX_VALUE, 6},
  23. //                        {3, 3, 5, 6, Integer.MAX_VALUE}};
  24.  
  25. //      TEST CASE 3: (Output should be 110 TSP EXER2) (UNCOMMENT below to enable)
  26. //  static char[] letters = {'A', 'B', 'C', 'D', 'E', 'F'};
  27. //  static int val[][]= {{Integer.MAX_VALUE, 20, 23, 27, 29, 34},
  28. //                       {21, Integer.MAX_VALUE, 19, 26, 31, 24},
  29. //                       {26, 28, Integer.MAX_VALUE, 15, 36, 26},
  30. //                       {25, 26, 25, Integer.MAX_VALUE, 23, 28},
  31. //                       {23, 40, 13, 31, Integer.MAX_VALUE, 10},
  32. //                       {27, 18, 12, 35, 16, Integer.MAX_VALUE}};
  33.                        
  34.   private boolean Check(int[][] table) {
  35.     for (int i = 0; i < (int) table.length; i++) {
  36.       for (int j = 0; j < (int) table[i].length; j++) {
  37.         if (table[i][j] != 0 && table[i][j] != Integer.MAX_VALUE) {
  38.           return false;
  39.         }
  40.       }
  41.     }
  42.     return true;
  43.   }
  44.  
  45.   private boolean Zeroes(Vector<Integer> temp) {
  46.     for (int i = 0; i < (int) temp.size(); i++) {
  47.       if (temp.elementAt(i) == 0) {
  48.         return true;
  49.       }
  50.     }
  51.     return false;
  52.   }
  53.  
  54.   private int[][] RowMinimization(int[][] table) {
  55.     for (int i = 0; i < (int) table.length; i++) {
  56.       Vector<Integer> temp = new Vector<Integer>();
  57.       for (int j = 0; j < (int) table.length; j++) {
  58.         temp.add(table[i][j]);
  59.       }
  60.       if (Zeroes(temp)) {
  61.         continue;
  62.       }
  63.       int minimum = Integer.MAX_VALUE;
  64.       for (int j = 0; j < (int) table[i].length; j++) {
  65.         minimum = Math.min(minimum, table[i][j]);
  66.       }
  67.       for (int j = 0; j < (int) table[i].length; j++) {
  68.         if (table[i][j] != Integer.MAX_VALUE) {
  69.           table[i][j] = table[i][j] - minimum;
  70.         }
  71.       }
  72.     }
  73.     return table;
  74.   }
  75.  
  76.   private int[][] ColumnMinimization(int[][] table) {
  77.     for (int i = 0; i < (int) table.length; i++) {
  78.       Vector<Integer> temp = new Vector<Integer>();
  79.       for (int j = 0; j < (int) table.length; j++) {
  80.         temp.add(table[j][i]);
  81.       }
  82.       if (Zeroes(temp)) {
  83.         continue;
  84.       }
  85.       int minimum = Integer.MAX_VALUE;
  86.       for (int j = 0; j < (int) table[i].length; j++) {
  87.         minimum = Math.min(minimum, table[j][i]);
  88.       }
  89.       for (int j = 0; j < (int) table[i].length; j++) {
  90.         if (table[j][i] != Integer.MAX_VALUE) {
  91.           table[j][i] = table[j][i] - minimum;
  92.         }
  93.       }
  94.     }
  95.     return table;
  96.   }
  97.  
  98.   private int[][] Penalty(int[][] table) {
  99.     int[][] temp = new int[(int) table.length][(int) table.length];
  100.     for (int i = 0; i < (int) table.length; i++) {
  101.       for (int j = 0; j < (int) table[i].length; j++) {
  102.         temp[i][j] = table[i][j];
  103.       }
  104.     }
  105.     int key = Integer.MIN_VALUE;
  106.     for (int i = 0; i < (int) temp.length; i++) {
  107.       for (int j = 0; j < (int) temp[i].length; j++) {
  108.         if (table[i][j] == 0) {
  109.           int row_minimum = Integer.MAX_VALUE;
  110.           for (int k = 0; k < (int) temp[i].length; k++) {
  111.             if (k != j) {
  112.               row_minimum = Math.min(row_minimum, table[i][k]);
  113.             }
  114.           }
  115.           int column_minimum = Integer.MAX_VALUE;
  116.           for (int k = 0; k < (int) temp[i].length; k++) {
  117.             if (k != i) {
  118.               column_minimum = Math.min(column_minimum, table[k][j]);
  119.             }
  120.           }
  121.           temp[i][j] = row_minimum + column_minimum;
  122.           key = Math.max(key, temp[i][j]);
  123.         }
  124.       }
  125.     }
  126.     int row_index = -1;
  127.     int column_index = -1;
  128.     for (int i = 0; i < (int) temp.length; i++) {
  129.       for (int j = 0; j < (int) temp[i].length; j++) {
  130.         if (table[i][j] == 0 && temp[i][j] == key) {
  131.           row_index = i;
  132.           column_index = j;
  133.           cost += val[i][j];
  134.           path += String.valueOf(letters[i]);
  135.           path += String.valueOf(letters[j]);
  136.           break;
  137.         }
  138.       }
  139.       if (row_index != -1 && column_index != -1) {
  140.         break;
  141.       }
  142.     }
  143.     table[column_index][row_index] = Integer.MAX_VALUE;
  144.     for (int i = 0; i < (int) table.length; i++) {
  145.       for (int j = 0; j < (int) table[i].length; j++) {
  146.         if (i == row_index || j == column_index) {
  147.           table[i][j] = Integer.MAX_VALUE;
  148.         }
  149.       }
  150.     }
  151.     return table;
  152.   }
  153.  
  154.   public static void main(String[] args) {
  155.       Main object = new Main();
  156.       int table[][] = new int[(int) val.length][(int) val.length];
  157.       for (int i = 0; i < (int) table.length; i++) {
  158.         for (int j = 0; j < (int) table[i].length; j++) {
  159.           table[i][j] = val[i][j];
  160.         }
  161.       }
  162.       while (true) {
  163.         if (object.Check(table)) {
  164.             break;
  165.         }
  166.         table = object.RowMinimization(table);
  167.         table = object.ColumnMinimization(table);
  168.         table = object.Penalty(table);
  169.       }
  170.       System.out.println("Table Status:");
  171.       System.out.print(" ");
  172.       for (int i = 0; i < (int) letters.length; i++) {
  173.         System.out.print(" " + letters[i]);
  174.       }
  175.       System.out.println("");
  176.       for (int i = 0; i < (int) table.length; i++) {
  177.         System.out.print(letters[i] + " ");
  178.         for (int j = 0; j < (int) table[i].length; j++) {
  179.           System.out.print((table[i][j] == Integer.MAX_VALUE ? "-" : table[i][j]) + " ");
  180.         }
  181.         System.out.println("");
  182.       }
  183.       for (int i = 0; i < (int) table.length; i++) {
  184.         int zeroes = 0;
  185.         for (int j = 0; j < (int) table.length; j++) {
  186.           if (table[i][j] == 0) {
  187.             zeroes++;
  188.           }
  189.         }
  190.         int minimum_per_row = Integer.MAX_VALUE;
  191.         int row_index = -1;
  192.         int column_index = -1;
  193.         for (int j = 0; j < (int) table.length; j++) {
  194.           if (table[i][j] == 0) {
  195.             boolean checker = false;
  196.             if (j == 0) {
  197.               for (int k = 0; k < (int) table[i].length; k++) {
  198.                 if (table[k][j] == 0 && k != i) {
  199.                   checker = true;
  200.                   break;
  201.                 }
  202.               }
  203.             }
  204.             if (checker && zeroes > 1) {
  205.               continue;
  206.             }
  207.             if (val[i][j] < minimum_per_row) {
  208.               minimum_per_row = val[i][j];
  209.               row_index = i;
  210.               column_index = j;
  211.             }
  212.           }
  213.         }
  214.         if (minimum_per_row != Integer.MAX_VALUE) {
  215.           String connected = String.valueOf(letters[row_index]) + String.valueOf(letters[column_index]);
  216.           path += connected;
  217.           cost += minimum_per_row;
  218.         }
  219.       }
  220.       Vector<String> separate = new Vector<String>();
  221.       for (int i = 0; i < (int) path.length(); i += 2) {
  222.         String temp = String.valueOf(path.charAt(i)) + String.valueOf(path.charAt(i + 1));
  223.         separate.add(temp);
  224.       }
  225.       System.out.println("\nFinal Path:");
  226.       String last_path = "";
  227.       boolean[] visited = new boolean[(int) separate.size()];
  228.       Arrays.fill(visited, false);
  229.       for (int i = 0; i < (int) separate.size(); i++) {
  230.         if (separate.elementAt(i).charAt(0) == 'A') {
  231.           visited[i] = true;
  232.           last_path += String.valueOf(separate.elementAt(i).charAt(0)) + "->" + String.valueOf(separate.elementAt(i).charAt(1));
  233.           break;
  234.         }
  235.       }
  236.       while (true) {
  237.         boolean checker = true;
  238.         for (int i = 0; i < (int) visited.length; i++) {
  239.           if (!visited[i]) {
  240.             checker = false;
  241.             break;
  242.           }
  243.         }
  244.         if (checker) {
  245.           break;
  246.         }
  247.         for (int i = 0; i < (int) separate.size(); i++) {
  248.           if (last_path.charAt((int) last_path.length() - 1) == separate.elementAt(i).charAt(0)) {
  249.             visited[i] = true;
  250.             last_path += ("->" + String.valueOf(separate.elementAt(i).charAt(1)));
  251.             break;
  252.           }
  253.         }
  254.       }
  255.       System.out.println(last_path + " = " + cost + '\n');
  256.    }
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement