Daa 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DESIGN AND ANALYSIS OG

ALGORITHM DIGITAL
ASSIGNMENT-2
Name: Mounika N
Reg no: 21BBS0046

Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Structure to represent an item


struct Item {

int weight;
int profit;
int index;
};

// Node of the search tree


struct Node {
int level;
int profit;
int weight;
double bound;

vector<bool> included;
};

// Custom sort function based on profit per unit weight


bool compare(Item a, Item b) {

double ratio_a = (double)a.profit / a.weight;


double ratio_b = (double)b.profit / b.weight;
return ratio_a > ratio_b;
}

// Function to calculate the upper bound of the node

double calculateBound(Node u, int n, int capacity, vector<Item> items) {


if (u.weight >= capacity)

return 0.0;

double bound = u.profit;


int j = u.level + 1;

int totalWeight = u.weight;

while ((j < n) && (totalWeight + items[j].weight <= capacity)) {


totalWeight += items[j].weight;
bound += items[j].profit;
j++;

if (j < n) {

bound += (capacity - totalWeight) * ((double)items[j].profit / items[j].weight);

return bound;
}

// Function to solve the 0/1 Knapsack problem using branch and bound
int knapsackBranchAndBound(int capacity, vector<int> weights, vector<int>
profits) {
int n = weights.size();
vector<Item> items(n);

// Creating Item structures


for (int i = 0; i < n; i++) {
items[i].weight = weights[i];
items[i].profit = profits[i];
items[i].index = i + 1;

// Sort items based on profit per unit weight in descending order


sort(items.begin(), items.end(), compare);

// Initialize the root node


Node root, curr;

root.level = -1;
root.profit = 0;

root.weight = 0;
root.bound = calculateBound(root, n, capacity, items);
root.included.resize(n, false);

// Create a queue to store nodes of the search tree


vector<Node> queue;
queue.push_back(root);

int maxProfit = 0;
while (!queue.empty()) {

// Get the node with the maximum bound


curr = queue.back();
queue.pop_back();

if (curr.bound > maxProfit) {

// Create the left child node (without including the current item)
Node left = curr;
left.level = curr.level + 1;

left.weight += items[left.level].weight;
left.profit += items[left.level].profit;

if (left.weight <= capacity && left.profit > maxProfit) {


maxProfit = left.profit;

left.bound = calculateBound(left, n, capacity, items);

if (left.bound > maxProfit) {


left.included[left.level] = true;
queue.push_back(left);

// Create the right child node (including the current item)


Node right = curr;

right.level = curr.level + 1;
right.bound = calculateBound(right, n, capacity, items);

if (right.bound > maxProfit) {


right.included[right.level] = false;
queue.push_back(right);
}
}

return maxProfit;

int main() {

int capacity;
cout << "Enter the capacity of the bag: ";
cin >> capacity;

vector<int> weights, profits;

cout << "Enter the number of items: ";


int n;
cin >> n;

cout << "Enter the weight and profit of each item:\n";


for (int i = 0; i < n; i++) {

int weight, profit;


cin >> weight >> profit;
weights.push_back(weight);
profits.push_back(profit);
}

int maxProfit = knapsackBranchAndBound(capacity, weights,


profits); cout << "Maximum profit: " << maxProfit << endl;

return 0;
}

Output:
Time complexity: The time complexity of this algorithm is
exponential, specifically O(2^n), where n is the number of items. The
branch and bound method explores all possible combinations of
including or excluding each item, resulting in an exponential time
complexity.

Code:

#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;
// Structure for the Huffman tree node
struct Node {

char data;
unsigned freq;
Node *left,
*right;

Node(char data, unsigned freq)


: data(data), freq(freq), left(nullptr), right(nullptr) {}
};

// Comparator function for the priority queue


struct compare {
bool operator()(Node* l, Node* r)

{
return l->freq > r->freq;

}
};

// Function to print Huffman codes from the root of the Huffman tree
void printHuffmanCodes(Node* root, string str)

{
if (!root)
return;

if (root->data != '$')

cout << root->data << " : " << str << "\n";

printHuffmanCodes(root->left, str + "0");


printHuffmanCodes(root->right, str + "1");

// Function to build the Huffman tree and generate codes


void huffmanCodes(char data[], int freq[], int size)

Node *left, *right, *top;


priority_queue<Node*, vector<Node*>, compare> minHeap;

for (int i = 0; i < size; ++i)


minHeap.push(new Node(data[i], freq[i]));

while (minHeap.size() != 1) {
left = minHeap.top();
minHeap.pop();

right = minHeap.top();
minHeap.pop();

top = new Node('$', left->freq + right->freq);

top->left = left;
top->right = right;

minHeap.push(top);

printHuffmanCodes(minHeap.top(), "");
}

int main()
{

int n;

cout << "Enter the number of characters: ";


cin >> n;
char data[n];
int freq[n];

cout << "Enter characters and their frequencies:\n";


for (int i = 0; i < n; i++) {
cout << "Character " << i + 1 << ": ";
cin >> data[i];

cout << "Frequency: ";


cin >> freq[i];
}

huffmanCodes(data, freq, n);

return 0;

Output:
Time complexity: The time complexity of this implementation is
O(nlogn), where n is the number of characters. The priority queue
(min-heap) operations take logarithmic time complexity, and we
perform n - 1 iterations to build the Huffman tree.

***END***

You might also like