Advertisement
PhilHole

Untitled

Oct 31st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package ee.ttu.algoritmid.tsp;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. public class GreedyTSP {
  9.  
  10. /* Greedy search */
  11. public static int[] greedySolution(int[][] adjacencyMatrix) {
  12. int n = adjacencyMatrix.length;
  13. int[] answer = new int[n + 1];
  14. answer[0] = 0;
  15. Map<Integer, Integer> map = new HashMap<>();
  16. // key is 0, and value is one, cause it occurs once
  17. map.put(0, 1);
  18. int key = 0;
  19.  
  20. // loc = location, des = destination
  21. for (int loc = 1; loc < n; loc++) {
  22. int min = Integer.MAX_VALUE;
  23. // if it hasn't already travelled to destination
  24. for (int des = 0; des < 3; des++) {
  25. if (!map.containsKey(des) && adjacencyMatrix[loc][des] < min) {
  26. min = adjacencyMatrix[loc][des];
  27. key = des;
  28. }
  29. }
  30. // puts every value once
  31. map.put(key, 1);
  32. answer[0] = key;
  33. }
  34. return answer;
  35. }
  36.  
  37. public static void main(String[] args) {
  38. for (int i = 0; i < 3; i++) {
  39. for (int j = 0; j < 3; j++) {
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement