-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraveling Salesman Problem(TSP).java
More file actions
64 lines (50 loc) · 1.68 KB
/
Traveling Salesman Problem(TSP).java
File metadata and controls
64 lines (50 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
public class TSPNearestNeighbor {
private int[][] graph;
private int numOfNodes;
private List<Integer> tour;
public TSPNearestNeighbor(int[][] graph) {
this.graph = graph;
this.numOfNodes = graph.length;
this.tour = new ArrayList<>();
}
public List<Integer> solveTSP() {
// Start from the first node (node 0)
tour.add(0);
// Keep track of visited nodes
boolean[] visited = new boolean[numOfNodes];
visited[0] = true;
// Loop until all nodes are visited
while (tour.size() < numOfNodes) {
int currentNode = tour.get(tour.size() - 1);
int nextNode = findNearestNeighbor(currentNode, visited);
tour.add(nextNode);
visited[nextNode] = true;
}
// Return to the starting node to complete the tour
tour.add(0);
return tour;
}
private int findNearestNeighbor(int node, boolean[] visited) {
int nearestNeighbor = -1;
int shortestDistance = Integer.MAX_VALUE;
for (int i = 0; i < numOfNodes; i++) {
if (!visited[i] && graph[node][i] < shortestDistance) {
shortestDistance = graph[node][i];
nearestNeighbor = i;
}
}
return nearestNeighbor;
}
public static void main(String[] args) {
int[][] graph = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
TSPNearestNeighbor tsp = new TSPNearestNeighbor(graph);
List<Integer> tour = tsp.solveTSP();
System.out.println("TSP Tour: " + tour);
}
}