0% found this document useful (0 votes)
2 views

Lab6 daa

The document contains two Java programs: one for solving the Fractional Knapsack problem, which calculates the maximum value that can be carried given a set of items with specific weights and values, and another for Huffman Coding, which generates Huffman codes for characters in a given string based on their frequency. The Fractional Knapsack program sorts items by value-to-weight ratio and allows for fractional selection, while the Huffman Coding program builds a tree structure to compress the input string. Both programs utilize user input for item details and display the results accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab6 daa

The document contains two Java programs: one for solving the Fractional Knapsack problem, which calculates the maximum value that can be carried given a set of items with specific weights and values, and another for Huffman Coding, which generates Huffman codes for characters in a given string based on their frequency. The Fractional Knapsack program sorts items by value-to-weight ratio and allows for fractional selection, while the Huffman Coding program builds a tree structure to compress the input string. Both programs utilize user input for item details and display the results accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

import java.util.

*;

class Item {
int value, weight;
Item(int value, int weight) {
this.value = value;
this.weight = weight;
}
}

public class FractionalKnapsack


{

public static double


getMaxValue(int capacity, Item[]
items) {
// Sort items by value-to-
weight ratio in descending order
Arrays.sort(items, (a, b) ->
Double.compare((double)b.value
/ b.weight, (double)a.value /
a.weight));

double totalValue = 0.0;


for (Item item : items) {
if (capacity == 0) break;

if (item.weight <=
capacity) {
totalValue +=
item.value;
capacity -=
item.weight;
} else {
// Take fraction of the
item
totalValue +=
(double)item.value * capacity /
item.weight;
capacity = 0;
}
}
return totalValue;
}

public static void main(String[]


args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter
number of items: ");
int n = sc.nextInt();

Item[] items = new Item[n];


System.out.println("Enter
value and weight for each
item:");
for (int i = 0; i < n; i++) {
int value = sc.nextInt();
int weight = sc.nextInt();
items[i] = new Item(value,
weight);
}

System.out.print("Enter
knapsack capacity: ");
int capacity = sc.nextInt();
double maxValue =
getMaxValue(capacity, items);
System.out.printf("Maximum
value in knapsack = %.2f\n",
maxValue);
}
}

....

import java.util.*;

class HuffmanNode {
char ch;
int freq;
HuffmanNode left, right;

HuffmanNode(char ch, int


freq) {
this.ch = ch;
this.freq = freq;
}
}
class CompareFreq implements
Comparator<HuffmanNode> {
public int
compare(HuffmanNode a,
HuffmanNode b) {
return a.freq - b.freq;
}
}

public class HuffmanCoding {

static void
generateCodes(HuffmanNode
root, String code,
Map<Character, String>
huffmanCodes) {
if (root == null) return;
if (root.left == null &&
root.right == null) {
huffmanCodes.put(root.ch,
code);
}
generateCodes(root.left,
code + "0", huffmanCodes);
generateCodes(root.right,
code + "1", huffmanCodes);
}

public static void main(String[]


args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter the
input string: ");
String input = sc.nextLine();

// Frequency map
Map<Character, Integer>
freqMap = new HashMap<>();
for (char ch :
input.toCharArray()) {
freqMap.put(ch,
freqMap.getOrDefault(ch, 0) + 1);
}

// Priority queue (min-heap)


PriorityQueue<HuffmanNode>
pq = new PriorityQueue<>(new
CompareFreq());
for (var entry :
freqMap.entrySet()) {
pq.add(new
HuffmanNode(entry.getKey(),
entry.getValue()));
}

// Build Huffman Tree


while (pq.size() > 1) {
HuffmanNode left =
pq.poll();
HuffmanNode right =
pq.poll();
HuffmanNode parent =
new HuffmanNode('-', left.freq +
right.freq);
parent.left = left;
parent.right = right;
pq.add(parent);
}

HuffmanNode root =
pq.poll();
Map<Character, String>
huffmanCodes
= new HashMap<>();
generateCodes(root, "",
huffmanCodes);

// Display the codes


System.out.println("Huffman
Codes:");
huffmanCodes.entrySet()
.stream()
.sorted(Map.Entry.co
mparingByKey())
.forEach(e ->
System.out.println(e.getKey() + ":
" + e.getValue()));

// Encode input
StringBuilder encoded =
new StringBuilder();
for (char ch :
input.toCharArray()) {
encoded.append(huffmanCodes.
get(ch));
}
System.out.println("Encoded
string: " + encoded.toString());
}
}

You might also like