Advertisement
nathansimonis1612

Untitled

Nov 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import java.util.*;
  2. public class last_algo {
  3. @SuppressWarnings("unused")
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. int [][] arr = {{21,32,10},{50,20,15}};
  7. adj_matrix(arr);
  8. }
  9. public static void adj_matrix(int [][] arr){
  10. /*
  11. Store the WF and DF of everyone who is going up or down
  12. */
  13. ArrayList<Integer> up_WF = new ArrayList<Integer>();
  14. ArrayList<Integer> up_DF = new ArrayList<Integer>();
  15. ArrayList<Integer> down_WF = new ArrayList<Integer>();
  16. ArrayList<Integer> down_DF = new ArrayList<Integer>();
  17. for (int i = 0; i<arr[0].length;i++) {
  18. if (arr[0][i]<arr[1][i] ) { //person is going up
  19. up_WF.add(arr[0][i]);
  20. up_DF.add(arr[1][i]);
  21. }else { //person is going down
  22. down_WF.add(arr[0][i]);
  23. down_DF.add(arr[1][i]);
  24. }
  25. }
  26. /*
  27. Compute the min and max for each WF and DF
  28. */
  29.  
  30. int min_up_WF = Collections.min(up_WF);
  31. int max_up_DF = Collections.max(up_DF);
  32. int max_down_WF = Collections.max(down_WF);
  33. int min_down_DF = Collections.max(down_DF);
  34.  
  35. // Create the complete list of
  36. ArrayList<Integer> floors = new ArrayList<Integer>();
  37. floors.add(min_up_WF);
  38. floors.add(max_up_DF);
  39. floors.add(max_down_WF);
  40. floors.add(min_down_DF);
  41. Collections.sort(floors);
  42. System.out.println("List of floors : " + floors);
  43. /*
  44. Create the distance Matrix
  45. */
  46. int inf = 99999; // a very large number that is disconnected to the potential costs. Integer.MAX_VALUE gave issues
  47. Graph graph = new Graph(floors.size());
  48. graph.addEdge(floors.indexOf(min_up_WF),floors.indexOf(min_down_DF),Math.abs(min_up_WF-min_down_DF),true);
  49. graph.addEdge(floors.indexOf(min_up_WF), floors.indexOf(max_up_DF), Math.abs(min_up_WF-max_up_DF),false);
  50. graph.addEdge(floors.indexOf(min_up_WF), floors.indexOf(max_down_WF), inf, true);
  51. graph.addEdge(floors.indexOf(max_up_DF), floors.indexOf(max_down_WF), Math.abs(max_up_DF-max_down_WF), true);
  52. graph.addEdge(floors.indexOf(max_up_DF), floors.indexOf(min_up_WF), inf, false);
  53. graph.addEdge(floors.indexOf(max_up_DF), floors.indexOf(min_down_DF), inf, true);
  54. graph.addEdge(floors.indexOf(min_down_DF), floors.indexOf(max_down_WF), inf, false);
  55. graph.addEdge(floors.indexOf(max_down_WF), floors.indexOf(min_down_DF), Math.abs(max_down_WF-min_down_DF), false);
  56. graph.print_adjMatrix();
  57. ArrayList<Integer> sorted_floors = new ArrayList<Integer> (floors); //copy of the original list of floors
  58. int [][] adj_matrix = graph.getAdjMatrix();
  59. min_hamiltonian_path(floors,0,adj_matrix,sorted_floors);
  60. //permutingArray(floors, 0);
  61. }
  62. static void min_hamiltonian_path(ArrayList<Integer> arrayList, int element, int [][] adj_matrix, ArrayList<Integer> sorted_floors) {
  63. for (int i = element; i < arrayList.size(); i++) {
  64. Collections.swap(arrayList, i, element);
  65. min_hamiltonian_path(arrayList, element+1, adj_matrix, sorted_floors);
  66. Collections.swap(arrayList, element, i);
  67. }
  68. if (element == arrayList.size() -1 && arrayList.get(0)==sorted_floors.get(0)) { //recursive base case && starting at the first floor always
  69. //System.out.println(Arrays.deepToString(arrayList.toArray()));
  70. helper(arrayList,adj_matrix, sorted_floors);
  71. }
  72. }
  73. // add a helper function
  74. static void helper(ArrayList<Integer> arrayList, int[][] adj_matrix, ArrayList<Integer> sorted_floors){
  75. System.out.println("New combination of values");
  76. int current_cost = 0;
  77. for (int i = 0; i < arrayList.size() -1; i++){
  78. current_cost += adj_matrix[sorted_floors.indexOf(arrayList.get(i))][sorted_floors.indexOf(arrayList.get(i+1))];
  79. }
  80. System.out.println("The path : " + arrayList + " Has a cost of : " + current_cost);
  81. }
  82. }
  83.  
  84. class Graph{ // graph structure
  85. private static int[][] adjMatrix;
  86. @SuppressWarnings("unused")
  87. private int numVertices;
  88. public Graph(int numVertices){
  89. this.numVertices = numVertices;
  90. adjMatrix = new int[numVertices][numVertices];
  91. }
  92. public void addEdge (int source, int destination, int distance, boolean reverse) {
  93. if (reverse == true) { //Two edges between the vertices
  94. adjMatrix[source][destination] = distance;
  95. adjMatrix[destination][source] = distance;
  96.  
  97. }else { //Directed graph to only one direction
  98. adjMatrix[source][destination] = distance;
  99. }
  100. }
  101. public void print_adjMatrix(){
  102. System.out.println("Distance Matrix : " + Arrays.deepToString(adjMatrix));
  103. }
  104. public static int [][] getAdjMatrix(){
  105. return adjMatrix;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement