-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortPath.java
More file actions
86 lines (71 loc) · 3.25 KB
/
ShortPath.java
File metadata and controls
86 lines (71 loc) · 3.25 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// https://gamma.app/docs/Food-Delivery-Route-Optimization-2mpvgp3yd3kz0m7?mode=doc
import java.util.*;
public class ShortPath {
private static final double INF = Double.MAX_VALUE;
private static Map<String, Double> dpCache = new HashMap<>();
private static Map<String, List<Integer>> pathCache = new HashMap<>();
public static double calculateDistance(int[] point1, int[] point2) {
return Math.sqrt(Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2));
}
public static double dp(int currentIndex, int visitedMask, int[][] locations, double[][] distanceMatrix) {
if (visitedMask == (1 << locations.length) - 1) {
return distanceMatrix[currentIndex][0];
}
String cacheKey = currentIndex + "," + visitedMask;
if (dpCache.containsKey(cacheKey)) {
return dpCache.get(cacheKey);
}
double minCost = INF;
List<Integer> bestPath = new ArrayList<>();
List<Integer> unvisited = new ArrayList<>();
for (int i = 0; i < locations.length; i++) {
if ((visitedMask & (1 << i)) == 0) {
unvisited.add(i);
}
}
unvisited.sort(Comparator.comparingDouble(i -> distanceMatrix[currentIndex][i]));
for (int nextIndex : unvisited) {
double cost = distanceMatrix[currentIndex][nextIndex]
+ dp(nextIndex, visitedMask | (1 << nextIndex), locations, distanceMatrix);
if (cost < minCost) {
minCost = cost;
bestPath.clear();
bestPath.add(nextIndex);
bestPath.addAll(pathCache.getOrDefault(nextIndex + "," + (visitedMask | (1 << nextIndex)), new ArrayList<>()));
}
}
dpCache.put(cacheKey, minCost);
pathCache.put(cacheKey, bestPath);
return minCost;
}
public static double[][] createDistanceMatrix(int[][] locations) {
int n = locations.length;
double[][] distanceMatrix = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distanceMatrix[i][j] = calculateDistance(locations[i], locations[j]);
}
}
return distanceMatrix;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Set default restaurant location to (0, 0)
int[] restaurant = {0, 0};
// Input delivery location
System.out.println("Enter the delivery location (x y):");
int[] delivery = {scanner.nextInt(), scanner.nextInt()};
// Create locations array with restaurant and delivery
int[][] locations = {restaurant, delivery};
double[][] distanceMatrix = createDistanceMatrix(locations);
double minCost = dp(0, 1, locations, distanceMatrix);
// Retrieve the path
List<Integer> path = pathCache.get("0,1");
System.out.println("Minimum Delivery Route Cost: " + minCost);
System.out.print("Delivery Route: Restaurant -> ");
for (int index : path) {
System.out.print("Delivery -> ");
}
System.out.println("Restaurant");
}
}