Compare commits

..

8 Commits

6 changed files with 373 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
// Code-A1 (Fibonacci)
#include <iostream>
using namespace std;
// Recursive function for Fibonacci
int fibRecursive(int n) {
if (n <= 1)
return n; // Base case: fib(0)=0, fib(1)=1
return fibRecursive(n - 1) + fibRecursive(n - 2);
}
// Non-recursive (Iterative) Fibonacci
int fibIterative(int n) {
if (n <= 1)
return n;
int prev = 0, curr = 1, next;
for (int i = 2; i <= n; i++) {
next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
cout << "\nFibonacci Series using Recursion: ";
for (int i = 0; i < n; i++)
cout << fibRecursive(i) << " ";
cout << "\nFibonacci Series using Iteration: ";
for (int i = 0; i < n; i++)
cout << fibIterative(i) << " ";
cout << endl;
return 0;
}
// SAMPLE OUTPUT
/*
* $ ./a.out
* Enter the number of terms: 5
*
* Fibonacci Series using Recursion: 0 1 1 2 3
* Fibonacci Series using Iteration: 0 1 1 2 3
*/
+102
View File
@@ -0,0 +1,102 @@
// Code-A2 (Huffman)
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
// A Huffman Tree node
struct Node {
char ch;
int freq;
Node *left, *right;
Node(char c, int f) {
ch = c;
freq = f;
left = right = nullptr;
}
};
// Compare function for priority queue (min-heap)
struct Compare {
bool operator()(Node* a, Node* b) {
return a->freq > b->freq; // smaller frequency => higher priority
}
};
// Recursive function to print Huffman Codes
void printCodes(Node* root, string code) {
if (!root) return;
// Leaf node -> contains a character
if (!root->left && !root->right)
cout << root->ch << " : " << code << endl;
printCodes(root->left, code + "0");
printCodes(root->right, code + "1");
}
// Main function to build Huffman Tree and generate codes
void huffmanEncoding(vector<char>& chars, vector<int>& freq) {
priority_queue<Node*, vector<Node*>, Compare> pq;
// Step 1: Create a leaf node for each character and add it to the queue
for (int i = 0; i < chars.size(); i++)
pq.push(new Node(chars[i], freq[i]));
// Step 2: Repeat until one node remains (the root)
while (pq.size() > 1) {
Node* left = pq.top(); pq.pop();
Node* right = pq.top(); pq.pop();
// Create new internal node with sum of two smallest frequencies
Node* newNode = new Node('-', left->freq + right->freq);
newNode->left = left;
newNode->right = right;
pq.push(newNode);
}
// Step 3: Print the Huffman codes
Node* root = pq.top();
cout << "\nHuffman Codes:\n";
printCodes(root, "");
}
int main() {
int n;
cout << "Enter number of characters: ";
cin >> n;
vector<char> chars(n);
vector<int> freq(n);
cout << "Enter characters: ";
for (int i = 0; i < n; i++)
cin >> chars[i];
cout << "Enter corresponding frequencies: ";
for (int i = 0; i < n; i++)
cin >> freq[i];
huffmanEncoding(chars, freq);
return 0;
}
// SAMPLE OUTPUT
/*
* $ ./a.out
* Enter number of characters: 5
* Enter characters: a f v c w
* Enter corresponding frequencies: 3 2 1 5 7
*
* Huffman Codes:
* w : 0
* c : 10
* a : 110
* v : 1110
* f : 1111
*/
@@ -0,0 +1,72 @@
// Code-A3 (Fractical Knapsack)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure for an item
struct Item {
int weight;
int value;
};
// Comparison function to sort items by value/weight ratio
bool compare(Item a, Item b) {
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2; // Sort in descending order
}
double fractionalKnapsack(int W, vector<Item>& items) {
// Sort items by value-to-weight ratio
sort(items.begin(), items.end(), compare);
double totalValue = 0.0; // Total value in knapsack
for (auto &i : items) {
if (W == 0) break; // Knapsack is full
// If item can be fully taken
if (i.weight <= W) {
W -= i.weight;
totalValue += i.value;
} else {
// Take fraction of item
totalValue += i.value * ((double)W / i.weight);
W = 0;
}
}
return totalValue;
}
int main() {
int n, W;
cout << "Enter number of items: ";
cin >> n;
vector<Item> items(n);
cout << "Enter value and weight of each item:\n";
for (int i = 0; i < n; i++)
cin >> items[i].value >> items[i].weight;
cout << "Enter capacity of knapsack: ";
cin >> W;
double maxValue = fractionalKnapsack(W, items);
cout << "\nMaximum value in knapsack = " << maxValue << endl;
return 0;
}
// SAMPLE OUTPUT
/*
* $ ./a.out
* Enter number of items: 3
* Enter value and weight of each item:
* 12 32
* 56 67
* 23 87
* Enter capacity of knapsack: 45
*
* Maximum value in knapsack = 37.6119
*/
+60
View File
@@ -0,0 +1,60 @@
// Code-A4 (0-1 Knapsack)
#include <iostream>
#include <vector>
using namespace std;
int knapsack(int W, vector<int>& wt, vector<int>& val, int n) {
// dp[i][w] = max value for first i items with weight limit w
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
// Build the table dp[][] bottom-up
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (wt[i - 1] <= w)
dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W]; // The bottom-right cell gives the result
}
int main() {
int n, W;
cout << "Enter number of items: ";
cin >> n;
vector<int> val(n), wt(n);
cout << "Enter values of items:\n";
for (int i = 0; i < n; i++) cin >> val[i];
cout << "Enter weights of items:\n";
for (int i = 0; i < n; i++) cin >> wt[i];
cout << "Enter capacity of knapsack: ";
cin >> W;
int maxValue = knapsack(W, wt, val, n);
cout << "\nMaximum value in knapsack = " << maxValue << endl;
return 0;
}
// SAMPLE OUTPUT
/*
* $ ./a.out
* Enter number of items: 3
* Enter values of items:
* 12
* 32
* 14
* Enter weights of items:
* 45
* 34
* 65
* Enter capacity of knapsack: 60
*
* Maximum value in knapsack = 32
*/
+86
View File
@@ -0,0 +1,86 @@
// Code-A5 (N-Queen)
#include <iostream>
#include <vector>
using namespace std;
// Function to print the board
void printBoard(vector<vector<int>>& board, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << board[i][j] << " ";
cout << endl;
}
cout << endl;
}
// Function to check if placing a queen is safe
bool isSafe(vector<vector<int>>& board, int row, int col, int n) {
// Check column
for (int i = 0; i < row; i++)
if (board[i][col]) return false;
// Check upper left diagonal
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--)
if (board[i][j]) return false;
// Check upper right diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++)
if (board[i][j]) return false;
return true;
}
// Backtracking function
bool solveNQueens(vector<vector<int>>& board, int row, int n) {
if (row == n) {
printBoard(board, n); // Print one valid arrangement
return true;
}
for (int col = 0; col < n; col++) {
if (isSafe(board, row, col, n)) {
board[row][col] = 1; // Place queen
solveNQueens(board, row + 1, n);
board[row][col] = 0; // Backtrack
}
}
return false;
}
int main() {
int n, firstRow, firstCol;
cout << "Enter size of board (N): ";
cin >> n;
vector<vector<int>> board(n, vector<int>(n, 0));
cout << "Enter position of first queen (row and column index starting from 0): ";
cin >> firstRow >> firstCol;
// Place first queen
board[firstRow][firstCol] = 1;
cout << "\nAll possible solutions:\n";
solveNQueens(board, 0, n); // Start solving from row 0
return 0;
}
// SAMPLE OUTPUT
/*
* $ ./a.out
* Enter size of board (N): 4
* Enter position of first queen (row and column index starting from 0): 0 0
*
* All possible solutions:
* 0 1 0 0
* 0 0 0 1
* 1 0 0 0
* 0 0 1 0
*
* 0 0 1 0
* 1 0 0 0
* 0 0 0 1
* 0 1 0 0
*/
+3
View File
@@ -23,6 +23,9 @@ This repository contains valuable resources for the Design and Analysis of Algor
4. [Code-A4 - 0/1 Knapsack](Codes/Code_A4.java) 4. [Code-A4 - 0/1 Knapsack](Codes/Code_A4.java)
5. [Code-A5 - N-Queen Problem](Codes/Code-A5.py) 5. [Code-A5 - N-Queen Problem](Codes/Code-A5.py)
> [!NOTE]
> C++ versions of all codes are available in the [./Codes/C++](./Codes/C++) directory.
### Practical ### Practical
1. [Practical-1](Practical/Practical-1) 1. [Practical-1](Practical/Practical-1)