dancealgorithm 0.1.2__tar.gz → 0.1.3__tar.gz
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-0.1.2/src/dancealgorithm.egg-info → dancealgorithm-0.1.3}/PKG-INFO +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/pyproject.toml +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/__init__.py +1 -1
- dancealgorithm-0.1.3/src/dancealgorithm/assignments/LC - Delivery/Solution.java +152 -0
- dancealgorithm-0.1.3/src/dancealgorithm/assignments/LC - Drilling/Solution.java +164 -0
- dancealgorithm-0.1.3/src/dancealgorithm/assignments/LC - Flight/Solution.java +152 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/downloader.py +1 -1
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3/src/dancealgorithm.egg-info}/PKG-INFO +1 -1
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Delivery/Solution.java +0 -261
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Drilling/Solution.java +0 -275
- dancealgorithm-0.1.2/src/dancealgorithm/assignments/LC - Flight/Solution.java +0 -261
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/LICENSE +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/README.md +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/setup.cfg +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/__main__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Campus/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Campus/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Flight/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Flight/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Smart/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA - Smart/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/DA.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Advertisement/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Advertisement/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Electric/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Electric/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Emergency/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK - Emergency/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/FK.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - Classroom/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - Classroom/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - Interference/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - Interference/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - University/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC - University/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/GC.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Cloud/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Cloud/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Disaster/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Disaster/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Hospital/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K - Hospital/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/K.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/LC - Delivery/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/LC - Drilling/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/LC - Flight/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/LC.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Commerce/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Commerce/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Commerce/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Customer/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Customer/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Customer/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Library/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Library/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS - Library/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MS.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Emergency/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Emergency/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Logistics/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Logistics/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Skyconnect/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG - Skyconnect/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/MSG.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Commerce/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Commerce/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Commerce/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Movie/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Movie/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Movie/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Online/Solution.java +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Online/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS - Online/dataset.csv +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/QS.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/assignments/__init__.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/cli.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm/config.py +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm.egg-info/SOURCES.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm.egg-info/dependency_links.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm.egg-info/entry_points.txt +0 -0
- {dancealgorithm-0.1.2 → dancealgorithm-0.1.3}/src/dancealgorithm.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "dancealgorithm"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.3"
|
|
8
8
|
description = "DanceAlgorithm: Instant DAA Algorithms, Lab Problem Sheets, Datasets, and Code Vault."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -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
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
}
|
|
@@ -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
|
|