dancealgorithm 0.1.0__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 +128 -0
- dancealgorithm/__main__.py +8 -0
- dancealgorithm/assignments/DA - Campus/Solution.java +198 -0
- dancealgorithm/assignments/DA - Campus/__init__.py +34 -0
- dancealgorithm/assignments/DA - Flight/Solution.java +204 -0
- dancealgorithm/assignments/DA - Flight/__init__.py +34 -0
- dancealgorithm/assignments/DA - Smart/Solution.java +220 -0
- dancealgorithm/assignments/DA - Smart/__init__.py +34 -0
- dancealgorithm/assignments/DA.txt +29 -0
- dancealgorithm/assignments/FK - Advertisement/Solution.java +141 -0
- dancealgorithm/assignments/FK - Advertisement/__init__.py +34 -0
- dancealgorithm/assignments/FK - Electric/Solution.java +168 -0
- dancealgorithm/assignments/FK - Electric/__init__.py +34 -0
- dancealgorithm/assignments/FK - Emergency/Solution.java +159 -0
- dancealgorithm/assignments/FK - Emergency/__init__.py +34 -0
- dancealgorithm/assignments/FK.txt +26 -0
- dancealgorithm/assignments/GC - Classroom/Solution.java +87 -0
- dancealgorithm/assignments/GC - Classroom/__init__.py +34 -0
- dancealgorithm/assignments/GC - Interference/Solution.java +88 -0
- dancealgorithm/assignments/GC - Interference/__init__.py +34 -0
- dancealgorithm/assignments/GC - University/Solution.java +88 -0
- dancealgorithm/assignments/GC - University/__init__.py +34 -0
- dancealgorithm/assignments/GC.txt +25 -0
- dancealgorithm/assignments/K - Cloud/Solution.java +115 -0
- dancealgorithm/assignments/K - Cloud/__init__.py +34 -0
- dancealgorithm/assignments/K - Disaster/Solution.java +116 -0
- dancealgorithm/assignments/K - Disaster/__init__.py +34 -0
- dancealgorithm/assignments/K - Hospital/Solution.java +115 -0
- dancealgorithm/assignments/K - Hospital/__init__.py +34 -0
- dancealgorithm/assignments/K.txt +38 -0
- dancealgorithm/assignments/LC - Delivery/Solution.java +180 -0
- dancealgorithm/assignments/LC - Delivery/__init__.py +34 -0
- dancealgorithm/assignments/LC - Drilling/Solution.java +197 -0
- dancealgorithm/assignments/LC - Drilling/__init__.py +34 -0
- dancealgorithm/assignments/LC - Flight/Solution.java +180 -0
- dancealgorithm/assignments/LC - Flight/__init__.py +34 -0
- dancealgorithm/assignments/LC.txt +31 -0
- dancealgorithm/assignments/MS - Commerce/Solution.java +162 -0
- dancealgorithm/assignments/MS - Commerce/__init__.py +34 -0
- dancealgorithm/assignments/MS - Commerce/dataset.csv +16 -0
- dancealgorithm/assignments/MS - Customer/Solution.java +144 -0
- dancealgorithm/assignments/MS - Customer/__init__.py +34 -0
- dancealgorithm/assignments/MS - Customer/dataset.csv +16 -0
- dancealgorithm/assignments/MS - Library/Solution.java +162 -0
- dancealgorithm/assignments/MS - Library/__init__.py +34 -0
- dancealgorithm/assignments/MS - Library/dataset.csv +16 -0
- dancealgorithm/assignments/MS.txt +32 -0
- dancealgorithm/assignments/MSG - Emergency/Solution.java +211 -0
- dancealgorithm/assignments/MSG - Emergency/__init__.py +34 -0
- dancealgorithm/assignments/MSG - Logistics/Solution.java +189 -0
- dancealgorithm/assignments/MSG - Logistics/__init__.py +34 -0
- dancealgorithm/assignments/MSG - Skyconnect/Solution.java +204 -0
- dancealgorithm/assignments/MSG - Skyconnect/__init__.py +34 -0
- dancealgorithm/assignments/MSG.txt +28 -0
- dancealgorithm/assignments/QS - Commerce/Solution.java +147 -0
- dancealgorithm/assignments/QS - Commerce/__init__.py +34 -0
- dancealgorithm/assignments/QS - Commerce/dataset.csv +16 -0
- dancealgorithm/assignments/QS - Movie/Solution.java +148 -0
- dancealgorithm/assignments/QS - Movie/__init__.py +34 -0
- dancealgorithm/assignments/QS - Movie/dataset.csv +16 -0
- dancealgorithm/assignments/QS - Online/Solution.java +147 -0
- dancealgorithm/assignments/QS - Online/__init__.py +34 -0
- dancealgorithm/assignments/QS - Online/dataset.csv +16 -0
- dancealgorithm/assignments/QS.txt +33 -0
- dancealgorithm/assignments/__init__.py +65 -0
- dancealgorithm/cli.py +349 -0
- dancealgorithm/config.py +125 -0
- dancealgorithm/downloader.py +330 -0
- dancealgorithm-0.1.0.dist-info/METADATA +199 -0
- dancealgorithm-0.1.0.dist-info/RECORD +74 -0
- dancealgorithm-0.1.0.dist-info/WHEEL +5 -0
- dancealgorithm-0.1.0.dist-info/entry_points.txt +3 -0
- dancealgorithm-0.1.0.dist-info/licenses/LICENSE +22 -0
- dancealgorithm-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
public class Solution {
|
|
5
|
+
// 1. Main Function
|
|
6
|
+
public static void main(String args[]) {
|
|
7
|
+
Scanner sc = new Scanner(System.in);
|
|
8
|
+
|
|
9
|
+
System.out.print("Enter server RAM capacity (GB): ");
|
|
10
|
+
int iCapacity = sc.nextInt();
|
|
11
|
+
|
|
12
|
+
System.out.print("Enter number of VM requests: ");
|
|
13
|
+
int iSize = sc.nextInt();
|
|
14
|
+
|
|
15
|
+
VirtualMachine vms[] = new VirtualMachine[iSize];
|
|
16
|
+
|
|
17
|
+
// 2. Get Virtual Machine Details
|
|
18
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
19
|
+
sc.nextLine();
|
|
20
|
+
|
|
21
|
+
System.out.println("\nEnter details for VM " + (iCnt + 1));
|
|
22
|
+
|
|
23
|
+
System.out.print("Instance Type: ");
|
|
24
|
+
String sType = sc.nextLine();
|
|
25
|
+
|
|
26
|
+
System.out.print("RAM Required (GB): ");
|
|
27
|
+
int iRAM = sc.nextInt();
|
|
28
|
+
|
|
29
|
+
System.out.print("Monthly Revenue ($): ");
|
|
30
|
+
int iRevenue = sc.nextInt();
|
|
31
|
+
|
|
32
|
+
vms[iCnt] = new VirtualMachine(sType, iRAM, iRevenue);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 3. Apply Knapsack Algorithm
|
|
36
|
+
Knapsack(vms, iCapacity);
|
|
37
|
+
|
|
38
|
+
sc.close();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 4. Virtual Machine Entity
|
|
42
|
+
static class VirtualMachine {
|
|
43
|
+
String sType;
|
|
44
|
+
int iRAM, iRevenue;
|
|
45
|
+
|
|
46
|
+
VirtualMachine(String sType, int iRAM, int iRevenue) {
|
|
47
|
+
this.sType = sType;
|
|
48
|
+
this.iRAM = iRAM;
|
|
49
|
+
this.iRevenue = iRevenue;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 5. Knapsack Dynamic Programming
|
|
54
|
+
static void Knapsack(VirtualMachine vms[], int iCapacity) {
|
|
55
|
+
int iSize = vms.length;
|
|
56
|
+
int iDP[][] = new int[iSize + 1][iCapacity + 1];
|
|
57
|
+
|
|
58
|
+
// 6. Calculate Maximum Revenue
|
|
59
|
+
for (int iCnt = 1; iCnt <= iSize; iCnt++) {
|
|
60
|
+
for (int iW = 1; iW <= iCapacity; iW++) {
|
|
61
|
+
if (vms[iCnt - 1].iRAM <= iW) {
|
|
62
|
+
iDP[iCnt][iW] = Math.max(
|
|
63
|
+
vms[iCnt - 1].iRevenue +
|
|
64
|
+
iDP[iCnt - 1][iW - vms[iCnt - 1].iRAM],
|
|
65
|
+
|
|
66
|
+
iDP[iCnt - 1][iW]);
|
|
67
|
+
} else {
|
|
68
|
+
iDP[iCnt][iW] = iDP[iCnt - 1][iW];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 7. Find Selected Virtual Machines
|
|
74
|
+
boolean bSelected[] = new boolean[iSize];
|
|
75
|
+
int iW = iCapacity;
|
|
76
|
+
|
|
77
|
+
for (int iCnt = iSize; iCnt > 0; iCnt--) {
|
|
78
|
+
if (iDP[iCnt][iW] != iDP[iCnt - 1][iW]) {
|
|
79
|
+
bSelected[iCnt - 1] = true;
|
|
80
|
+
iW -= vms[iCnt - 1].iRAM;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 8. Display Selected Virtual Machines
|
|
85
|
+
int iTotalRAM = 0;
|
|
86
|
+
|
|
87
|
+
System.out.println("\nCloud Server Virtual Machine Allocation:");
|
|
88
|
+
System.out.println("---------------------------------------------------------------");
|
|
89
|
+
|
|
90
|
+
System.out.printf("%-28s %-12s %-15s%n",
|
|
91
|
+
"VM Instance Type", "RAM (GB)", "Revenue ($/mo)");
|
|
92
|
+
|
|
93
|
+
System.out.println("---------------------------------------------------------------");
|
|
94
|
+
|
|
95
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
96
|
+
if (bSelected[iCnt]) {
|
|
97
|
+
System.out.printf("%-28s %-12d %-15d%n",
|
|
98
|
+
vms[iCnt].sType,
|
|
99
|
+
vms[iCnt].iRAM,
|
|
100
|
+
vms[iCnt].iRevenue);
|
|
101
|
+
|
|
102
|
+
iTotalRAM += vms[iCnt].iRAM;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
System.out.println("---------------------------------------------------------------");
|
|
107
|
+
System.out.println("Total RAM Provisioned: " +
|
|
108
|
+
iTotalRAM + " GB / " + iCapacity + " GB");
|
|
109
|
+
|
|
110
|
+
System.out.println("Maximum Monthly Revenue: $" +
|
|
111
|
+
iDP[iSize][iCapacity]);
|
|
112
|
+
|
|
113
|
+
System.out.println("---------------------------------------------------------------");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "K - Cloud"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Function
|
|
5
|
+
public class Solution {
|
|
6
|
+
public static void main(String args[]) {
|
|
7
|
+
Scanner sc = new Scanner(System.in);
|
|
8
|
+
|
|
9
|
+
// 2. Get Truck Capacity
|
|
10
|
+
System.out.print("Enter truck capacity (kg): ");
|
|
11
|
+
int iCapacity = sc.nextInt();
|
|
12
|
+
|
|
13
|
+
System.out.print("Enter number of relief items: ");
|
|
14
|
+
int iSize = sc.nextInt();
|
|
15
|
+
|
|
16
|
+
ReliefSupply items[] = new ReliefSupply[iSize];
|
|
17
|
+
|
|
18
|
+
// 3. Get Item Details
|
|
19
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
20
|
+
sc.nextLine();
|
|
21
|
+
|
|
22
|
+
System.out.println("\nEnter details for Item " + (iCnt + 1));
|
|
23
|
+
|
|
24
|
+
System.out.print("Item Name: ");
|
|
25
|
+
String sName = sc.nextLine();
|
|
26
|
+
|
|
27
|
+
System.out.print("Weight (kg): ");
|
|
28
|
+
int iWeight = sc.nextInt();
|
|
29
|
+
|
|
30
|
+
System.out.print("Utility Value: ");
|
|
31
|
+
int iValue = sc.nextInt();
|
|
32
|
+
|
|
33
|
+
items[iCnt] = new ReliefSupply(sName, iWeight, iValue);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 4. Apply Knapsack Algorithm
|
|
37
|
+
Knapsack(items, iCapacity);
|
|
38
|
+
|
|
39
|
+
sc.close();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 5. Relief Supply Entity
|
|
43
|
+
static class ReliefSupply {
|
|
44
|
+
String sName;
|
|
45
|
+
int iWeight, iValue;
|
|
46
|
+
|
|
47
|
+
ReliefSupply(String sName, int iWeight, int iValue) {
|
|
48
|
+
this.sName = sName;
|
|
49
|
+
this.iWeight = iWeight;
|
|
50
|
+
this.iValue = iValue;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 6. Knapsack Dynamic Programming
|
|
55
|
+
static void Knapsack(ReliefSupply items[], int iCapacity) {
|
|
56
|
+
int iSize = items.length;
|
|
57
|
+
int iDP[][] = new int[iSize + 1][iCapacity + 1];
|
|
58
|
+
|
|
59
|
+
// 7. Calculate Maximum Utility
|
|
60
|
+
for (int iCnt = 1; iCnt <= iSize; iCnt++) {
|
|
61
|
+
for (int iW = 1; iW <= iCapacity; iW++) {
|
|
62
|
+
if (items[iCnt - 1].iWeight <= iW) {
|
|
63
|
+
iDP[iCnt][iW] = Math.max(
|
|
64
|
+
items[iCnt - 1].iValue +
|
|
65
|
+
iDP[iCnt - 1][iW - items[iCnt - 1].iWeight],
|
|
66
|
+
|
|
67
|
+
iDP[iCnt - 1][iW]);
|
|
68
|
+
} else {
|
|
69
|
+
iDP[iCnt][iW] = iDP[iCnt - 1][iW];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 8. Find Selected Items
|
|
75
|
+
boolean bSelected[] = new boolean[iSize];
|
|
76
|
+
int iW = iCapacity;
|
|
77
|
+
|
|
78
|
+
for (int iCnt = iSize; iCnt > 0; iCnt--) {
|
|
79
|
+
if (iDP[iCnt][iW] != iDP[iCnt - 1][iW]) {
|
|
80
|
+
bSelected[iCnt - 1] = true;
|
|
81
|
+
iW -= items[iCnt - 1].iWeight;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 9. Display Selected Supplies
|
|
86
|
+
int iTotalWeight = 0;
|
|
87
|
+
|
|
88
|
+
System.out.println("\nDisaster Relief Supply Allocation:");
|
|
89
|
+
System.out.println("-------------------------------------------------------------");
|
|
90
|
+
|
|
91
|
+
System.out.printf("%-26s %-15s %-15s%n",
|
|
92
|
+
"Supply Item", "Weight (kg)", "Utility Value");
|
|
93
|
+
|
|
94
|
+
System.out.println("-------------------------------------------------------------");
|
|
95
|
+
|
|
96
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
97
|
+
if (bSelected[iCnt]) {
|
|
98
|
+
System.out.printf("%-26s %-15d %-15d%n",
|
|
99
|
+
items[iCnt].sName,
|
|
100
|
+
items[iCnt].iWeight,
|
|
101
|
+
items[iCnt].iValue);
|
|
102
|
+
|
|
103
|
+
iTotalWeight += items[iCnt].iWeight;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
System.out.println("-------------------------------------------------------------");
|
|
108
|
+
System.out.println("Total Weight: " + iTotalWeight +
|
|
109
|
+
" kg / " + iCapacity + " kg");
|
|
110
|
+
|
|
111
|
+
System.out.println("Maximum Utility: " +
|
|
112
|
+
iDP[iSize][iCapacity] + " points");
|
|
113
|
+
|
|
114
|
+
System.out.println("-------------------------------------------------------------");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "K - Disaster"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
public class Solution {
|
|
5
|
+
// 1. Main Function
|
|
6
|
+
public static void main(String args[]) {
|
|
7
|
+
Scanner sc = new Scanner(System.in);
|
|
8
|
+
|
|
9
|
+
System.out.print("Enter backup generator capacity (Watts): ");
|
|
10
|
+
int iCapacity = sc.nextInt();
|
|
11
|
+
|
|
12
|
+
System.out.print("Enter number of medical devices: ");
|
|
13
|
+
int iSize = sc.nextInt();
|
|
14
|
+
|
|
15
|
+
MedicalDevice devices[] = new MedicalDevice[iSize];
|
|
16
|
+
|
|
17
|
+
// 2. Get Medical Device Details
|
|
18
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
19
|
+
sc.nextLine();
|
|
20
|
+
|
|
21
|
+
System.out.println("\nEnter details for Device " + (iCnt + 1));
|
|
22
|
+
|
|
23
|
+
System.out.print("Device Name: ");
|
|
24
|
+
String sName = sc.nextLine();
|
|
25
|
+
|
|
26
|
+
System.out.print("Power Requirement (Watts): ");
|
|
27
|
+
int iPower = sc.nextInt();
|
|
28
|
+
|
|
29
|
+
System.out.print("Critical Score: ");
|
|
30
|
+
int iScore = sc.nextInt();
|
|
31
|
+
|
|
32
|
+
devices[iCnt] = new MedicalDevice(sName, iPower, iScore);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 3. Apply Knapsack Algorithm
|
|
36
|
+
Knapsack(devices, iCapacity);
|
|
37
|
+
|
|
38
|
+
sc.close();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 4. Medical Device Entity
|
|
42
|
+
static class MedicalDevice {
|
|
43
|
+
String sName;
|
|
44
|
+
int iPower, iScore;
|
|
45
|
+
|
|
46
|
+
MedicalDevice(String sName, int iPower, int iScore) {
|
|
47
|
+
this.sName = sName;
|
|
48
|
+
this.iPower = iPower;
|
|
49
|
+
this.iScore = iScore;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 5. Knapsack Dynamic Programming
|
|
54
|
+
static void Knapsack(MedicalDevice devices[], int iCapacity) {
|
|
55
|
+
int iSize = devices.length;
|
|
56
|
+
int iDP[][] = new int[iSize + 1][iCapacity + 1];
|
|
57
|
+
|
|
58
|
+
// 6. Calculate Maximum Critical Score
|
|
59
|
+
for (int iCnt = 1; iCnt <= iSize; iCnt++) {
|
|
60
|
+
for (int iW = 1; iW <= iCapacity; iW++) {
|
|
61
|
+
if (devices[iCnt - 1].iPower <= iW) {
|
|
62
|
+
iDP[iCnt][iW] = Math.max(
|
|
63
|
+
devices[iCnt - 1].iScore +
|
|
64
|
+
iDP[iCnt - 1][iW - devices[iCnt - 1].iPower],
|
|
65
|
+
|
|
66
|
+
iDP[iCnt - 1][iW]);
|
|
67
|
+
} else {
|
|
68
|
+
iDP[iCnt][iW] = iDP[iCnt - 1][iW];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 7. Find Selected Medical Devices
|
|
74
|
+
boolean bSelected[] = new boolean[iSize];
|
|
75
|
+
int iW = iCapacity;
|
|
76
|
+
|
|
77
|
+
for (int iCnt = iSize; iCnt > 0; iCnt--) {
|
|
78
|
+
if (iDP[iCnt][iW] != iDP[iCnt - 1][iW]) {
|
|
79
|
+
bSelected[iCnt - 1] = true;
|
|
80
|
+
iW -= devices[iCnt - 1].iPower;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 8. Display Selected Equipment
|
|
85
|
+
int iTotalPower = 0;
|
|
86
|
+
|
|
87
|
+
System.out.println("\nHospital Emergency ICU Equipment Allocation:");
|
|
88
|
+
System.out.println("---------------------------------------------------------------");
|
|
89
|
+
|
|
90
|
+
System.out.printf("%-30s %-15s %-15s%n",
|
|
91
|
+
"Medical Equipment", "Power (W)", "Critical Score");
|
|
92
|
+
|
|
93
|
+
System.out.println("---------------------------------------------------------------");
|
|
94
|
+
|
|
95
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
96
|
+
if (bSelected[iCnt]) {
|
|
97
|
+
System.out.printf("%-30s %-15d %-15d%n",
|
|
98
|
+
devices[iCnt].sName,
|
|
99
|
+
devices[iCnt].iPower,
|
|
100
|
+
devices[iCnt].iScore);
|
|
101
|
+
|
|
102
|
+
iTotalPower += devices[iCnt].iPower;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
System.out.println("---------------------------------------------------------------");
|
|
107
|
+
System.out.println("Total Power Consumed: " +
|
|
108
|
+
iTotalPower + " W / " + iCapacity + " W");
|
|
109
|
+
|
|
110
|
+
System.out.println("Maximum Critical Score: " +
|
|
111
|
+
iDP[iSize][iCapacity] + " points");
|
|
112
|
+
|
|
113
|
+
System.out.println("---------------------------------------------------------------");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "K - Hospital"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
==================================================
|
|
2
|
+
6. 0/1 KNAPSACK (DYNAMIC PROGRAMMING)
|
|
3
|
+
==================================================
|
|
4
|
+
|
|
5
|
+
ALGORITHM:
|
|
6
|
+
1. Read the weight and profit of each item.
|
|
7
|
+
2. Create DP table of size (n+1) x (W+1).
|
|
8
|
+
3. Initialize the first row and column with zero.
|
|
9
|
+
4. Check whether the current item fits.
|
|
10
|
+
5. If it fits, calculate inclusion and exclusion values.
|
|
11
|
+
6. Store the maximum of both values.
|
|
12
|
+
7. Otherwise, copy the previous row value.
|
|
13
|
+
8. Find maximum profit from dp[n][W].
|
|
14
|
+
9. Backtrack to find selected items.
|
|
15
|
+
|
|
16
|
+
RECURRENCE RELATION:
|
|
17
|
+
|
|
18
|
+
If w[i] <= w:
|
|
19
|
+
|
|
20
|
+
V(i,w) = max { V(i-1,w),
|
|
21
|
+
V(i-1,w-w[i]) + P[i] }
|
|
22
|
+
|
|
23
|
+
If w[i] > w:
|
|
24
|
+
|
|
25
|
+
V(i,w) = V(i-1,w)
|
|
26
|
+
|
|
27
|
+
BASE CONDITIONS:
|
|
28
|
+
V(0,w) = 0
|
|
29
|
+
V(i,0) = 0
|
|
30
|
+
|
|
31
|
+
TIME COMPLEXITY:
|
|
32
|
+
Best Case : O(nW)
|
|
33
|
+
Average Case : O(nW)
|
|
34
|
+
Worst Case : O(nW)
|
|
35
|
+
Space : O(nW)
|
|
36
|
+
|
|
37
|
+
IMPORTANT:
|
|
38
|
+
1. 0/1 Knapsack does not allow fractions of items.
|
|
@@ -0,0 +1,180 @@
|
|
|
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
|
+
}
|