Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.ttu.algoritmid.tsp;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class GreedyTSP {
- /* Greedy search */
- public static int[] greedySolution(int[][] adjacencyMatrix) {
- int n = adjacencyMatrix.length;
- int[] answer = new int[n + 1];
- answer[0] = 0;
- Map<Integer, Integer> map = new HashMap<>();
- // key is 0, and value is one, cause it occurs once
- map.put(0, 1);
- int key = 0;
- // loc = location, des = destination
- for (int loc = 1; loc < n; loc++) {
- int min = Integer.MAX_VALUE;
- // if it hasn't already travelled to destination
- for (int des = 0; des < 3; des++) {
- if (!map.containsKey(des) && adjacencyMatrix[loc][des] < min) {
- min = adjacencyMatrix[loc][des];
- key = des;
- }
- }
- // puts every value once
- map.put(key, 1);
- answer[0] = key;
- }
- return answer;
- }
- public static void main(String[] args) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement