DAA Codes
DAA Codes
import java.util.Scanner;
rSteps++;
if (n < 0)
return 0;
if (n == 1 || n == 0)
return 1;
f[0] = 0;
f[1] = 1;
iSteps = 1;
iSteps++;
}
System.out.println();
System.out.println();
int choice;
int n;
while (true) {
System.out.println("3. Quit");
choice = scanner.nextInt();
if (choice == 1 || choice == 2) {
n = scanner.nextInt();
if (n <= 0) {
System.out.println("Invalid input. Please enter a positive number.");
continue;
if (choice == 1) {
iSteps = 0;
iStepFibonacci(n);
} else if (choice == 2) {
rSteps = 0;
printFibonacciSeries(n);
} else if (choice == 3) {
System.out.println("Successfully Exited.");
break;
} else {
}
}
scanner.close();
/*
output:
1. Calculate Iteratively
2. Calculate Recursively
3. Quit
1. Calculate Iteratively
2. Calculate Recursively
3. Quit
1. Calculate Iteratively
2. Calculate Recursively
3. Quit
Successfully Exited.
*/
// HuffMann Encoding using greedy
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.HashMap;
class TreeNode {
char data;
float freq;
TreeNode left;
TreeNode right;
this.data = data;
this.freq = freq;
this.left = null;
this.right = null;
static int n = 0;
minHeap.add(node);
newNode.left = left;
newNode.right = right;
minHeap.add(newNode);
root = minHeap.poll();
return root;
if (index >= 0) {
encodedMessage.append(codes[index]);
if (bit == '0') {
current = current.left;
} else {
current = current.right;
decodedMessage.append(current.data);
current = root;
if (alphabets[i] == c) {
return i;
return -1;
while (true) {
System.out.println("4. Exit");
case 1:
root = createHuffmanTree();
System.out.println("\nPrefix codes:");
n = 0;
break;
case 2:
encode();
break;
case 3:
decode(root);
break;
case 4:
System.out.println("Successfully Exited.");
scanner.close();
return;
default:
if (p != null) {
word[i] = 0;
alphabets[n] = p.data;
codes[n++] = new String(word);
word[i] = '0';
preorder(p.left, i + 1, word);
word[i] = '1';
preorder(p.right, i + 1, word);
/*
output:
2. Encode a Message
3. Decode a Message
4. Exit
Enter alphabet: A
Enter alphabet: B
Enter alphabet: C
Enter alphabet: E
Enter alphabet: F
2. Encode a Message
3. Decode a Message
4. Exit
2. Encode a Message
3. Decode a Message
4. Exit
2. Encode a Message
3. Decode a Message
4. Exit
Successfully Exited.
*/
// Fractional Knapsack
//Fractional Knapsack
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Item {
int profit;
int weight;
this.profit = profit;
this.weight = weight;
double first;
double second;
this.first = first;
this.second = second;
}
static boolean compare(Pair p1, Pair p2) {
int n;
n = scanner.nextInt();
int w = scanner.nextInt();
}
Collections.sort(a, (p1, p2) -> Double.compare(p2.first, p1.first));
if (w >= a.get(i).second) {
ans += items.get(i).profit;
w -= a.get(i).second;
solution.set(i, 1.0);
} else {
ans += vw * w;
w = 0;
break;
scanner.close();
/*
Enter Number of Objects: 5
60 10
100 20
120 30
90 5
50 15
*/
// 0-1 Knapsack
import java.util.Scanner;
public static int knapsack(int[] values, int[] weights, int capacity, int[] solution) {
int n = values.length;
// dp[i][w] represents the maximum value that can be obtained with i items and a knapsack of
capacity w.
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else {
int i = n, w = capacity;
w -= weights[i - 1];
i--;
// The value in the bottom-right corner of the table represents the maximum value that can be
obtained.
return dp[n][capacity];
int n = scanner.nextInt();
values[i] = scanner.nextInt();
weights[i] = scanner.nextInt();
}
System.out.println();
/*
Output:
*/
// N-Queen
import java.util.*;
if (r == n) {
printBoard(board);
return;
board[r][c] = true;
solve(r + 1, n);
board[r][c] = false;
int n = board.length;
if (board[i][j]) {
System.out.print("Q ");
} else {
System.out.print(". ");
System.out.println();
System.out.println();
int n = scanner.nextInt();
scanner.close();
solve(0, n);
}
/*
output:
.Q..
...Q
Q...
..Q.
..Q.
Q...
...Q
.Q..
*/