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,159 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Class
|
|
5
|
+
|
|
6
|
+
class Solution {
|
|
7
|
+
// 2. Main Function
|
|
8
|
+
|
|
9
|
+
public static void main(String A[]) {
|
|
10
|
+
Scanner sobj = new Scanner(System.in);
|
|
11
|
+
|
|
12
|
+
double dCapacity;
|
|
13
|
+
int iSize;
|
|
14
|
+
|
|
15
|
+
// 6. Accept Boat Capacity
|
|
16
|
+
|
|
17
|
+
System.out.print("Enter boat capacity (kg): ");
|
|
18
|
+
dCapacity = sobj.nextDouble();
|
|
19
|
+
|
|
20
|
+
// 7. Accept Number of Relief Items
|
|
21
|
+
|
|
22
|
+
System.out.print("Enter number of relief items: ");
|
|
23
|
+
iSize = sobj.nextInt();
|
|
24
|
+
|
|
25
|
+
ReliefItem Arr[] = new ReliefItem[iSize];
|
|
26
|
+
|
|
27
|
+
// 8. Accept Relief Item Details
|
|
28
|
+
|
|
29
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
30
|
+
String sName;
|
|
31
|
+
double dWeight;
|
|
32
|
+
double dValue;
|
|
33
|
+
|
|
34
|
+
System.out.println("\nEnter details of item " + (iCnt + 1));
|
|
35
|
+
|
|
36
|
+
System.out.print("Item name: ");
|
|
37
|
+
sName = sobj.next();
|
|
38
|
+
|
|
39
|
+
System.out.print("Weight (kg): ");
|
|
40
|
+
dWeight = sobj.nextDouble();
|
|
41
|
+
|
|
42
|
+
System.out.print("Utility value: ");
|
|
43
|
+
dValue = sobj.nextDouble();
|
|
44
|
+
|
|
45
|
+
Arr[iCnt] = new ReliefItem(sName, dWeight, dValue);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 9. Call Fractional Knapsack
|
|
49
|
+
|
|
50
|
+
FractionalKnapsack(Arr, dCapacity);
|
|
51
|
+
|
|
52
|
+
sobj.close();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 3. Relief Item Entity
|
|
56
|
+
// Stores item name, weight, utility value and ratio.
|
|
57
|
+
|
|
58
|
+
static class ReliefItem {
|
|
59
|
+
String sName;
|
|
60
|
+
double dWeight;
|
|
61
|
+
double dValue;
|
|
62
|
+
double dRatio;
|
|
63
|
+
|
|
64
|
+
ReliefItem(String sName, double dWeight, double dValue) {
|
|
65
|
+
this.sName = sName;
|
|
66
|
+
this.dWeight = dWeight;
|
|
67
|
+
this.dValue = dValue;
|
|
68
|
+
this.dRatio = dValue / dWeight;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 4. Swap Function
|
|
73
|
+
// Exchanges two relief items.
|
|
74
|
+
|
|
75
|
+
public static void Swap(ReliefItem Arr[], int iFirst, int iSecond) {
|
|
76
|
+
ReliefItem temp = Arr[iFirst];
|
|
77
|
+
|
|
78
|
+
Arr[iFirst] = Arr[iSecond];
|
|
79
|
+
|
|
80
|
+
Arr[iSecond] = temp;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 5. Sort Function
|
|
84
|
+
// Sorts relief items in descending order of utility-to-weight ratio.
|
|
85
|
+
|
|
86
|
+
public static void Sort(ReliefItem Arr[], int iSize) {
|
|
87
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
88
|
+
for (int jCnt = 0; jCnt < iSize - iCnt - 1; jCnt++) {
|
|
89
|
+
if (Arr[jCnt].dRatio < Arr[jCnt + 1].dRatio) {
|
|
90
|
+
Swap(Arr, jCnt, jCnt + 1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 10. Fractional Knapsack Function
|
|
97
|
+
// Selects items to maximize total utility within boat capacity.
|
|
98
|
+
|
|
99
|
+
public static void FractionalKnapsack(ReliefItem Arr[], double dCapacity) {
|
|
100
|
+
double dTotalValue = 0;
|
|
101
|
+
double dTotalWeight = 0;
|
|
102
|
+
|
|
103
|
+
// 10.1 Sort Items by Ratio
|
|
104
|
+
|
|
105
|
+
Sort(Arr, Arr.length);
|
|
106
|
+
|
|
107
|
+
// 10.2 Select Relief Items
|
|
108
|
+
|
|
109
|
+
System.out.println("\nRelief Supply Allocation:");
|
|
110
|
+
|
|
111
|
+
System.out.printf("%-20s %-12s %-12s %-12s%n",
|
|
112
|
+
"Item", "Weight", "Value", "Taken");
|
|
113
|
+
|
|
114
|
+
System.out.println("------------------------------------------------------------");
|
|
115
|
+
|
|
116
|
+
for (int iCnt = 0; iCnt < Arr.length; iCnt++) {
|
|
117
|
+
if (dCapacity <= 0) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (Arr[iCnt].dWeight <= dCapacity) {
|
|
122
|
+
dCapacity = dCapacity - Arr[iCnt].dWeight;
|
|
123
|
+
|
|
124
|
+
dTotalWeight = dTotalWeight + Arr[iCnt].dWeight;
|
|
125
|
+
|
|
126
|
+
dTotalValue = dTotalValue + Arr[iCnt].dValue;
|
|
127
|
+
|
|
128
|
+
System.out.printf("%-20s %-12.2f %-12.2f 100%%%n",
|
|
129
|
+
Arr[iCnt].sName,
|
|
130
|
+
Arr[iCnt].dWeight,
|
|
131
|
+
Arr[iCnt].dValue);
|
|
132
|
+
} else {
|
|
133
|
+
double dFraction = dCapacity / Arr[iCnt].dWeight;
|
|
134
|
+
|
|
135
|
+
double dTakenValue = dFraction * Arr[iCnt].dValue;
|
|
136
|
+
|
|
137
|
+
dTotalWeight = dTotalWeight + dCapacity;
|
|
138
|
+
|
|
139
|
+
dTotalValue = dTotalValue + dTakenValue;
|
|
140
|
+
|
|
141
|
+
System.out.printf("%-20s %-12.2f %-12.2f %.2f%%%n",
|
|
142
|
+
Arr[iCnt].sName,
|
|
143
|
+
dCapacity,
|
|
144
|
+
dTakenValue,
|
|
145
|
+
dFraction * 100);
|
|
146
|
+
|
|
147
|
+
dCapacity = 0;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 10.3 Display Final Result
|
|
152
|
+
|
|
153
|
+
System.out.println("------------------------------------------------------------");
|
|
154
|
+
|
|
155
|
+
System.out.printf("Total Weight : %.2f kg%n", dTotalWeight);
|
|
156
|
+
|
|
157
|
+
System.out.printf("Maximum Utility : %.2f%n", dTotalValue);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -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 = "FK - Emergency"
|
|
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,26 @@
|
|
|
1
|
+
==================================================
|
|
2
|
+
3. FRACTIONAL KNAPSACK (GREEDY)
|
|
3
|
+
==================================================
|
|
4
|
+
|
|
5
|
+
ALGORITHM:
|
|
6
|
+
1. Calculate value/weight ratio for every item.
|
|
7
|
+
2. Sort items in decreasing order of ratio.
|
|
8
|
+
3. Initialize the remaining knapsack capacity.
|
|
9
|
+
4. Select the item with the highest ratio.
|
|
10
|
+
5. If the item fits, take it completely.
|
|
11
|
+
6. Otherwise, take the fraction that fits.
|
|
12
|
+
7. Calculate the total maximum value.
|
|
13
|
+
|
|
14
|
+
FORMULA:
|
|
15
|
+
Ratio = Value / Weight
|
|
16
|
+
|
|
17
|
+
Total Value = Sum(Value[i] * Fraction[i])
|
|
18
|
+
|
|
19
|
+
RECURRENCE:
|
|
20
|
+
Not recursive. Uses Greedy technique.
|
|
21
|
+
|
|
22
|
+
TIME COMPLEXITY:
|
|
23
|
+
Sorting : O(n log n)
|
|
24
|
+
Selection : O(n)
|
|
25
|
+
Overall : O(n log n)
|
|
26
|
+
Space : O(n)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import java.util.Arrays;
|
|
2
|
+
|
|
3
|
+
// Main solution
|
|
4
|
+
public class Solution {
|
|
5
|
+
// O(V * m^V)
|
|
6
|
+
|
|
7
|
+
// Safety check
|
|
8
|
+
public static boolean isSafe(int v, int[][] graph, int[] color, int c, int V) {
|
|
9
|
+
for (int i = 0; i < V; i++) {
|
|
10
|
+
if (graph[v][i] == 1 && color[i] == c) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Backtracking search
|
|
18
|
+
public static boolean graphColoringUtil(int[][] graph, int m, int[] color, int v, int V) {
|
|
19
|
+
if (v == V) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (int c = 1; c <= m; c++) {
|
|
24
|
+
if (isSafe(v, graph, color, c, V)) {
|
|
25
|
+
color[v] = c;
|
|
26
|
+
|
|
27
|
+
if (graphColoringUtil(graph, m, color, v + 1, V)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Backtrack
|
|
32
|
+
color[v] = 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public static void classroomScheduler(int[][] graph, int m, String[] lectures) {
|
|
39
|
+
int V = lectures.length;
|
|
40
|
+
int[] color = new int[V];
|
|
41
|
+
Arrays.fill(color, 0);
|
|
42
|
+
|
|
43
|
+
System.out.println("\nAcademic Classroom Allocation (Conflict-Free Scheduling):");
|
|
44
|
+
System.out.println("------------------------------------------------------------------");
|
|
45
|
+
|
|
46
|
+
if (!graphColoringUtil(graph, m, color, 0, V)) {
|
|
47
|
+
System.out.println("Cannot schedule all lectures with " + m + " classrooms without conflicts.");
|
|
48
|
+
} else {
|
|
49
|
+
System.out.printf("%-30s %-20s%n", "Lecture Session", "Allocated Classroom");
|
|
50
|
+
System.out.println("------------------------------------------------------------------");
|
|
51
|
+
for (int i = 0; i < V; i++) {
|
|
52
|
+
System.out.printf("%-30s Room Hall %d%n", lectures[i], color[i]);
|
|
53
|
+
}
|
|
54
|
+
System.out.println("------------------------------------------------------------------");
|
|
55
|
+
System.out.println("Status: All lectures scheduled with zero room booking collisions.");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Driver code
|
|
60
|
+
public static void main(String[] args) {
|
|
61
|
+
System.out.println("=======================================================================");
|
|
62
|
+
System.out.println("PS 20: Classroom Scheduling Without Conflicts (Graph Coloring - Java)");
|
|
63
|
+
System.out.println("=======================================================================");
|
|
64
|
+
|
|
65
|
+
String[] lectures = {
|
|
66
|
+
"Physics 101 (9:00 - 10:00 AM)", // 0
|
|
67
|
+
"Chemistry 101 (9:30 - 10:30 AM)", // 1
|
|
68
|
+
"Math 201 (10:00 - 11:00 AM)", // 2
|
|
69
|
+
"Biology 101 (10:30 - 11:30 AM)", // 3
|
|
70
|
+
"English 102 (9:00 - 10:30 AM)", // 4
|
|
71
|
+
"History 101 (11:00 - 12:00 PM)" // 5
|
|
72
|
+
};
|
|
73
|
+
int V = lectures.length;
|
|
74
|
+
|
|
75
|
+
int[][] conflictGraph = {
|
|
76
|
+
{0, 1, 0, 0, 1, 0},
|
|
77
|
+
{1, 0, 1, 0, 1, 0},
|
|
78
|
+
{0, 1, 0, 1, 1, 0},
|
|
79
|
+
{0, 0, 1, 0, 0, 1},
|
|
80
|
+
{1, 1, 1, 0, 0, 0},
|
|
81
|
+
{0, 0, 0, 1, 0, 0}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
int availableClassrooms = 3;
|
|
85
|
+
classroomScheduler(conflictGraph, availableClassrooms, lectures);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -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 = "GC - Classroom"
|
|
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,88 @@
|
|
|
1
|
+
import java.util.Arrays;
|
|
2
|
+
|
|
3
|
+
// Main solution
|
|
4
|
+
public class Solution {
|
|
5
|
+
// O(V * m^V)
|
|
6
|
+
|
|
7
|
+
// Safety check
|
|
8
|
+
public static boolean isSafe(int v, int[][] graph, int[] color, int c, int V) {
|
|
9
|
+
for (int i = 0; i < V; i++) {
|
|
10
|
+
if (graph[v][i] == 1 && color[i] == c) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Backtracking search
|
|
18
|
+
public static boolean graphColoringUtil(int[][] graph, int m, int[] color, int v, int V) {
|
|
19
|
+
if (v == V) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (int c = 1; c <= m; c++) {
|
|
24
|
+
if (isSafe(v, graph, color, c, V)) {
|
|
25
|
+
color[v] = c;
|
|
26
|
+
|
|
27
|
+
if (graphColoringUtil(graph, m, color, v + 1, V)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Backtrack
|
|
32
|
+
color[v] = 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Solver entry
|
|
39
|
+
public static void frequencyAllocator(int[][] graph, int m, String[] towers) {
|
|
40
|
+
int V = towers.length;
|
|
41
|
+
int[] color = new int[V];
|
|
42
|
+
Arrays.fill(color, 0);
|
|
43
|
+
|
|
44
|
+
System.out.println("\nCellular Network Interference-Free Frequency Band Allocation:");
|
|
45
|
+
System.out.println("------------------------------------------------------------------");
|
|
46
|
+
|
|
47
|
+
if (!graphColoringUtil(graph, m, color, 0, V)) {
|
|
48
|
+
System.out.println("Cannot allocate frequencies without RF interference using " + m + " channels.");
|
|
49
|
+
} else {
|
|
50
|
+
System.out.printf("%-26s %-22s%n", "Cellular Tower", "Assigned Frequency Channel");
|
|
51
|
+
System.out.println("------------------------------------------------------------------");
|
|
52
|
+
for (int i = 0; i < V; i++) {
|
|
53
|
+
System.out.printf("%-26s Channel %d (Band %d MHz)%n", towers[i], color[i], 800 + color[i] * 50);
|
|
54
|
+
}
|
|
55
|
+
System.out.println("------------------------------------------------------------------");
|
|
56
|
+
System.out.println("Status: Zero co-channel interference across neighboring base stations.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Driver code
|
|
61
|
+
public static void main(String[] args) {
|
|
62
|
+
System.out.println("=======================================================================");
|
|
63
|
+
System.out.println("PS 21: Mobile Tower Frequency Allocation (Graph Coloring - Java)");
|
|
64
|
+
System.out.println("=======================================================================");
|
|
65
|
+
|
|
66
|
+
String[] towers = {
|
|
67
|
+
"Tower 1 (Marine Drive)", // 0
|
|
68
|
+
"Tower 2 (Bandra West)", // 1
|
|
69
|
+
"Tower 3 (Andheri East)", // 2
|
|
70
|
+
"Tower 4 (Kurla Junction)", // 3
|
|
71
|
+
"Tower 5 (Powai Hiranandani)", // 4
|
|
72
|
+
"Tower 6 (CSMI Airport Terminal)" // 5
|
|
73
|
+
};
|
|
74
|
+
int V = towers.length;
|
|
75
|
+
|
|
76
|
+
int[][] interferenceGraph = {
|
|
77
|
+
{0, 1, 1, 0, 0, 0},
|
|
78
|
+
{1, 0, 1, 1, 0, 0},
|
|
79
|
+
{1, 1, 0, 1, 1, 0},
|
|
80
|
+
{0, 1, 1, 0, 1, 1},
|
|
81
|
+
{0, 0, 1, 1, 0, 1},
|
|
82
|
+
{0, 0, 0, 1, 1, 0}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
int availableChannels = 3;
|
|
86
|
+
frequencyAllocator(interferenceGraph, availableChannels, towers);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -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 = "GC - Interference"
|
|
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,88 @@
|
|
|
1
|
+
import java.util.Arrays;
|
|
2
|
+
|
|
3
|
+
// Main solution
|
|
4
|
+
public class Solution {
|
|
5
|
+
// O(V * m^V)
|
|
6
|
+
|
|
7
|
+
// Safety check
|
|
8
|
+
public static boolean isSafe(int v, int[][] graph, int[] color, int c, int V) {
|
|
9
|
+
for (int i = 0; i < V; i++) {
|
|
10
|
+
if (graph[v][i] == 1 && color[i] == c) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Backtracking search
|
|
18
|
+
public static boolean graphColoringUtil(int[][] graph, int m, int[] color, int v, int V) {
|
|
19
|
+
if (v == V) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (int c = 1; c <= m; c++) {
|
|
24
|
+
if (isSafe(v, graph, color, c, V)) {
|
|
25
|
+
color[v] = c;
|
|
26
|
+
|
|
27
|
+
if (graphColoringUtil(graph, m, color, v + 1, V)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Backtrack
|
|
32
|
+
color[v] = 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Solver entry
|
|
39
|
+
public static void timetableScheduler(int[][] graph, int m, String[] courses) {
|
|
40
|
+
int V = courses.length;
|
|
41
|
+
int[] color = new int[V];
|
|
42
|
+
Arrays.fill(color, 0);
|
|
43
|
+
|
|
44
|
+
System.out.println("\nUniversity Exam Timetable Conflict-Free Scheduling:");
|
|
45
|
+
System.out.println("------------------------------------------------------------------");
|
|
46
|
+
|
|
47
|
+
if (!graphColoringUtil(graph, m, color, 0, V)) {
|
|
48
|
+
System.out.println("No conflict-free schedule possible using " + m + " time slots.");
|
|
49
|
+
} else {
|
|
50
|
+
System.out.printf("%-26s %-20s%n", "Course Name", "Assigned Exam Slot");
|
|
51
|
+
System.out.println("------------------------------------------------------------------");
|
|
52
|
+
for (int i = 0; i < V; i++) {
|
|
53
|
+
System.out.printf("%-26s Slot %d (Day %d)%n", courses[i], color[i], color[i]);
|
|
54
|
+
}
|
|
55
|
+
System.out.println("------------------------------------------------------------------");
|
|
56
|
+
System.out.println("Status: Successfully scheduled all courses with zero student clashes.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Driver code
|
|
61
|
+
public static void main(String[] args) {
|
|
62
|
+
System.out.println("=======================================================================");
|
|
63
|
+
System.out.println("PS 19: University Exam Timetable (Graph Coloring Backtracking - Java)");
|
|
64
|
+
System.out.println("=======================================================================");
|
|
65
|
+
|
|
66
|
+
String[] courses = {
|
|
67
|
+
"CS-101: Data Structures", // 0
|
|
68
|
+
"CS-102: Algorithms", // 1
|
|
69
|
+
"CS-103: Operating Systems", // 2
|
|
70
|
+
"EE-201: Digital Circuits", // 3
|
|
71
|
+
"MATH-301: Discrete Math", // 4
|
|
72
|
+
"AI-401: Machine Learning" // 5
|
|
73
|
+
};
|
|
74
|
+
int V = courses.length;
|
|
75
|
+
|
|
76
|
+
int[][] conflictGraph = {
|
|
77
|
+
{0, 1, 1, 1, 0, 0}, // CS-101
|
|
78
|
+
{1, 0, 1, 0, 1, 0}, // CS-102
|
|
79
|
+
{1, 1, 0, 0, 1, 1}, // CS-103
|
|
80
|
+
{1, 0, 0, 0, 1, 0}, // EE-201
|
|
81
|
+
{0, 1, 1, 1, 0, 1}, // MATH-301
|
|
82
|
+
{0, 0, 1, 0, 1, 0} // AI-401
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
int availableExamSlots = 3;
|
|
86
|
+
timetableScheduler(conflictGraph, availableExamSlots, courses);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -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 = "GC - University"
|
|
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,25 @@
|
|
|
1
|
+
==================================================
|
|
2
|
+
7. GRAPH COLORING (BACKTRACKING)
|
|
3
|
+
==================================================
|
|
4
|
+
|
|
5
|
+
ALGORITHM:
|
|
6
|
+
1. Represent the graph using an adjacency matrix.
|
|
7
|
+
2. Initialize all vertex colors to zero.
|
|
8
|
+
3. Start coloring from vertex 0.
|
|
9
|
+
4. Try every available color from 1 to m.
|
|
10
|
+
5. Check whether the color is safe.
|
|
11
|
+
6. If safe, assign the color and recursively color the next vertex.
|
|
12
|
+
7. If unsuccessful, reset color to zero (backtracking).
|
|
13
|
+
8. Continue until all vertices are colored or no solution exists.
|
|
14
|
+
|
|
15
|
+
RECURRENCE RELATION:
|
|
16
|
+
T(V) = mT(V-1) + O(V)
|
|
17
|
+
|
|
18
|
+
TIME COMPLEXITY:
|
|
19
|
+
Best Case : Depends on early solution.
|
|
20
|
+
Average Case : Input-dependent.
|
|
21
|
+
Worst Case : O(V * m^V)
|
|
22
|
+
Space : O(V) auxiliary space
|
|
23
|
+
|
|
24
|
+
IMPORTANT:
|
|
25
|
+
1. Graph Coloring uses Backtracking.
|