-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy.java
More file actions
343 lines (313 loc) · 12.2 KB
/
Greedy.java
File metadata and controls
343 lines (313 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package TimeAndSpace.DSA;
import java.util.*;
public class Greedy {
// Coin Change Problem: Minimum Coins
public static void coinChange(int[] coins, int amount) {
Arrays.sort(coins);
int count = 0;
for (int i = coins.length - 1; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
if (amount == 0) {
System.out.println("Minimum coins required: " + count);
} else {
System.out.println("Change not possible with given coins.");
}
}
// Kruskal’s Algorithm: Minimum Spanning Tree
static class Edge {
int src, dest, weight;
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
static class Subset {
int parent, rank;
}
public static int find(Subset[] subsets, int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
public static void union(Subset[] subsets, int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
public static void kruskalMST(int v, List<Edge> edges) {
Collections.sort(edges, Comparator.comparingInt(o -> o.weight));
Subset[] subsets = new Subset[v];
for (int i = 0; i < v; i++)
subsets[i] = new Subset();
for (int i = 0; i < v; i++) {
subsets[i].parent = i;
subsets[i].rank = 0;
}
List<Edge> mst = new ArrayList<>();
for (Edge edge : edges) {
int x = find(subsets, edge.src);
int y = find(subsets, edge.dest);
if (x != y) {
mst.add(edge);
union(subsets, x, y);
}
}
System.out.println("Minimum Spanning Tree:");
for (Edge edge : mst)
System.out.println(edge.src + " - " + edge.dest + ": " + edge.weight);
}
// Dijkstra’s Algorithm: Shortest Path
public static void dijkstra(int[][] graph, int start) {
int V = graph.length;
int[] dist = new int[V];
boolean[] sptSet = new boolean[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
System.out.println("Shortest Path from node " + start + ":");
for (int i = 0; i < V; i++) {
if (dist[i] == Integer.MAX_VALUE)
System.out.println("Node " + i + " is not reachable.");
else
System.out.println("Distance from " + start + " to " + i + ": " + dist[i]);
}
}
private static int minDistance(int[] dist, boolean[] sptSet) {
int min = Integer.MAX_VALUE, minIndex = -1;
for (int v = 0; v < dist.length; v++) {
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}
// Activity Selection Problem
public static void activitySelection(int[] start, int[] finish) {
int n = start.length;
System.out.println("Selected activities:");
int i = 0;
System.out.print(i + " ");
for (int j = 1; j < n; j++) {
if (start[j] >= finish[i]) {
System.out.print(j + " ");
i = j;
}
}
System.out.println();
}
// Job Sequencing Problem
static class Job {
int id, deadline, profit;
public Job(int id, int deadline, int profit) {
this.id = id;
this.deadline = deadline;
this.profit = profit;
}
}
public static void jobSequencing(Job[] jobs, int n) {
Arrays.sort(jobs, Comparator.comparingInt(o -> -o.profit));
int[] result = new int[n];
boolean[] slot = new boolean[n];
for (int i = 0; i < n; i++) {
for (int j = Math.min(n, jobs[i].deadline) - 1; j >= 0; j--) {
if (!slot[j]) {
result[j] = jobs[i].id;
slot[j] = true;
break;
}
}
}
System.out.println("Job Sequencing with Maximum Profit:");
for (int i = 0; i < n; i++) {
if (result[i] != 0)
System.out.print(result[i] + " ");
}
System.out.println();
}
// Fractional Knapsack Problem
public static void fractionalKnapsack(int W, int[] wt, int[] val, int n) {
double[] ratio = new double[n];
for (int i = 0; i < n; i++) {
ratio[i] = (double) val[i] / wt[i];
}
double totalValue = 0;
for (int i = 0; i < n; i++) {
if (wt[i] <= W) {
W -= wt[i];
totalValue += val[i];
} else {
totalValue += val[i] * ((double) W / wt[i]);
break;
}
}
System.out.println("Maximum value in Knapsack = " + totalValue);
}
// Huffman Coding
public static void huffmanCoding(char[] chars, int[] freqs) {
int n = chars.length;
PriorityQueue<HuffmanNode> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.freq));
for (int i = 0; i < n; i++) {
pq.add(new HuffmanNode(chars[i], freqs[i]));
}
while (pq.size() > 1) {
HuffmanNode left = pq.poll();
HuffmanNode right = pq.poll();
HuffmanNode top = new HuffmanNode('-', left.freq + right.freq);
top.left = left;
top.right = right;
pq.add(top);
}
HuffmanNode root = pq.poll();
printHuffmanCodes(root, "");
}
static class HuffmanNode {
char ch;
int freq;
HuffmanNode left, right;
public HuffmanNode(char ch, int freq) {
this.ch = ch;
this.freq = freq;
left = right = null;
}
}
public static void printHuffmanCodes(HuffmanNode root, String code) {
if (root == null)
return;
if (root.ch != '-')
System.out.println(root.ch + ": " + code);
printHuffmanCodes(root.left, code + "0");
printHuffmanCodes(root.right, code + "1");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an algorithm to run:");
System.out.println("1. Coin Change Problem");
System.out.println("2. Kruskal's Algorithm (MST)");
System.out.println("3. Dijkstra's Algorithm");
System.out.println("4. Activity Selection Problem");
System.out.println("5. Job Sequencing Problem");
System.out.println("6. Fractional Knapsack");
System.out.println("7. Huffman Coding");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter amount:");
int amount = scanner.nextInt();
System.out.println("Enter number of coins:");
int n = scanner.nextInt();
int[] coins = new int[n];
System.out.println("Enter the coins:");
for (int i = 0; i < n; i++) {
coins[i] = scanner.nextInt();
}
coinChange(coins, amount);
break;
case 2:
System.out.println("Enter number of vertices:");
int v = scanner.nextInt();
List<Edge> edges = new ArrayList<>();
System.out.println("Enter the edges (src dest weight):");
while (true) {
int src = scanner.nextInt();
int dest = scanner.nextInt();
int weight = scanner.nextInt();
edges.add(new Edge(src, dest, weight));
System.out.print("Do you want to add another edge? (y/n): ");
char ch = scanner.next().charAt(0);
if (ch == 'n' || ch == 'N') break;
}
kruskalMST(v, edges);
break;
case 3:
System.out.println("Enter the number of vertices:");
int vertices = scanner.nextInt();
int[][] graph = new int[vertices][vertices];
System.out.println("Enter the adjacency matrix:");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
graph[i][j] = scanner.nextInt();
}
}
System.out.println("Enter the source vertex:");
int source = scanner.nextInt();
dijkstra(graph, source);
break;
case 4:
System.out.println("Enter number of activities:");
int numActivities = scanner.nextInt();
int[] start = new int[numActivities];
int[] finish = new int[numActivities];
System.out.println("Enter start and finish times for each activity:");
for (int i = 0; i < numActivities; i++) {
start[i] = scanner.nextInt();
finish[i] = scanner.nextInt();
}
activitySelection(start, finish);
break;
case 5:
System.out.println("Enter number of jobs:");
int jobCount = scanner.nextInt();
Job[] jobs = new Job[jobCount];
System.out.println("Enter jobs (id, deadline, profit):");
for (int i = 0; i < jobCount; i++) {
int id = scanner.nextInt();
int deadline = scanner.nextInt();
int profit = scanner.nextInt();
jobs[i] = new Job(id, deadline, profit);
}
jobSequencing(jobs, jobCount);
break;
case 6:
System.out.println("Enter number of items:");
int itemCount = scanner.nextInt();
int[] wt = new int[itemCount];
int[] val = new int[itemCount];
System.out.println("Enter weight and value of each item:");
for (int i = 0; i < itemCount; i++) {
wt[i] = scanner.nextInt();
val[i] = scanner.nextInt();
}
System.out.println("Enter knapsack capacity:");
int W = scanner.nextInt();
fractionalKnapsack(W, wt, val, itemCount);
break;
case 7:
System.out.println("Enter number of characters:");
int size = scanner.nextInt();
char[] chars = new char[size];
int[] freqs = new int[size];
System.out.println("Enter characters and their frequencies:");
for (int i = 0; i < size; i++) {
chars[i] = scanner.next().charAt(0);
freqs[i] = scanner.nextInt();
}
huffmanCoding(chars, freqs);
break;
default:
System.out.println("Invalid choice!");
}
scanner.close();
}
}