dancealgorithm 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dancealgorithm/__init__.py +1 -1
- dancealgorithm/assignments/LC - Delivery/Solution.java +102 -211
- dancealgorithm/assignments/LC - Drilling/Solution.java +110 -221
- dancealgorithm/assignments/LC - Flight/Solution.java +102 -211
- dancealgorithm/downloader.py +1 -1
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/METADATA +1 -1
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/RECORD +11 -11
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/WHEEL +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/entry_points.txt +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/licenses/LICENSE +0 -0
- {dancealgorithm-0.1.2.dist-info → dancealgorithm-0.1.3.dist-info}/top_level.txt +0 -0
dancealgorithm/__init__.py
CHANGED
|
@@ -9,7 +9,7 @@ import sys
|
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from typing import Any, Dict, List, Optional, Union
|
|
11
11
|
|
|
12
|
-
__version__ = "0.1.
|
|
12
|
+
__version__ = "0.1.3"
|
|
13
13
|
|
|
14
14
|
# Import bundled assignments registry and sheets
|
|
15
15
|
from dancealgorithm.assignments import ASSIGNMENTS_DIR, REGISTRY, SHEETS
|
|
@@ -1,261 +1,152 @@
|
|
|
1
|
-
|
|
2
1
|
import java.util.*;
|
|
3
2
|
|
|
4
3
|
public class Solution {
|
|
5
4
|
static final double INF = 1e9;
|
|
6
5
|
|
|
7
|
-
// 1. Main Function
|
|
8
|
-
public static void main(String args[]) {
|
|
9
|
-
Scanner sc = new Scanner(System.in);
|
|
10
|
-
|
|
11
|
-
System.out.print("Enter number of locations: ");
|
|
12
|
-
int iSize = sc.nextInt();
|
|
13
|
-
|
|
14
|
-
String sLocations[] = new String[iSize];
|
|
15
|
-
|
|
16
|
-
// 2. Get Location Names
|
|
17
|
-
sc.nextLine();
|
|
18
|
-
|
|
19
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
20
|
-
System.out.print("Enter location " + (iCnt + 1) + ": ");
|
|
21
|
-
sLocations[iCnt] = sc.nextLine();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 3. Get Cost Matrix
|
|
25
|
-
double dGraph[][] = new double[iSize][iSize];
|
|
26
|
-
|
|
27
|
-
System.out.println("\nEnter travel cost matrix:");
|
|
28
|
-
System.out.println("Enter -1 if there is no direct route.");
|
|
29
|
-
|
|
30
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
31
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
32
|
-
if (iCnt == jCnt) {
|
|
33
|
-
dGraph[iCnt][jCnt] = INF;
|
|
34
|
-
} else {
|
|
35
|
-
System.out.print("Cost from " + sLocations[iCnt] +
|
|
36
|
-
" to " + sLocations[jCnt] + ": ");
|
|
37
|
-
|
|
38
|
-
double dValue = sc.nextDouble();
|
|
39
|
-
|
|
40
|
-
if (dValue == -1) {
|
|
41
|
-
dGraph[iCnt][jCnt] = INF;
|
|
42
|
-
} else {
|
|
43
|
-
dGraph[iCnt][jCnt] = dValue;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 4. Apply LC Branch and Bound
|
|
50
|
-
SolveTSP(dGraph, sLocations);
|
|
51
|
-
|
|
52
|
-
sc.close();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 5. Search Node
|
|
56
6
|
static class Node implements Comparable<Node> {
|
|
57
|
-
double
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
70
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
71
|
-
dMatrix[iCnt], 0, iSize);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
this.iPath = Arrays.copyOf(iPath, iPath.length);
|
|
75
|
-
this.iLevel = iLevel;
|
|
76
|
-
this.dCost = dCost;
|
|
77
|
-
this.iCity = iCity;
|
|
7
|
+
double[][] matrix;
|
|
8
|
+
List<Integer> path;
|
|
9
|
+
double cost;
|
|
10
|
+
int city, level;
|
|
11
|
+
|
|
12
|
+
Node(double[][] m, List<Integer> p, int city, int level, double cost) {
|
|
13
|
+
this.matrix = copy(m);
|
|
14
|
+
this.path = new ArrayList<>(p);
|
|
15
|
+
this.path.add(city);
|
|
16
|
+
this.city = city;
|
|
17
|
+
this.level = level;
|
|
18
|
+
this.cost = cost;
|
|
78
19
|
}
|
|
79
20
|
|
|
80
|
-
public int compareTo(Node
|
|
81
|
-
return Double.compare(this.
|
|
21
|
+
public int compareTo(Node o) {
|
|
22
|
+
return Double.compare(this.cost, o.cost);
|
|
82
23
|
}
|
|
83
24
|
}
|
|
84
25
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
91
|
-
double dMin = INF;
|
|
92
|
-
|
|
93
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
94
|
-
if (dGraph[iCnt][jCnt] < dMin) {
|
|
95
|
-
dMin = dGraph[iCnt][jCnt];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (dMin > 0 && dMin < INF) {
|
|
100
|
-
dReduction += dMin;
|
|
26
|
+
static double[][] copy(double[][] m) {
|
|
27
|
+
double[][] res = new double[m.length][m.length];
|
|
28
|
+
for (int i = 0; i < m.length; i++) res[i] = m[i].clone();
|
|
29
|
+
return res;
|
|
30
|
+
}
|
|
101
31
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
32
|
+
static double reduce(double[][] m) {
|
|
33
|
+
int n = m.length;
|
|
34
|
+
double reduction = 0;
|
|
35
|
+
|
|
36
|
+
// Row reduction
|
|
37
|
+
for (int i = 0; i < n; i++) {
|
|
38
|
+
double min = INF;
|
|
39
|
+
for (int j = 0; j < n; j++) if (m[i][j] < min) min = m[i][j];
|
|
40
|
+
if (min > 0 && min < INF) {
|
|
41
|
+
reduction += min;
|
|
42
|
+
for (int j = 0; j < n; j++) if (m[i][j] < INF) m[i][j] -= min;
|
|
107
43
|
}
|
|
108
44
|
}
|
|
109
45
|
|
|
110
|
-
// Column
|
|
111
|
-
for (int
|
|
112
|
-
double
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (dMin > 0 && dMin < INF) {
|
|
121
|
-
dReduction += dMin;
|
|
122
|
-
|
|
123
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
124
|
-
if (dGraph[iCnt][jCnt] < INF) {
|
|
125
|
-
dGraph[iCnt][jCnt] -= dMin;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
46
|
+
// Column reduction
|
|
47
|
+
for (int j = 0; j < n; j++) {
|
|
48
|
+
double min = INF;
|
|
49
|
+
for (int i = 0; i < n; i++) if (m[i][j] < min) min = m[i][j];
|
|
50
|
+
if (min > 0 && min < INF) {
|
|
51
|
+
reduction += min;
|
|
52
|
+
for (int i = 0; i < n; i++) if (m[i][j] < INF) m[i][j] -= min;
|
|
128
53
|
}
|
|
129
54
|
}
|
|
130
|
-
|
|
131
|
-
return dReduction;
|
|
55
|
+
return reduction;
|
|
132
56
|
}
|
|
133
57
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
double dInitial[][] = new double[iSize][iSize];
|
|
139
|
-
|
|
140
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
141
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
142
|
-
dInitial[iCnt], 0, iSize);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
double dRootCost = ReduceMatrix(dInitial, iSize);
|
|
58
|
+
static void solveTSP(double[][] graph, String[] locations) {
|
|
59
|
+
int n = graph.length;
|
|
60
|
+
double[][] initial = copy(graph);
|
|
61
|
+
double rootCost = reduce(initial);
|
|
146
62
|
|
|
147
63
|
PriorityQueue<Node> pq = new PriorityQueue<>();
|
|
64
|
+
pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
|
|
148
65
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
pq.add(new Node(dInitial, iPath, 1, dRootCost, 0));
|
|
152
|
-
|
|
153
|
-
double dBestCost = INF;
|
|
154
|
-
int iBestTour[] = null;
|
|
66
|
+
double bestCost = INF;
|
|
67
|
+
List<Integer> bestTour = null;
|
|
155
68
|
|
|
156
|
-
// 8. Search for Minimum Cost Tour
|
|
157
69
|
while (!pq.isEmpty()) {
|
|
158
70
|
Node curr = pq.poll();
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (dGraph[iLast][0] < INF) {
|
|
168
|
-
double dTourCost = 0;
|
|
169
|
-
|
|
170
|
-
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
171
|
-
dTourCost += dGraph[curr.iPath[iCnt]][curr.iPath[iCnt + 1]];
|
|
71
|
+
if (curr.cost >= bestCost) continue;
|
|
72
|
+
|
|
73
|
+
if (curr.level == n) {
|
|
74
|
+
int last = curr.city;
|
|
75
|
+
if (graph[last][0] < INF) {
|
|
76
|
+
double tourCost = 0;
|
|
77
|
+
for (int i = 0; i < n - 1; i++) {
|
|
78
|
+
tourCost += graph[curr.path.get(i)][curr.path.get(i + 1)];
|
|
172
79
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
iBestTour = Arrays.copyOf(curr.iPath, iSize + 1);
|
|
179
|
-
iBestTour[iSize] = 0;
|
|
80
|
+
tourCost += graph[last][0];
|
|
81
|
+
if (tourCost < bestCost) {
|
|
82
|
+
bestCost = tourCost;
|
|
83
|
+
bestTour = new ArrayList<>(curr.path);
|
|
84
|
+
bestTour.add(0);
|
|
180
85
|
}
|
|
181
86
|
}
|
|
182
|
-
|
|
183
87
|
continue;
|
|
184
88
|
}
|
|
185
89
|
|
|
186
|
-
int
|
|
90
|
+
int u = curr.city;
|
|
91
|
+
for (int v = 0; v < n; v++) {
|
|
92
|
+
if (!curr.path.contains(v) && curr.matrix[u][v] < INF) {
|
|
93
|
+
double[][] child = copy(curr.matrix);
|
|
94
|
+
double edge = curr.matrix[u][v];
|
|
187
95
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
96
|
+
for (int j = 0; j < n; j++) child[u][j] = INF;
|
|
97
|
+
for (int i = 0; i < n; i++) child[i][v] = INF;
|
|
98
|
+
if (curr.level < n - 1) child[v][0] = INF;
|
|
191
99
|
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (!bVisited && curr.dMatrix[iU][iV] < INF) {
|
|
200
|
-
double dChild[][] = new double[iSize][iSize];
|
|
201
|
-
|
|
202
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
203
|
-
System.arraycopy(curr.dMatrix[iCnt], 0,
|
|
204
|
-
dChild[iCnt], 0, iSize);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
double dEdge = curr.dMatrix[iU][iV];
|
|
208
|
-
|
|
209
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
210
|
-
dChild[iU][jCnt] = INF;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
214
|
-
dChild[iCnt][iV] = INF;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (curr.iLevel < iSize - 1) {
|
|
218
|
-
dChild[iV][0] = INF;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
double dReduction = ReduceMatrix(dChild, iSize);
|
|
222
|
-
|
|
223
|
-
double dChildCost = curr.dCost +
|
|
224
|
-
dEdge + dReduction;
|
|
225
|
-
|
|
226
|
-
if (dChildCost < dBestCost) {
|
|
227
|
-
int iChildPath[] = Arrays.copyOf(
|
|
228
|
-
curr.iPath, curr.iPath.length + 1);
|
|
229
|
-
|
|
230
|
-
iChildPath[iChildPath.length - 1] = iV;
|
|
231
|
-
|
|
232
|
-
pq.add(new Node(dChild, iChildPath,
|
|
233
|
-
curr.iLevel + 1,
|
|
234
|
-
dChildCost, iV));
|
|
100
|
+
double childCost = curr.cost + edge + reduce(child);
|
|
101
|
+
if (childCost < bestCost) {
|
|
102
|
+
pq.add(new Node(child, curr.path, v, curr.level + 1, childCost));
|
|
235
103
|
}
|
|
236
104
|
}
|
|
237
105
|
}
|
|
238
106
|
}
|
|
239
107
|
|
|
240
|
-
|
|
241
|
-
if (iBestTour == null) {
|
|
108
|
+
if (bestTour == null) {
|
|
242
109
|
System.out.println("\nNo complete round-trip route exists.");
|
|
243
110
|
return;
|
|
244
111
|
}
|
|
245
112
|
|
|
246
|
-
System.out.printf("\nOptimal Round-Trip Delivery Cost: Rs. %.2f
|
|
247
|
-
dBestCost);
|
|
248
|
-
|
|
113
|
+
System.out.printf("\nOptimal Round-Trip Delivery Cost: Rs. %.2f\n", bestCost);
|
|
249
114
|
System.out.print("Optimal Delivery Tour: ");
|
|
115
|
+
for (int i = 0; i < bestTour.size(); i++) {
|
|
116
|
+
System.out.print(locations[bestTour.get(i)] + (i < bestTour.size() - 1 ? " -> " : "\n"));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
250
119
|
|
|
251
|
-
|
|
252
|
-
|
|
120
|
+
public static void main(String[] args) {
|
|
121
|
+
Scanner sc = new Scanner(System.in);
|
|
253
122
|
|
|
254
|
-
|
|
255
|
-
|
|
123
|
+
System.out.print("Enter number of locations: ");
|
|
124
|
+
int n = sc.nextInt();
|
|
125
|
+
sc.nextLine();
|
|
126
|
+
|
|
127
|
+
String[] locations = new String[n];
|
|
128
|
+
for (int i = 0; i < n; i++) {
|
|
129
|
+
System.out.print("Enter location " + (i + 1) + ": ");
|
|
130
|
+
locations[i] = sc.nextLine().trim();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
double[][] graph = new double[n][n];
|
|
134
|
+
System.out.println("\nEnter travel cost matrix:");
|
|
135
|
+
System.out.println("Enter -1 if there is no direct route.");
|
|
136
|
+
|
|
137
|
+
for (int i = 0; i < n; i++) {
|
|
138
|
+
for (int j = 0; j < n; j++) {
|
|
139
|
+
if (i == j) {
|
|
140
|
+
graph[i][j] = INF;
|
|
141
|
+
} else {
|
|
142
|
+
System.out.print("Cost from " + locations[i] + " to " + locations[j] + ": ");
|
|
143
|
+
double val = sc.nextDouble();
|
|
144
|
+
graph[i][j] = (val == -1) ? INF : val;
|
|
145
|
+
}
|
|
256
146
|
}
|
|
257
147
|
}
|
|
258
148
|
|
|
259
|
-
|
|
149
|
+
solveTSP(graph, locations);
|
|
150
|
+
sc.close();
|
|
260
151
|
}
|
|
261
152
|
}
|
|
@@ -1,275 +1,164 @@
|
|
|
1
|
-
|
|
2
1
|
import java.util.*;
|
|
3
2
|
|
|
4
3
|
public class Solution {
|
|
5
4
|
static final double INF = 1e9;
|
|
6
5
|
|
|
7
|
-
// 1. Main Function
|
|
8
|
-
public static void main(String args[]) {
|
|
9
|
-
Scanner sc = new Scanner(System.in);
|
|
10
|
-
|
|
11
|
-
System.out.print("Enter number of drilling points (including Home): ");
|
|
12
|
-
int iSize = sc.nextInt();
|
|
13
|
-
|
|
14
|
-
Hole holes[] = new Hole[iSize];
|
|
15
|
-
|
|
16
|
-
// 2. Get Hole Coordinates
|
|
17
|
-
sc.nextLine();
|
|
18
|
-
|
|
19
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
20
|
-
System.out.println("\nEnter details for Point " + iCnt);
|
|
21
|
-
|
|
22
|
-
System.out.print("Point Name: ");
|
|
23
|
-
String sName = sc.nextLine();
|
|
24
|
-
|
|
25
|
-
System.out.print("X Coordinate: ");
|
|
26
|
-
double dX = sc.nextDouble();
|
|
27
|
-
|
|
28
|
-
System.out.print("Y Coordinate: ");
|
|
29
|
-
double dY = sc.nextDouble();
|
|
30
|
-
|
|
31
|
-
sc.nextLine();
|
|
32
|
-
|
|
33
|
-
holes[iCnt] = new Hole(dX, dY, sName);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// 3. Create Distance Matrix
|
|
37
|
-
double dGraph[][] = new double[iSize][iSize];
|
|
38
|
-
|
|
39
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
40
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
41
|
-
if (iCnt == jCnt) {
|
|
42
|
-
dGraph[iCnt][jCnt] = INF;
|
|
43
|
-
} else {
|
|
44
|
-
dGraph[iCnt][jCnt] = Math.hypot(
|
|
45
|
-
holes[iCnt].dX - holes[jCnt].dX,
|
|
46
|
-
holes[iCnt].dY - holes[jCnt].dY);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 4. Apply LC Branch and Bound
|
|
52
|
-
SolveTSP(dGraph, holes);
|
|
53
|
-
|
|
54
|
-
sc.close();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// 5. Hole Entity
|
|
58
6
|
static class Hole {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
65
|
-
this.sName = sName;
|
|
7
|
+
String name;
|
|
8
|
+
double x, y;
|
|
9
|
+
Hole(String name, double x, double y) {
|
|
10
|
+
this.name = name;
|
|
11
|
+
this.x = x;
|
|
12
|
+
this.y = y;
|
|
66
13
|
}
|
|
67
14
|
}
|
|
68
15
|
|
|
69
|
-
// 6. Search Node
|
|
70
16
|
static class Node implements Comparable<Node> {
|
|
71
|
-
double
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
84
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
85
|
-
dMatrix[iCnt], 0, iSize);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
this.iPath = Arrays.copyOf(iPath, iPath.length);
|
|
89
|
-
this.iLevel = iLevel;
|
|
90
|
-
this.dCost = dCost;
|
|
91
|
-
this.iCity = iCity;
|
|
17
|
+
double[][] matrix;
|
|
18
|
+
List<Integer> path;
|
|
19
|
+
double cost;
|
|
20
|
+
int city, level;
|
|
21
|
+
|
|
22
|
+
Node(double[][] m, List<Integer> p, int city, int level, double cost) {
|
|
23
|
+
this.matrix = copy(m);
|
|
24
|
+
this.path = new ArrayList<>(p);
|
|
25
|
+
this.path.add(city);
|
|
26
|
+
this.city = city;
|
|
27
|
+
this.level = level;
|
|
28
|
+
this.cost = cost;
|
|
92
29
|
}
|
|
93
30
|
|
|
94
|
-
public int compareTo(Node
|
|
95
|
-
return Double.compare(this.
|
|
31
|
+
public int compareTo(Node o) {
|
|
32
|
+
return Double.compare(this.cost, o.cost);
|
|
96
33
|
}
|
|
97
34
|
}
|
|
98
35
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
105
|
-
double dMin = INF;
|
|
106
|
-
|
|
107
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
108
|
-
if (dGraph[iCnt][jCnt] < dMin) {
|
|
109
|
-
dMin = dGraph[iCnt][jCnt];
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (dMin > 0 && dMin < INF) {
|
|
114
|
-
dReduction += dMin;
|
|
36
|
+
static double[][] copy(double[][] m) {
|
|
37
|
+
double[][] res = new double[m.length][m.length];
|
|
38
|
+
for (int i = 0; i < m.length; i++) res[i] = m[i].clone();
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
115
41
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
42
|
+
static double reduce(double[][] m) {
|
|
43
|
+
int n = m.length;
|
|
44
|
+
double reduction = 0;
|
|
45
|
+
|
|
46
|
+
// Row reduction
|
|
47
|
+
for (int i = 0; i < n; i++) {
|
|
48
|
+
double min = INF;
|
|
49
|
+
for (int j = 0; j < n; j++) if (m[i][j] < min) min = m[i][j];
|
|
50
|
+
if (min > 0 && min < INF) {
|
|
51
|
+
reduction += min;
|
|
52
|
+
for (int j = 0; j < n; j++) if (m[i][j] < INF) m[i][j] -= min;
|
|
121
53
|
}
|
|
122
54
|
}
|
|
123
55
|
|
|
124
|
-
// Column
|
|
125
|
-
for (int
|
|
126
|
-
double
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (dMin > 0 && dMin < INF) {
|
|
135
|
-
dReduction += dMin;
|
|
136
|
-
|
|
137
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
138
|
-
if (dGraph[iCnt][jCnt] < INF) {
|
|
139
|
-
dGraph[iCnt][jCnt] -= dMin;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
56
|
+
// Column reduction
|
|
57
|
+
for (int j = 0; j < n; j++) {
|
|
58
|
+
double min = INF;
|
|
59
|
+
for (int i = 0; i < n; i++) if (m[i][j] < min) min = m[i][j];
|
|
60
|
+
if (min > 0 && min < INF) {
|
|
61
|
+
reduction += min;
|
|
62
|
+
for (int i = 0; i < n; i++) if (m[i][j] < INF) m[i][j] -= min;
|
|
142
63
|
}
|
|
143
64
|
}
|
|
144
|
-
|
|
145
|
-
return dReduction;
|
|
65
|
+
return reduction;
|
|
146
66
|
}
|
|
147
67
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
double dInitial[][] = new double[iSize][iSize];
|
|
153
|
-
|
|
154
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
155
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
156
|
-
dInitial[iCnt], 0, iSize);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
double dRootCost = ReduceMatrix(dInitial, iSize);
|
|
68
|
+
static void solveTSP(double[][] graph, Hole[] holes) {
|
|
69
|
+
int n = graph.length;
|
|
70
|
+
double[][] initial = copy(graph);
|
|
71
|
+
double rootCost = reduce(initial);
|
|
160
72
|
|
|
161
73
|
PriorityQueue<Node> pq = new PriorityQueue<>();
|
|
74
|
+
pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
|
|
162
75
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
pq.add(new Node(dInitial, iPath, 1, dRootCost, 0));
|
|
166
|
-
|
|
167
|
-
double dBestCost = INF;
|
|
168
|
-
int iBestTour[] = null;
|
|
76
|
+
double bestCost = INF;
|
|
77
|
+
List<Integer> bestTour = null;
|
|
169
78
|
|
|
170
|
-
// 9. Search for Minimum Cost Tour
|
|
171
79
|
while (!pq.isEmpty()) {
|
|
172
80
|
Node curr = pq.poll();
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (dGraph[iLast][0] < INF) {
|
|
182
|
-
double dTourCost = 0;
|
|
183
|
-
|
|
184
|
-
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
185
|
-
dTourCost += dGraph[curr.iPath[iCnt]][curr.iPath[iCnt + 1]];
|
|
81
|
+
if (curr.cost >= bestCost) continue;
|
|
82
|
+
|
|
83
|
+
if (curr.level == n) {
|
|
84
|
+
int last = curr.city;
|
|
85
|
+
if (graph[last][0] < INF) {
|
|
86
|
+
double tourCost = 0;
|
|
87
|
+
for (int i = 0; i < n - 1; i++) {
|
|
88
|
+
tourCost += graph[curr.path.get(i)][curr.path.get(i + 1)];
|
|
186
89
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
iBestTour = Arrays.copyOf(curr.iPath, iSize + 1);
|
|
193
|
-
iBestTour[iSize] = 0;
|
|
90
|
+
tourCost += graph[last][0];
|
|
91
|
+
if (tourCost < bestCost) {
|
|
92
|
+
bestCost = tourCost;
|
|
93
|
+
bestTour = new ArrayList<>(curr.path);
|
|
94
|
+
bestTour.add(0);
|
|
194
95
|
}
|
|
195
96
|
}
|
|
196
|
-
|
|
197
97
|
continue;
|
|
198
98
|
}
|
|
199
99
|
|
|
200
|
-
int
|
|
100
|
+
int u = curr.city;
|
|
101
|
+
for (int v = 0; v < n; v++) {
|
|
102
|
+
if (!curr.path.contains(v) && curr.matrix[u][v] < INF) {
|
|
103
|
+
double[][] child = copy(curr.matrix);
|
|
104
|
+
double edge = curr.matrix[u][v];
|
|
201
105
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
106
|
+
for (int j = 0; j < n; j++) child[u][j] = INF;
|
|
107
|
+
for (int i = 0; i < n; i++) child[i][v] = INF;
|
|
108
|
+
if (curr.level < n - 1) child[v][0] = INF;
|
|
205
109
|
|
|
206
|
-
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (!bVisited && curr.dMatrix[iU][iV] < INF) {
|
|
214
|
-
double dChild[][] = new double[iSize][iSize];
|
|
215
|
-
|
|
216
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
217
|
-
System.arraycopy(curr.dMatrix[iCnt], 0,
|
|
218
|
-
dChild[iCnt], 0, iSize);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
double dEdge = curr.dMatrix[iU][iV];
|
|
222
|
-
|
|
223
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
224
|
-
dChild[iU][jCnt] = INF;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
228
|
-
dChild[iCnt][iV] = INF;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (curr.iLevel < iSize - 1) {
|
|
232
|
-
dChild[iV][0] = INF;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
double dReduction = ReduceMatrix(dChild, iSize);
|
|
236
|
-
|
|
237
|
-
double dChildCost = curr.dCost +
|
|
238
|
-
dEdge + dReduction;
|
|
239
|
-
|
|
240
|
-
if (dChildCost < dBestCost) {
|
|
241
|
-
int iChildPath[] = Arrays.copyOf(
|
|
242
|
-
curr.iPath, curr.iPath.length + 1);
|
|
243
|
-
|
|
244
|
-
iChildPath[iChildPath.length - 1] = iV;
|
|
245
|
-
|
|
246
|
-
pq.add(new Node(dChild, iChildPath,
|
|
247
|
-
curr.iLevel + 1,
|
|
248
|
-
dChildCost, iV));
|
|
110
|
+
double childCost = curr.cost + edge + reduce(child);
|
|
111
|
+
if (childCost < bestCost) {
|
|
112
|
+
pq.add(new Node(child, curr.path, v, curr.level + 1, childCost));
|
|
249
113
|
}
|
|
250
114
|
}
|
|
251
115
|
}
|
|
252
116
|
}
|
|
253
117
|
|
|
254
|
-
|
|
255
|
-
if (iBestTour == null) {
|
|
118
|
+
if (bestTour == null) {
|
|
256
119
|
System.out.println("\nNo complete drilling tour exists.");
|
|
257
120
|
return;
|
|
258
121
|
}
|
|
259
122
|
|
|
260
|
-
System.out.printf("\nTotal Drill Head Travel Distance: %.2f mm
|
|
261
|
-
dBestCost);
|
|
262
|
-
|
|
123
|
+
System.out.printf("\nTotal Drill Head Travel Distance: %.2f mm\n", bestCost);
|
|
263
124
|
System.out.print("Optimal Drilling Toolpath: ");
|
|
125
|
+
for (int i = 0; i < bestTour.size(); i++) {
|
|
126
|
+
System.out.print(holes[bestTour.get(i)].name + (i < bestTour.size() - 1 ? " -> " : "\n"));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public static void main(String[] args) {
|
|
131
|
+
Scanner sc = new Scanner(System.in);
|
|
132
|
+
|
|
133
|
+
System.out.print("Enter number of drilling points (including Home): ");
|
|
134
|
+
int n = sc.nextInt();
|
|
135
|
+
sc.nextLine();
|
|
264
136
|
|
|
265
|
-
|
|
266
|
-
|
|
137
|
+
Hole[] holes = new Hole[n];
|
|
138
|
+
for (int i = 0; i < n; i++) {
|
|
139
|
+
System.out.println("\nEnter details for Point " + i);
|
|
140
|
+
System.out.print("Point Name: ");
|
|
141
|
+
String name = sc.nextLine().trim();
|
|
142
|
+
System.out.print("X Coordinate: ");
|
|
143
|
+
double x = sc.nextDouble();
|
|
144
|
+
System.out.print("Y Coordinate: ");
|
|
145
|
+
double y = sc.nextDouble();
|
|
146
|
+
sc.nextLine();
|
|
147
|
+
holes[i] = new Hole(name, x, y);
|
|
148
|
+
}
|
|
267
149
|
|
|
268
|
-
|
|
269
|
-
|
|
150
|
+
double[][] graph = new double[n][n];
|
|
151
|
+
for (int i = 0; i < n; i++) {
|
|
152
|
+
for (int j = 0; j < n; j++) {
|
|
153
|
+
if (i == j) {
|
|
154
|
+
graph[i][j] = INF;
|
|
155
|
+
} else {
|
|
156
|
+
graph[i][j] = Math.hypot(holes[i].x - holes[j].x, holes[i].y - holes[j].y);
|
|
157
|
+
}
|
|
270
158
|
}
|
|
271
159
|
}
|
|
272
160
|
|
|
273
|
-
|
|
161
|
+
solveTSP(graph, holes);
|
|
162
|
+
sc.close();
|
|
274
163
|
}
|
|
275
164
|
}
|
|
@@ -1,261 +1,152 @@
|
|
|
1
|
-
|
|
2
1
|
import java.util.*;
|
|
3
2
|
|
|
4
3
|
public class Solution {
|
|
5
4
|
static final double INF = 1e9;
|
|
6
5
|
|
|
7
|
-
// 1. Main Function
|
|
8
|
-
public static void main(String args[]) {
|
|
9
|
-
Scanner sc = new Scanner(System.in);
|
|
10
|
-
|
|
11
|
-
System.out.print("Enter number of airports: ");
|
|
12
|
-
int iSize = sc.nextInt();
|
|
13
|
-
|
|
14
|
-
String sAirports[] = new String[iSize];
|
|
15
|
-
|
|
16
|
-
// 2. Get Airport Names
|
|
17
|
-
sc.nextLine();
|
|
18
|
-
|
|
19
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
20
|
-
System.out.print("Enter airport " + (iCnt + 1) + ": ");
|
|
21
|
-
sAirports[iCnt] = sc.nextLine();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 3. Get Cost Matrix
|
|
25
|
-
double dGraph[][] = new double[iSize][iSize];
|
|
26
|
-
|
|
27
|
-
System.out.println("\nEnter flight operational cost matrix:");
|
|
28
|
-
System.out.println("Enter -1 if no direct flight exists.");
|
|
29
|
-
|
|
30
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
31
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
32
|
-
if (iCnt == jCnt) {
|
|
33
|
-
dGraph[iCnt][jCnt] = INF;
|
|
34
|
-
} else {
|
|
35
|
-
System.out.print("Cost from " + sAirports[iCnt] +
|
|
36
|
-
" to " + sAirports[jCnt] + ": ");
|
|
37
|
-
|
|
38
|
-
double dValue = sc.nextDouble();
|
|
39
|
-
|
|
40
|
-
if (dValue == -1) {
|
|
41
|
-
dGraph[iCnt][jCnt] = INF;
|
|
42
|
-
} else {
|
|
43
|
-
dGraph[iCnt][jCnt] = dValue;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 4. Apply LC Branch and Bound
|
|
50
|
-
SolveTSP(dGraph, sAirports);
|
|
51
|
-
|
|
52
|
-
sc.close();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 5. Search Node
|
|
56
6
|
static class Node implements Comparable<Node> {
|
|
57
|
-
double
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
70
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
71
|
-
dMatrix[iCnt], 0, iSize);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
this.iPath = Arrays.copyOf(iPath, iPath.length);
|
|
75
|
-
this.iLevel = iLevel;
|
|
76
|
-
this.dCost = dCost;
|
|
77
|
-
this.iCity = iCity;
|
|
7
|
+
double[][] matrix;
|
|
8
|
+
List<Integer> path;
|
|
9
|
+
double cost;
|
|
10
|
+
int city, level;
|
|
11
|
+
|
|
12
|
+
Node(double[][] m, List<Integer> p, int city, int level, double cost) {
|
|
13
|
+
this.matrix = copy(m);
|
|
14
|
+
this.path = new ArrayList<>(p);
|
|
15
|
+
this.path.add(city);
|
|
16
|
+
this.city = city;
|
|
17
|
+
this.level = level;
|
|
18
|
+
this.cost = cost;
|
|
78
19
|
}
|
|
79
20
|
|
|
80
|
-
public int compareTo(Node
|
|
81
|
-
return Double.compare(this.
|
|
21
|
+
public int compareTo(Node o) {
|
|
22
|
+
return Double.compare(this.cost, o.cost);
|
|
82
23
|
}
|
|
83
24
|
}
|
|
84
25
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
91
|
-
double dMin = INF;
|
|
92
|
-
|
|
93
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
94
|
-
if (dGraph[iCnt][jCnt] < dMin) {
|
|
95
|
-
dMin = dGraph[iCnt][jCnt];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (dMin > 0 && dMin < INF) {
|
|
100
|
-
dReduction += dMin;
|
|
26
|
+
static double[][] copy(double[][] m) {
|
|
27
|
+
double[][] res = new double[m.length][m.length];
|
|
28
|
+
for (int i = 0; i < m.length; i++) res[i] = m[i].clone();
|
|
29
|
+
return res;
|
|
30
|
+
}
|
|
101
31
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
32
|
+
static double reduce(double[][] m) {
|
|
33
|
+
int n = m.length;
|
|
34
|
+
double reduction = 0;
|
|
35
|
+
|
|
36
|
+
// Row reduction
|
|
37
|
+
for (int i = 0; i < n; i++) {
|
|
38
|
+
double min = INF;
|
|
39
|
+
for (int j = 0; j < n; j++) if (m[i][j] < min) min = m[i][j];
|
|
40
|
+
if (min > 0 && min < INF) {
|
|
41
|
+
reduction += min;
|
|
42
|
+
for (int j = 0; j < n; j++) if (m[i][j] < INF) m[i][j] -= min;
|
|
107
43
|
}
|
|
108
44
|
}
|
|
109
45
|
|
|
110
|
-
// Column
|
|
111
|
-
for (int
|
|
112
|
-
double
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (dMin > 0 && dMin < INF) {
|
|
121
|
-
dReduction += dMin;
|
|
122
|
-
|
|
123
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
124
|
-
if (dGraph[iCnt][jCnt] < INF) {
|
|
125
|
-
dGraph[iCnt][jCnt] -= dMin;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
46
|
+
// Column reduction
|
|
47
|
+
for (int j = 0; j < n; j++) {
|
|
48
|
+
double min = INF;
|
|
49
|
+
for (int i = 0; i < n; i++) if (m[i][j] < min) min = m[i][j];
|
|
50
|
+
if (min > 0 && min < INF) {
|
|
51
|
+
reduction += min;
|
|
52
|
+
for (int i = 0; i < n; i++) if (m[i][j] < INF) m[i][j] -= min;
|
|
128
53
|
}
|
|
129
54
|
}
|
|
130
|
-
|
|
131
|
-
return dReduction;
|
|
55
|
+
return reduction;
|
|
132
56
|
}
|
|
133
57
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
double dInitial[][] = new double[iSize][iSize];
|
|
139
|
-
|
|
140
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
141
|
-
System.arraycopy(dGraph[iCnt], 0,
|
|
142
|
-
dInitial[iCnt], 0, iSize);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
double dRootCost = ReduceMatrix(dInitial, iSize);
|
|
58
|
+
static void solveTSP(double[][] graph, String[] airports) {
|
|
59
|
+
int n = graph.length;
|
|
60
|
+
double[][] initial = copy(graph);
|
|
61
|
+
double rootCost = reduce(initial);
|
|
146
62
|
|
|
147
63
|
PriorityQueue<Node> pq = new PriorityQueue<>();
|
|
64
|
+
pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
|
|
148
65
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
pq.add(new Node(dInitial, iPath, 1, dRootCost, 0));
|
|
152
|
-
|
|
153
|
-
double dBestCost = INF;
|
|
154
|
-
int iBestTour[] = null;
|
|
66
|
+
double bestCost = INF;
|
|
67
|
+
List<Integer> bestTour = null;
|
|
155
68
|
|
|
156
|
-
// 8. Search for Minimum Cost Tour
|
|
157
69
|
while (!pq.isEmpty()) {
|
|
158
70
|
Node curr = pq.poll();
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (dGraph[iLast][0] < INF) {
|
|
168
|
-
double dTourCost = 0;
|
|
169
|
-
|
|
170
|
-
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
171
|
-
dTourCost += dGraph[curr.iPath[iCnt]][curr.iPath[iCnt + 1]];
|
|
71
|
+
if (curr.cost >= bestCost) continue;
|
|
72
|
+
|
|
73
|
+
if (curr.level == n) {
|
|
74
|
+
int last = curr.city;
|
|
75
|
+
if (graph[last][0] < INF) {
|
|
76
|
+
double tourCost = 0;
|
|
77
|
+
for (int i = 0; i < n - 1; i++) {
|
|
78
|
+
tourCost += graph[curr.path.get(i)][curr.path.get(i + 1)];
|
|
172
79
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
iBestTour = Arrays.copyOf(curr.iPath, iSize + 1);
|
|
179
|
-
iBestTour[iSize] = 0;
|
|
80
|
+
tourCost += graph[last][0];
|
|
81
|
+
if (tourCost < bestCost) {
|
|
82
|
+
bestCost = tourCost;
|
|
83
|
+
bestTour = new ArrayList<>(curr.path);
|
|
84
|
+
bestTour.add(0);
|
|
180
85
|
}
|
|
181
86
|
}
|
|
182
|
-
|
|
183
87
|
continue;
|
|
184
88
|
}
|
|
185
89
|
|
|
186
|
-
int
|
|
90
|
+
int u = curr.city;
|
|
91
|
+
for (int v = 0; v < n; v++) {
|
|
92
|
+
if (!curr.path.contains(v) && curr.matrix[u][v] < INF) {
|
|
93
|
+
double[][] child = copy(curr.matrix);
|
|
94
|
+
double edge = curr.matrix[u][v];
|
|
187
95
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
96
|
+
for (int j = 0; j < n; j++) child[u][j] = INF;
|
|
97
|
+
for (int i = 0; i < n; i++) child[i][v] = INF;
|
|
98
|
+
if (curr.level < n - 1) child[v][0] = INF;
|
|
191
99
|
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (!bVisited && curr.dMatrix[iU][iV] < INF) {
|
|
200
|
-
double dChild[][] = new double[iSize][iSize];
|
|
201
|
-
|
|
202
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
203
|
-
System.arraycopy(curr.dMatrix[iCnt], 0,
|
|
204
|
-
dChild[iCnt], 0, iSize);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
double dEdge = curr.dMatrix[iU][iV];
|
|
208
|
-
|
|
209
|
-
for (int jCnt = 0; jCnt < iSize; jCnt++) {
|
|
210
|
-
dChild[iU][jCnt] = INF;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
214
|
-
dChild[iCnt][iV] = INF;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (curr.iLevel < iSize - 1) {
|
|
218
|
-
dChild[iV][0] = INF;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
double dReduction = ReduceMatrix(dChild, iSize);
|
|
222
|
-
|
|
223
|
-
double dChildCost = curr.dCost +
|
|
224
|
-
dEdge + dReduction;
|
|
225
|
-
|
|
226
|
-
if (dChildCost < dBestCost) {
|
|
227
|
-
int iChildPath[] = Arrays.copyOf(
|
|
228
|
-
curr.iPath, curr.iPath.length + 1);
|
|
229
|
-
|
|
230
|
-
iChildPath[iChildPath.length - 1] = iV;
|
|
231
|
-
|
|
232
|
-
pq.add(new Node(dChild, iChildPath,
|
|
233
|
-
curr.iLevel + 1,
|
|
234
|
-
dChildCost, iV));
|
|
100
|
+
double childCost = curr.cost + edge + reduce(child);
|
|
101
|
+
if (childCost < bestCost) {
|
|
102
|
+
pq.add(new Node(child, curr.path, v, curr.level + 1, childCost));
|
|
235
103
|
}
|
|
236
104
|
}
|
|
237
105
|
}
|
|
238
106
|
}
|
|
239
107
|
|
|
240
|
-
|
|
241
|
-
if (iBestTour == null) {
|
|
108
|
+
if (bestTour == null) {
|
|
242
109
|
System.out.println("\nNo complete round-trip flight route exists.");
|
|
243
110
|
return;
|
|
244
111
|
}
|
|
245
112
|
|
|
246
|
-
System.out.printf("\nMinimum Total Aircraft Operational Cost: Rs. %.2f Lakhs
|
|
247
|
-
dBestCost);
|
|
248
|
-
|
|
113
|
+
System.out.printf("\nMinimum Total Aircraft Operational Cost: Rs. %.2f Lakhs\n", bestCost);
|
|
249
114
|
System.out.print("Optimal Flight Tour: ");
|
|
115
|
+
for (int i = 0; i < bestTour.size(); i++) {
|
|
116
|
+
System.out.print(airports[bestTour.get(i)] + (i < bestTour.size() - 1 ? " -> " : "\n"));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
250
119
|
|
|
251
|
-
|
|
252
|
-
|
|
120
|
+
public static void main(String[] args) {
|
|
121
|
+
Scanner sc = new Scanner(System.in);
|
|
253
122
|
|
|
254
|
-
|
|
255
|
-
|
|
123
|
+
System.out.print("Enter number of airports: ");
|
|
124
|
+
int n = sc.nextInt();
|
|
125
|
+
sc.nextLine();
|
|
126
|
+
|
|
127
|
+
String[] airports = new String[n];
|
|
128
|
+
for (int i = 0; i < n; i++) {
|
|
129
|
+
System.out.print("Enter airport " + (i + 1) + ": ");
|
|
130
|
+
airports[i] = sc.nextLine().trim();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
double[][] graph = new double[n][n];
|
|
134
|
+
System.out.println("\nEnter flight operational cost matrix:");
|
|
135
|
+
System.out.println("Enter -1 if no direct flight exists.");
|
|
136
|
+
|
|
137
|
+
for (int i = 0; i < n; i++) {
|
|
138
|
+
for (int j = 0; j < n; j++) {
|
|
139
|
+
if (i == j) {
|
|
140
|
+
graph[i][j] = INF;
|
|
141
|
+
} else {
|
|
142
|
+
System.out.print("Cost from " + airports[i] + " to " + airports[j] + ": ");
|
|
143
|
+
double val = sc.nextDouble();
|
|
144
|
+
graph[i][j] = (val == -1) ? INF : val;
|
|
145
|
+
}
|
|
256
146
|
}
|
|
257
147
|
}
|
|
258
148
|
|
|
259
|
-
|
|
149
|
+
solveTSP(graph, airports);
|
|
150
|
+
sc.close();
|
|
260
151
|
}
|
|
261
152
|
}
|
dancealgorithm/downloader.py
CHANGED
|
@@ -15,7 +15,7 @@ import zipfile
|
|
|
15
15
|
from pathlib import Path
|
|
16
16
|
from typing import Dict, List, Optional, Tuple
|
|
17
17
|
|
|
18
|
-
USER_AGENT = "DanceAlgorithm-CLI/0.1.
|
|
18
|
+
USER_AGENT = "DanceAlgorithm-CLI/0.1.3"
|
|
19
19
|
MAX_ARCHIVE_SIZE_BYTES = 300 * 1024 * 1024 # 300 MB limit
|
|
20
20
|
MAX_FILE_COUNT = 10000 # 10,000 files limit
|
|
21
21
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
dancealgorithm/__init__.py,sha256
|
|
1
|
+
dancealgorithm/__init__.py,sha256=-Ab304-q9HMimR-_dcbOqZOdwhf2Dnf6G8AqjvHsPpw,4102
|
|
2
2
|
dancealgorithm/__main__.py,sha256=o7NdtuZ8z0YCl_VHH-K-ScUaLzsEF-aJvBhEE83e_bA,147
|
|
3
3
|
dancealgorithm/cli.py,sha256=c8iLHbPfxXj7nByjj6S07rPP87nJZHjcWKBITOBRkKY,11400
|
|
4
4
|
dancealgorithm/config.py,sha256=DkKqK5KzWarrffRgOBXNMcP-6WO0plH7sWgRtJ_Clog,3469
|
|
5
|
-
dancealgorithm/downloader.py,sha256=
|
|
5
|
+
dancealgorithm/downloader.py,sha256=g0GJ2YvKUttHoxWGmSOTcajD5RuD8CtO4vbt3w-M5NI,10914
|
|
6
6
|
dancealgorithm/assignments/DA.txt,sha256=ApbF0CgOHjkxpu1U3mz9eKAFmms4YQTCIXSCI0tmGHY,821
|
|
7
7
|
dancealgorithm/assignments/FK.txt,sha256=jkYwT6a9kHoLBGlqY49johxZ5nZ7hFvLB4rhO5Ta6Yc,677
|
|
8
8
|
dancealgorithm/assignments/GC.txt,sha256=QYL1Bo4oZC1btUjj7tZJi-Toh3uDE9klzYJGDp9at9g,788
|
|
@@ -36,11 +36,11 @@ dancealgorithm/assignments/K - Disaster/Solution.java,sha256=MEtGgOqiwP9oWPnDszL
|
|
|
36
36
|
dancealgorithm/assignments/K - Disaster/__init__.py,sha256=yMf72b6rZmC_KytRvo3lRT7blYagOS6Owi6COLTlGfA,1244
|
|
37
37
|
dancealgorithm/assignments/K - Hospital/Solution.java,sha256=LrvcnmmErFjBs6y29e42JsIuu4JnZElFP3s-fTaAy7o,3737
|
|
38
38
|
dancealgorithm/assignments/K - Hospital/__init__.py,sha256=zLAICmZYB3VbovKbQYNCfn-n59HcGeeLYneXpEu_NYA,1244
|
|
39
|
-
dancealgorithm/assignments/LC - Delivery/Solution.java,sha256=
|
|
39
|
+
dancealgorithm/assignments/LC - Delivery/Solution.java,sha256=qfWcV0uSTR29clF9Dd3B3H293wJK7tyNpqfd4WU-8rM,5046
|
|
40
40
|
dancealgorithm/assignments/LC - Delivery/__init__.py,sha256=mG6F5WJxD_6CpQxdvuz6ktkQdyefcHiioBf8sVFNnwc,1245
|
|
41
|
-
dancealgorithm/assignments/LC - Drilling/Solution.java,sha256=
|
|
41
|
+
dancealgorithm/assignments/LC - Drilling/Solution.java,sha256=8Tg973jHIay0vOF4jargR9JreoaFfWzsU-75ZOLvXYc,5314
|
|
42
42
|
dancealgorithm/assignments/LC - Drilling/__init__.py,sha256=m6h04f3RWoA4Bwp7V91s-B_pKtVdVbKdQ_wuNCjLCwM,1245
|
|
43
|
-
dancealgorithm/assignments/LC - Flight/Solution.java,sha256=
|
|
43
|
+
dancealgorithm/assignments/LC - Flight/Solution.java,sha256=9bNOvZYzD3qPFl1lYbabIiloSUjDt3N74_V1RgwvFRA,5066
|
|
44
44
|
dancealgorithm/assignments/LC - Flight/__init__.py,sha256=7mrQt2JC3WcswbLrKSComS5uKgkAwPHSAPR3A7qX6jQ,1243
|
|
45
45
|
dancealgorithm/assignments/MS - Commerce/Solution.java,sha256=KZlxjeT0Zw1I06SnfsEfM5GJ-HFs8j4y5Nuoe5YaMmw,4457
|
|
46
46
|
dancealgorithm/assignments/MS - Commerce/__init__.py,sha256=zUTiYHn2ZC1xr5yvpgdYob5Fkj8_5pYbauC-56UZhkw,1245
|
|
@@ -66,9 +66,9 @@ dancealgorithm/assignments/QS - Movie/dataset.csv,sha256=rsnfsXCCQWrRfNEB_lUdNWi
|
|
|
66
66
|
dancealgorithm/assignments/QS - Online/Solution.java,sha256=A8bALrw4QDgazRTUKpYZ1nBr1rVXezoFWjnvSQKHk_I,3747
|
|
67
67
|
dancealgorithm/assignments/QS - Online/__init__.py,sha256=6HfCKL8PWcjMerKOJDq6qUglHp5XmvlF-x-YXpRaBfQ,1243
|
|
68
68
|
dancealgorithm/assignments/QS - Online/dataset.csv,sha256=jLrUusfK5fqLRT9ZmJKZPDZ5s2Zw94JixGrKJGoGHNc,631
|
|
69
|
-
dancealgorithm-0.1.
|
|
70
|
-
dancealgorithm-0.1.
|
|
71
|
-
dancealgorithm-0.1.
|
|
72
|
-
dancealgorithm-0.1.
|
|
73
|
-
dancealgorithm-0.1.
|
|
74
|
-
dancealgorithm-0.1.
|
|
69
|
+
dancealgorithm-0.1.3.dist-info/licenses/LICENSE,sha256=eE9Hj40DYTIwVR4OoOhSuK0sijGtFXOK9_atK1Lz6-A,1083
|
|
70
|
+
dancealgorithm-0.1.3.dist-info/METADATA,sha256=b7xMljFTUDeK9Jfjwsx1jt_VdRgFCfQeRiHEKgcTHnk,6736
|
|
71
|
+
dancealgorithm-0.1.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
72
|
+
dancealgorithm-0.1.3.dist-info/entry_points.txt,sha256=y31VtV8oOHRSQbU5bVTxqQndGGOyRCOaK_Cmd9bUcyI,91
|
|
73
|
+
dancealgorithm-0.1.3.dist-info/top_level.txt,sha256=gst6Jq05KzE4EKbhCzRDSKtchmLdmeYDIqlqry342lM,15
|
|
74
|
+
dancealgorithm-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|