dancealgorithm 0.1.3__py3-none-any.whl → 0.1.4__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.
@@ -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.3"
12
+ __version__ = "0.1.4"
13
13
 
14
14
  # Import bundled assignments registry and sheets
15
15
  from dancealgorithm.assignments import ASSIGNMENTS_DIR, REGISTRY, SHEETS
@@ -1,152 +1,180 @@
1
- import java.util.*;
2
-
3
- public class Solution {
4
- static final double INF = 1e9;
5
-
6
- static class Node implements Comparable<Node> {
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;
19
- }
20
-
21
- public int compareTo(Node o) {
22
- return Double.compare(this.cost, o.cost);
23
- }
24
- }
25
-
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
- }
31
-
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;
43
- }
44
- }
45
-
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;
53
- }
54
- }
55
- return reduction;
56
- }
57
-
58
- static void solveTSP(double[][] graph, String[] locations) {
59
- int n = graph.length;
60
- double[][] initial = copy(graph);
61
- double rootCost = reduce(initial);
62
-
63
- PriorityQueue<Node> pq = new PriorityQueue<>();
64
- pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
65
-
66
- double bestCost = INF;
67
- List<Integer> bestTour = null;
68
-
69
- while (!pq.isEmpty()) {
70
- Node curr = pq.poll();
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)];
79
- }
80
- tourCost += graph[last][0];
81
- if (tourCost < bestCost) {
82
- bestCost = tourCost;
83
- bestTour = new ArrayList<>(curr.path);
84
- bestTour.add(0);
85
- }
86
- }
87
- continue;
88
- }
89
-
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];
95
-
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;
99
-
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));
103
- }
104
- }
105
- }
106
- }
107
-
108
- if (bestTour == null) {
109
- System.out.println("\nNo complete round-trip route exists.");
110
- return;
111
- }
112
-
113
- System.out.printf("\nOptimal Round-Trip Delivery Cost: Rs. %.2f\n", bestCost);
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
- }
119
-
120
- public static void main(String[] args) {
121
- Scanner sc = new Scanner(System.in);
122
-
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
- }
146
- }
147
- }
148
-
149
- solveTSP(graph, locations);
150
- sc.close();
151
- }
152
- }
1
+ import java.util.Arrays;
2
+ import java.util.PriorityQueue;
3
+
4
+ // Main solution
5
+ public class Solution {
6
+ // O(n! * n^2)
7
+ static final double INF = 1e9;
8
+
9
+ // Search node
10
+ static class Node implements Comparable<Node> {
11
+ double[][] matrix;
12
+ int[] path;
13
+ int level;
14
+ double cost;
15
+ int city;
16
+
17
+ Node(double[][] mat, int[] path, int level, double cost, int city) {
18
+ int n = mat.length;
19
+ this.matrix = new double[n][n];
20
+ for (int i = 0; i < n; i++) {
21
+ System.arraycopy(mat[i], 0, this.matrix[i], 0, n);
22
+ }
23
+ this.path = Arrays.copyOf(path, path.length);
24
+ this.level = level;
25
+ this.cost = cost;
26
+ this.city = city;
27
+ }
28
+
29
+ @Override
30
+ public int compareTo(Node other) {
31
+ return Double.compare(this.cost, other.cost);
32
+ }
33
+ }
34
+
35
+ // Matrix reduction
36
+ public static double reduceMatrix(double[][] mat, int n) {
37
+ double reduction = 0;
38
+
39
+ for (int i = 0; i < n; i++) {
40
+ double rmin = INF;
41
+ for (int j = 0; j < n; j++) {
42
+ if (mat[i][j] < rmin) rmin = mat[i][j];
43
+ }
44
+ if (rmin > 0 && rmin < INF) {
45
+ reduction += rmin;
46
+ for (int j = 0; j < n; j++) {
47
+ if (mat[i][j] < INF) mat[i][j] -= rmin;
48
+ }
49
+ }
50
+ }
51
+
52
+ for (int j = 0; j < n; j++) {
53
+ double cmin = INF;
54
+ for (int i = 0; i < n; i++) {
55
+ if (mat[i][j] < cmin) cmin = mat[i][j];
56
+ }
57
+ if (cmin > 0 && cmin < INF) {
58
+ reduction += cmin;
59
+ for (int i = 0; i < n; i++) {
60
+ if (mat[i][j] < INF) mat[i][j] -= cmin;
61
+ }
62
+ }
63
+ }
64
+
65
+ return reduction;
66
+ }
67
+
68
+ // Branch and bound TSP
69
+ public static void solveLCBB_TSP(double[][] costMatrix, String[] locations) {
70
+ int n = costMatrix.length;
71
+
72
+ double[][] initialMat = new double[n][n];
73
+ for (int i = 0; i < n; i++) {
74
+ System.arraycopy(costMatrix[i], 0, initialMat[i], 0, n);
75
+ }
76
+
77
+ double rootCost = reduceMatrix(initialMat, n);
78
+
79
+ PriorityQueue<Node> pq = new PriorityQueue<>();
80
+ int[] rootPath = {0};
81
+ pq.add(new Node(initialMat, rootPath, 1, rootCost, 0));
82
+
83
+ double bestCost = INF;
84
+ int[] bestTour = null;
85
+
86
+ while (!pq.isEmpty()) {
87
+ Node curr = pq.poll();
88
+
89
+ if (curr.cost >= bestCost) {
90
+ continue;
91
+ }
92
+
93
+ if (curr.level == n) {
94
+ int lastCity = curr.city;
95
+ if (costMatrix[lastCity][0] < INF) {
96
+ double tourCost = 0;
97
+ for (int i = 0; i < n - 1; i++) {
98
+ tourCost += costMatrix[curr.path[i]][curr.path[i + 1]];
99
+ }
100
+ tourCost += costMatrix[lastCity][0];
101
+
102
+ if (tourCost < bestCost) {
103
+ bestCost = tourCost;
104
+ bestTour = new int[n + 1];
105
+ System.arraycopy(curr.path, 0, bestTour, 0, n);
106
+ bestTour[n] = 0;
107
+ }
108
+ }
109
+ continue;
110
+ }
111
+
112
+ int u = curr.city;
113
+
114
+ for (int v = 0; v < n; v++) {
115
+ boolean visited = false;
116
+ for (int p : curr.path) {
117
+ if (p == v) { visited = true; break; }
118
+ }
119
+
120
+ if (!visited && curr.matrix[u][v] < INF) {
121
+ double[][] childMat = new double[n][n];
122
+ for (int i = 0; i < n; i++) {
123
+ System.arraycopy(curr.matrix[i], 0, childMat[i], 0, n);
124
+ }
125
+ double edgeVal = curr.matrix[u][v];
126
+
127
+ for (int j = 0; j < n; j++) childMat[u][j] = INF;
128
+ for (int i = 0; i < n; i++) childMat[i][v] = INF;
129
+ if (curr.level < n - 1) {
130
+ childMat[v][0] = INF;
131
+ }
132
+
133
+ double reduction = reduceMatrix(childMat, n);
134
+ double childCost = curr.cost + edgeVal + reduction;
135
+
136
+ if (childCost < bestCost) {
137
+ int[] childPath = Arrays.copyOf(curr.path, curr.path.length + 1);
138
+ childPath[childPath.length - 1] = v;
139
+ pq.add(new Node(childMat, childPath, curr.level + 1, childCost, v));
140
+ }
141
+ }
142
+ }
143
+ }
144
+
145
+ System.out.printf("%nOptimal Round-Trip Delivery Cost: Rs. %.2f%n", bestCost);
146
+ System.out.print("Optimal Delivery Tour Sequence : ");
147
+ if (bestTour != null) {
148
+ for (int i = 0; i <= n; i++) {
149
+ System.out.print(locations[bestTour[i]]);
150
+ if (i < n) System.out.print(" -> ");
151
+ }
152
+ }
153
+ System.out.println();
154
+ }
155
+
156
+ // Driver code
157
+ public static void main(String args[]) {
158
+ System.out.println("=======================================================================");
159
+ System.out.println("PS 22: SwiftShip Delivery Route TSP - LC Branch & Bound (Java)");
160
+ System.out.println("=======================================================================");
161
+
162
+ String locations[] = {
163
+ "Central Depot (Connaught Place)",
164
+ "East Hub (Anand Vihar)",
165
+ "North Hub (Rohini)",
166
+ "West Hub (Dwarka)",
167
+ "South Hub (Saket)"
168
+ };
169
+
170
+ double costMatrix[][] = {
171
+ {INF, 20, 30, 10, 11},
172
+ {15, INF, 16, 4, 2},
173
+ {3, 5, INF, 2, 4},
174
+ {19, 6, 18, INF, 3},
175
+ {16, 4, 7, 16, INF}
176
+ };
177
+
178
+ solveLCBB_TSP(costMatrix, locations);
179
+ }
180
+ }
@@ -1,164 +1,197 @@
1
- import java.util.*;
2
-
3
- public class Solution {
4
- static final double INF = 1e9;
5
-
6
- static class Hole {
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;
13
- }
14
- }
15
-
16
- static class Node implements Comparable<Node> {
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;
29
- }
30
-
31
- public int compareTo(Node o) {
32
- return Double.compare(this.cost, o.cost);
33
- }
34
- }
35
-
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
- }
41
-
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;
53
- }
54
- }
55
-
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;
63
- }
64
- }
65
- return reduction;
66
- }
67
-
68
- static void solveTSP(double[][] graph, Hole[] holes) {
69
- int n = graph.length;
70
- double[][] initial = copy(graph);
71
- double rootCost = reduce(initial);
72
-
73
- PriorityQueue<Node> pq = new PriorityQueue<>();
74
- pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
75
-
76
- double bestCost = INF;
77
- List<Integer> bestTour = null;
78
-
79
- while (!pq.isEmpty()) {
80
- Node curr = pq.poll();
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)];
89
- }
90
- tourCost += graph[last][0];
91
- if (tourCost < bestCost) {
92
- bestCost = tourCost;
93
- bestTour = new ArrayList<>(curr.path);
94
- bestTour.add(0);
95
- }
96
- }
97
- continue;
98
- }
99
-
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];
105
-
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;
109
-
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));
113
- }
114
- }
115
- }
116
- }
117
-
118
- if (bestTour == null) {
119
- System.out.println("\nNo complete drilling tour exists.");
120
- return;
121
- }
122
-
123
- System.out.printf("\nTotal Drill Head Travel Distance: %.2f mm\n", bestCost);
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();
136
-
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
- }
149
-
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
- }
158
- }
159
- }
160
-
161
- solveTSP(graph, holes);
162
- sc.close();
163
- }
164
- }
1
+ import java.util.Arrays;
2
+ import java.util.PriorityQueue;
3
+
4
+ // Main solution
5
+ public class Solution {
6
+ // O(n! * n^2)
7
+ static final double INF = 1e9;
8
+
9
+ static class Hole {
10
+ double x, y;
11
+ String label;
12
+
13
+ Hole(double x, double y, String label) {
14
+ this.x = x;
15
+ this.y = y;
16
+ this.label = label;
17
+ }
18
+ }
19
+
20
+ // Search node
21
+ static class Node implements Comparable<Node> {
22
+ double[][] matrix;
23
+ int[] path;
24
+ int level;
25
+ double cost;
26
+ int city;
27
+
28
+ Node(double[][] mat, int[] path, int level, double cost, int city) {
29
+ int n = mat.length;
30
+ this.matrix = new double[n][n];
31
+ for (int i = 0; i < n; i++) {
32
+ System.arraycopy(mat[i], 0, this.matrix[i], 0, n);
33
+ }
34
+ this.path = Arrays.copyOf(path, path.length);
35
+ this.level = level;
36
+ this.cost = cost;
37
+ this.city = city;
38
+ }
39
+
40
+ @Override
41
+ public int compareTo(Node other) {
42
+ return Double.compare(this.cost, other.cost);
43
+ }
44
+ }
45
+
46
+ // Matrix reduction
47
+ public static double reduceMatrix(double[][] mat, int n) {
48
+ double reduction = 0;
49
+
50
+ for (int i = 0; i < n; i++) {
51
+ double rmin = INF;
52
+ for (int j = 0; j < n; j++) {
53
+ if (mat[i][j] < rmin) rmin = mat[i][j];
54
+ }
55
+ if (rmin > 0 && rmin < INF) {
56
+ reduction += rmin;
57
+ for (int j = 0; j < n; j++) {
58
+ if (mat[i][j] < INF) mat[i][j] -= rmin;
59
+ }
60
+ }
61
+ }
62
+
63
+ for (int j = 0; j < n; j++) {
64
+ double cmin = INF;
65
+ for (int i = 0; i < n; i++) {
66
+ if (mat[i][j] < cmin) cmin = mat[i][j];
67
+ }
68
+ if (cmin > 0 && cmin < INF) {
69
+ reduction += cmin;
70
+ for (int i = 0; i < n; i++) {
71
+ if (mat[i][j] < INF) mat[i][j] -= cmin;
72
+ }
73
+ }
74
+ }
75
+
76
+ return reduction;
77
+ }
78
+
79
+ // Branch and bound TSP
80
+ public static void solveLCBB_TSP(double[][] costMatrix, Hole[] holes) {
81
+ int n = costMatrix.length;
82
+
83
+ double[][] initialMat = new double[n][n];
84
+ for (int i = 0; i < n; i++) {
85
+ System.arraycopy(costMatrix[i], 0, initialMat[i], 0, n);
86
+ }
87
+
88
+ double rootCost = reduceMatrix(initialMat, n);
89
+
90
+ PriorityQueue<Node> pq = new PriorityQueue<>();
91
+ int[] rootPath = {0};
92
+ pq.add(new Node(initialMat, rootPath, 1, rootCost, 0));
93
+
94
+ double bestCost = INF;
95
+ int[] bestTour = null;
96
+
97
+ while (!pq.isEmpty()) {
98
+ Node curr = pq.poll();
99
+
100
+ if (curr.cost >= bestCost) {
101
+ continue;
102
+ }
103
+
104
+ if (curr.level == n) {
105
+ int lastCity = curr.city;
106
+ if (costMatrix[lastCity][0] < INF) {
107
+ double tourCost = 0;
108
+ for (int i = 0; i < n - 1; i++) {
109
+ tourCost += costMatrix[curr.path[i]][curr.path[i + 1]];
110
+ }
111
+ tourCost += costMatrix[lastCity][0];
112
+
113
+ if (tourCost < bestCost) {
114
+ bestCost = tourCost;
115
+ bestTour = new int[n + 1];
116
+ System.arraycopy(curr.path, 0, bestTour, 0, n);
117
+ bestTour[n] = 0;
118
+ }
119
+ }
120
+ continue;
121
+ }
122
+
123
+ int u = curr.city;
124
+
125
+ for (int v = 0; v < n; v++) {
126
+ boolean visited = false;
127
+ for (int p : curr.path) {
128
+ if (p == v) { visited = true; break; }
129
+ }
130
+
131
+ if (!visited && curr.matrix[u][v] < INF) {
132
+ double[][] childMat = new double[n][n];
133
+ for (int i = 0; i < n; i++) {
134
+ System.arraycopy(curr.matrix[i], 0, childMat[i], 0, n);
135
+ }
136
+ double edgeVal = curr.matrix[u][v];
137
+
138
+ for (int j = 0; j < n; j++) childMat[u][j] = INF;
139
+ for (int i = 0; i < n; i++) childMat[i][v] = INF;
140
+ if (curr.level < n - 1) {
141
+ childMat[v][0] = INF;
142
+ }
143
+
144
+ double reduction = reduceMatrix(childMat, n);
145
+ double childCost = curr.cost + edgeVal + reduction;
146
+
147
+ if (childCost < bestCost) {
148
+ int[] childPath = Arrays.copyOf(curr.path, curr.path.length + 1);
149
+ childPath[childPath.length - 1] = v;
150
+ pq.add(new Node(childMat, childPath, curr.level + 1, childCost, v));
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ System.out.printf("%nTotal Drill Head Travel Distance : %.2f mm%n", bestCost);
157
+ System.out.print("Optimal Drilling Toolpath Sequence : ");
158
+ if (bestTour != null) {
159
+ for (int i = 0; i <= n; i++) {
160
+ System.out.print(holes[bestTour[i]].label);
161
+ if (i < n) System.out.print(" -> ");
162
+ }
163
+ }
164
+ System.out.println();
165
+ }
166
+
167
+ // Driver code
168
+ public static void main(String args[]) {
169
+ System.out.println("=======================================================================");
170
+ System.out.println("PS 24: CircuitPro PCB CNC Drilling - LC Branch & Bound (Java)");
171
+ System.out.println("=======================================================================");
172
+
173
+ Hole holes[] = {
174
+ new Hole(0.0, 0.0, "Home (0,0)"),
175
+ new Hole(10.0, 45.0, "Hole-1 (10,45)"),
176
+ new Hole(25.0, 80.0, "Hole-2 (25,80)"),
177
+ new Hole(60.0, 75.0, "Hole-3 (60,75)"),
178
+ new Hole(85.0, 30.0, "Hole-4 (85,30)"),
179
+ new Hole(40.0, 10.0, "Hole-5 (40,10)")
180
+ };
181
+
182
+ int n = holes.length;
183
+ double costMatrix[][] = new double[n][n];
184
+
185
+ for (int i = 0; i < n; i++) {
186
+ for (int j = 0; j < n; j++) {
187
+ if (i == j) {
188
+ costMatrix[i][j] = INF;
189
+ } else {
190
+ costMatrix[i][j] = Math.hypot(holes[i].x - holes[j].x, holes[i].y - holes[j].y);
191
+ }
192
+ }
193
+ }
194
+
195
+ solveLCBB_TSP(costMatrix, holes);
196
+ }
197
+ }
@@ -1,152 +1,180 @@
1
- import java.util.*;
2
-
3
- public class Solution {
4
- static final double INF = 1e9;
5
-
6
- static class Node implements Comparable<Node> {
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;
19
- }
20
-
21
- public int compareTo(Node o) {
22
- return Double.compare(this.cost, o.cost);
23
- }
24
- }
25
-
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
- }
31
-
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;
43
- }
44
- }
45
-
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;
53
- }
54
- }
55
- return reduction;
56
- }
57
-
58
- static void solveTSP(double[][] graph, String[] airports) {
59
- int n = graph.length;
60
- double[][] initial = copy(graph);
61
- double rootCost = reduce(initial);
62
-
63
- PriorityQueue<Node> pq = new PriorityQueue<>();
64
- pq.add(new Node(initial, new ArrayList<>(), 0, 1, rootCost));
65
-
66
- double bestCost = INF;
67
- List<Integer> bestTour = null;
68
-
69
- while (!pq.isEmpty()) {
70
- Node curr = pq.poll();
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)];
79
- }
80
- tourCost += graph[last][0];
81
- if (tourCost < bestCost) {
82
- bestCost = tourCost;
83
- bestTour = new ArrayList<>(curr.path);
84
- bestTour.add(0);
85
- }
86
- }
87
- continue;
88
- }
89
-
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];
95
-
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;
99
-
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));
103
- }
104
- }
105
- }
106
- }
107
-
108
- if (bestTour == null) {
109
- System.out.println("\nNo complete round-trip flight route exists.");
110
- return;
111
- }
112
-
113
- System.out.printf("\nMinimum Total Aircraft Operational Cost: Rs. %.2f Lakhs\n", bestCost);
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
- }
119
-
120
- public static void main(String[] args) {
121
- Scanner sc = new Scanner(System.in);
122
-
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
- }
146
- }
147
- }
148
-
149
- solveTSP(graph, airports);
150
- sc.close();
151
- }
152
- }
1
+ import java.util.Arrays;
2
+ import java.util.PriorityQueue;
3
+
4
+ // Main solution
5
+ public class Solution {
6
+ // O(n! * n^2)
7
+ static final double INF = 1e9;
8
+
9
+ // Search node
10
+ static class Node implements Comparable<Node> {
11
+ double[][] matrix;
12
+ int[] path;
13
+ int level;
14
+ double cost;
15
+ int city;
16
+
17
+ Node(double[][] mat, int[] path, int level, double cost, int city) {
18
+ int n = mat.length;
19
+ this.matrix = new double[n][n];
20
+ for (int i = 0; i < n; i++) {
21
+ System.arraycopy(mat[i], 0, this.matrix[i], 0, n);
22
+ }
23
+ this.path = Arrays.copyOf(path, path.length);
24
+ this.level = level;
25
+ this.cost = cost;
26
+ this.city = city;
27
+ }
28
+
29
+ @Override
30
+ public int compareTo(Node other) {
31
+ return Double.compare(this.cost, other.cost);
32
+ }
33
+ }
34
+
35
+ // Matrix reduction
36
+ public static double reduceMatrix(double[][] mat, int n) {
37
+ double reduction = 0;
38
+
39
+ for (int i = 0; i < n; i++) {
40
+ double rmin = INF;
41
+ for (int j = 0; j < n; j++) {
42
+ if (mat[i][j] < rmin) rmin = mat[i][j];
43
+ }
44
+ if (rmin > 0 && rmin < INF) {
45
+ reduction += rmin;
46
+ for (int j = 0; j < n; j++) {
47
+ if (mat[i][j] < INF) mat[i][j] -= rmin;
48
+ }
49
+ }
50
+ }
51
+
52
+ for (int j = 0; j < n; j++) {
53
+ double cmin = INF;
54
+ for (int i = 0; i < n; i++) {
55
+ if (mat[i][j] < cmin) cmin = mat[i][j];
56
+ }
57
+ if (cmin > 0 && cmin < INF) {
58
+ reduction += cmin;
59
+ for (int i = 0; i < n; i++) {
60
+ if (mat[i][j] < INF) mat[i][j] -= cmin;
61
+ }
62
+ }
63
+ }
64
+
65
+ return reduction;
66
+ }
67
+
68
+ // Branch and bound TSP
69
+ public static void solveLCBB_TSP(double[][] costMatrix, String[] airports) {
70
+ int n = costMatrix.length;
71
+
72
+ double[][] initialMat = new double[n][n];
73
+ for (int i = 0; i < n; i++) {
74
+ System.arraycopy(costMatrix[i], 0, initialMat[i], 0, n);
75
+ }
76
+
77
+ double rootCost = reduceMatrix(initialMat, n);
78
+
79
+ PriorityQueue<Node> pq = new PriorityQueue<>();
80
+ int[] rootPath = {0};
81
+ pq.add(new Node(initialMat, rootPath, 1, rootCost, 0));
82
+
83
+ double bestCost = INF;
84
+ int[] bestTour = null;
85
+
86
+ while (!pq.isEmpty()) {
87
+ Node curr = pq.poll();
88
+
89
+ if (curr.cost >= bestCost) {
90
+ continue;
91
+ }
92
+
93
+ if (curr.level == n) {
94
+ int lastCity = curr.city;
95
+ if (costMatrix[lastCity][0] < INF) {
96
+ double tourCost = 0;
97
+ for (int i = 0; i < n - 1; i++) {
98
+ tourCost += costMatrix[curr.path[i]][curr.path[i + 1]];
99
+ }
100
+ tourCost += costMatrix[lastCity][0];
101
+
102
+ if (tourCost < bestCost) {
103
+ bestCost = tourCost;
104
+ bestTour = new int[n + 1];
105
+ System.arraycopy(curr.path, 0, bestTour, 0, n);
106
+ bestTour[n] = 0;
107
+ }
108
+ }
109
+ continue;
110
+ }
111
+
112
+ int u = curr.city;
113
+
114
+ for (int v = 0; v < n; v++) {
115
+ boolean visited = false;
116
+ for (int p : curr.path) {
117
+ if (p == v) { visited = true; break; }
118
+ }
119
+
120
+ if (!visited && curr.matrix[u][v] < INF) {
121
+ double[][] childMat = new double[n][n];
122
+ for (int i = 0; i < n; i++) {
123
+ System.arraycopy(curr.matrix[i], 0, childMat[i], 0, n);
124
+ }
125
+ double edgeVal = curr.matrix[u][v];
126
+
127
+ for (int j = 0; j < n; j++) childMat[u][j] = INF;
128
+ for (int i = 0; i < n; i++) childMat[i][v] = INF;
129
+ if (curr.level < n - 1) {
130
+ childMat[v][0] = INF;
131
+ }
132
+
133
+ double reduction = reduceMatrix(childMat, n);
134
+ double childCost = curr.cost + edgeVal + reduction;
135
+
136
+ if (childCost < bestCost) {
137
+ int[] childPath = Arrays.copyOf(curr.path, curr.path.length + 1);
138
+ childPath[childPath.length - 1] = v;
139
+ pq.add(new Node(childMat, childPath, curr.level + 1, childCost, v));
140
+ }
141
+ }
142
+ }
143
+ }
144
+
145
+ System.out.printf("%nTotal Operational Aircraft Fuel & Gate Cost: Rs. %.2f (Lakhs)%n", bestCost);
146
+ System.out.print("Optimal Aircraft Tour Circuit : ");
147
+ if (bestTour != null) {
148
+ for (int i = 0; i <= n; i++) {
149
+ System.out.print(airports[bestTour[i]]);
150
+ if (i < n) System.out.print(" -> ");
151
+ }
152
+ }
153
+ System.out.println();
154
+ }
155
+
156
+ // Driver code
157
+ public static void main(String args[]) {
158
+ System.out.println("=======================================================================");
159
+ System.out.println("PS 23: AeroConnect Aircraft Circuit Tour - LC Branch & Bound (Java)");
160
+ System.out.println("=======================================================================");
161
+
162
+ String airports[] = {
163
+ "Delhi (DL)",
164
+ "Mumbai (MH)",
165
+ "Pune (MH)",
166
+ "Ahmedabad (GJ)",
167
+ "Bengaluru (KA)"
168
+ };
169
+
170
+ double costMatrix[][] = {
171
+ {INF, 12, 29, 45, 38},
172
+ {14, INF, 19, 36, 42},
173
+ {30, 21, INF, 18, 55},
174
+ {48, 38, 17, INF, 50},
175
+ {39, 40, 52, 49, INF}
176
+ };
177
+
178
+ solveLCBB_TSP(costMatrix, airports);
179
+ }
180
+ }
@@ -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.3"
18
+ USER_AGENT = "DanceAlgorithm-CLI/0.1.4"
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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dancealgorithm
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: DanceAlgorithm: Instant DAA Algorithms, Lab Problem Sheets, Datasets, and Code Vault.
5
5
  Author: DanceAlgorithm Maintainer
6
6
  License-Expression: MIT
@@ -1,8 +1,8 @@
1
- dancealgorithm/__init__.py,sha256=-Ab304-q9HMimR-_dcbOqZOdwhf2Dnf6G8AqjvHsPpw,4102
1
+ dancealgorithm/__init__.py,sha256=Y-KLgv0bCO4WA5Q_Zr5W6xzbvCWHgBIIf8OOcQn6TVk,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=g0GJ2YvKUttHoxWGmSOTcajD5RuD8CtO4vbt3w-M5NI,10914
5
+ dancealgorithm/downloader.py,sha256=kQUS0YTgv7g3TdB1TWTdFtnWzO8p51C6-Y3QlCBZiH0,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=qfWcV0uSTR29clF9Dd3B3H293wJK7tyNpqfd4WU-8rM,5046
39
+ dancealgorithm/assignments/LC - Delivery/Solution.java,sha256=179RRi8_wJLsVi1YjkrOeA2dnzrJILvSfi-1Vil_5qA,6080
40
40
  dancealgorithm/assignments/LC - Delivery/__init__.py,sha256=mG6F5WJxD_6CpQxdvuz6ktkQdyefcHiioBf8sVFNnwc,1245
41
- dancealgorithm/assignments/LC - Drilling/Solution.java,sha256=8Tg973jHIay0vOF4jargR9JreoaFfWzsU-75ZOLvXYc,5314
41
+ dancealgorithm/assignments/LC - Drilling/Solution.java,sha256=H6u8IsYGBXwJbk0IduW2Tya6XGeuCyGPqTgwWNW2vkg,6609
42
42
  dancealgorithm/assignments/LC - Drilling/__init__.py,sha256=m6h04f3RWoA4Bwp7V91s-B_pKtVdVbKdQ_wuNCjLCwM,1245
43
- dancealgorithm/assignments/LC - Flight/Solution.java,sha256=9bNOvZYzD3qPFl1lYbabIiloSUjDt3N74_V1RgwvFRA,5066
43
+ dancealgorithm/assignments/LC - Flight/Solution.java,sha256=xVKAGAALyPWNsAN3jbW73sfvrfyuw20ppq4QOW9tpuQ,6067
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.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,,
69
+ dancealgorithm-0.1.4.dist-info/licenses/LICENSE,sha256=eE9Hj40DYTIwVR4OoOhSuK0sijGtFXOK9_atK1Lz6-A,1083
70
+ dancealgorithm-0.1.4.dist-info/METADATA,sha256=NpfigU7uCkr3_3d2dPpACSLzkqz3CSkDEsdE1W3JFmU,6736
71
+ dancealgorithm-0.1.4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
72
+ dancealgorithm-0.1.4.dist-info/entry_points.txt,sha256=y31VtV8oOHRSQbU5bVTxqQndGGOyRCOaK_Cmd9bUcyI,91
73
+ dancealgorithm-0.1.4.dist-info/top_level.txt,sha256=gst6Jq05KzE4EKbhCzRDSKtchmLdmeYDIqlqry342lM,15
74
+ dancealgorithm-0.1.4.dist-info/RECORD,,