Assignment No-2
Assignment No-2
Assignment No-2
Write a program in C/C++/ Java to implement huffman Code using greedy methods
and also calculate the best case and worst case complexity.
→
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <string>
#include <functional>
#include <memory>
using namespace std;
// Structure representing a node in the Huffman tree
struct HuffmanNode {
char character;
int frequency;
shared_ptr<HuffmanNode> left;
shared_ptr<HuffmanNode> right;
HuffmanNode(char character, int frequency)
: character(character), frequency(frequency), left(nullptr), right(nullptr) {}
};
// Comparator for priority queue (min-heap)
struct HuffmanComparator {
bool operator()(const shared_ptr<HuffmanNode>& a, const shared_ptr<HuffmanNode>& b) {
return a->frequency > b->frequency; // Min-heap, lower frequency nodes have higher priority }
};
// Function to build the Huffman Tree
shared_ptr<HuffmanNode> buildHuffmanTree(const unordered_map<char, int>& frequencyMap) {
priority_queue<shared_ptr<HuffmanNode>, vector<shared_ptr<HuffmanNode>>,
HuffmanComparator> minHeap;
// Insert all characters and their frequencies into the min-heap
for (const auto& entry : frequencyMap) {
minHeap.push(make_shared<HuffmanNode>(entry.first, entry.second)); }
// Build the Huffman Tree
while (minHeap.size() > 1) {
// Extract the two nodes with the lowest frequency
shared_ptr<HuffmanNode> left = minHeap.top();
minHeap.pop();
shared_ptr<HuffmanNode> right = minHeap.top();
minHeap.pop();
// Create a new internal node with these two nodes as children
int combinedFrequency = left->frequency + right->frequency;
shared_ptr<HuffmanNode> internalNode = make_shared<HuffmanNode>('\0',
combinedFrequency);
internalNode->left = left;
internalNode->right = right;
minHeap.push(internalNode); // Add the new internal node back into the heap
}
return minHeap.top(); // Root of the Huffman Tree
}
// Function to generate Huffman codes from the Huffman Tree
void generateHuffmanCodes(
shared_ptr<HuffmanNode> root,
const string& currentCode,
unordered_map<char, string>& huffmanCodes
){
if (!root) {
return; // Empty node
}
if (!root->left && !root->right) { // Leaf node
huffmanCodes[root->character] = currentCode;
return;
}
// Recur to the left and right children with updated codes
generateHuffmanCodes(root->left, currentCode + "0", huffmanCodes);
generateHuffmanCodes(root->right, currentCode + "1", huffmanCodes);
}
// Main function demonstrating Huffman Coding
int main() {
// Sample data and frequencies
unordered_map<char, int> frequencyMap = {
{'A', 5},
{'B', 9},
{'C', 12},
{'D', 13},
{'E', 16},
{'F', 45}
};
// Build the Huffman Tree
shared_ptr<HuffmanNode> root = buildHuffmanTree(frequencyMap);
// Generate Huffman Codes
unordered_map<char, string> huffmanCodes;
generateHuffmanCodes(root, "", huffmanCodes);
// Display the Huffman Codes
cout << "Huffman Codes:\n";
for (const auto& entry : huffmanCodes) {
cout << entry.first << ": " << entry.second << endl; }
return 0;
}
2. Write a program in C/C++/ Java to find Minimum number of multiplications in
Matrix Chain Multiplication.
→
#include <iostream>
#include <vector>
#include <limits>
int n = dimensions.size() - 1;
std::cout << "Minimum number of scalar multiplications is: " << m[1][n] << std::endl;
return 0;
}
// The last element in the matrix contains the length of the LCS
return lcs[m][n];
}
int main() {
std::string str1 = "AGGTAB";
std::string str2 = "GXTXAYB";
std::cout << "Length of Longest Common Subsequence: " << lcsLength << std::endl;
return 0;
}
4. Write programs in C/C++/ Java to implement DFS and BFS. Compare the time
complexity.
→
#include <iostream>
#include <vector>
#include <stack>
private:
int vertices;
std::vector<std::vector<int>> adjList;
int main() {
Graph g(6);
return 0;
}
// If all vertices are included in the path, check if it's a Hamiltonian cycle
if (pos == n) {
return isHamiltonianCycle(graph, path);
}
return false;
}
int main() {
// Example adjacency matrix for a graph
std::vector<std::vector<int>> graph = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 1},
{1, 0, 1, 0, 1},
{0, 1, 1, 1, 0}
};
if (hasHamiltonianCycle(graph)) {
std::cout << "The graph has a Hamiltonian cycle." << std::endl;
} else {
std::cout << "The graph does not have a Hamiltonian cycle." << std::endl;
}
return 0;
}