Daa Da H
Daa Da H
Digital Assignment
Name-Hariesh Reddy
Reg no-21BBS0201
1.Consider a postman starts from his own office and visit all the places to deliver the letters
exactly once and return to his office. i.e. given a set of places and distance between every pair
of places, the problem is to find the shortest possible route that visits every place exactly once
and returns to the starting point. Design a solution using Brute Force Approach.
Algorithm:
current_pathweight += graph[k][s]Add s to
pathway
Add current_pathweight to pathway
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
return ans;
}
int main() {
int n; // Number of places
cout << "Enter the number of places: ";
cin >> n;
cout << "The shortest possible route is: " << ans << endl;
return 0;
}
Output:
Time Complexity:
The time complexity of the Brute Force approach for the Traveling Salesman Problem is factorial,
which is denoted as O(n!), where n is the number of places. This is because there are n! possible
permutations to consider. In short, the time complexity of the Brute Force approach for finding
the shortest route in the given scenario is O(n!).
1. Consider a file containing 6 unique characters and frequency of each character is given
How many bits are required to store this file using Huffman Encoding? Also decode the
text "0101101101001". Implement Huffman encoding and decoding process using Greedy
algorithm Strategy.
Algorithm:
Huffman (C)
n=|C|Q
←C
for i=1 to n-1
z= allocate-Node ()
x= left[z]=Extract-Min(Q) y= right[z]
=Extract-Min(Q)f [z]=f[x]+f[y]
Insert (Q, z)
}
Code:
#include <iostream>
#include <queue>
#include
<unordered_map> using
namespace std;
struct Node {
char character;
int frequency;
Node* left;
Node* right;
struct Compare {
bool operator()(Node* a, Node* b) {
return a->frequency > b->frequency;
}
};
pq.push(newNode);
}
return pq.top();
}
return encodedText;
}
return decodedText;
}
int main() {
int numCharacters = 6;
vector<pair<char, int>> frequencies = {{'c', 34}, {'d', 9}, {'g', 35},
{'u', 2}, {'m', 2}, {'a', 100}};
Node* root = buildHuffmanTree(frequencies);
generateHuffmanCodes(root, "");
return 0;
}
Output:
Time Complexity:
The time complexity of Huffman Encoding and Decoding using a Greedy algorithm strategy is
typically O(n log n), where n is the number of unique characters in the file. This is because the
algorithm involves building a priority queue (min-heap) based on character frequencies, which
requires O(n log n) time complexity. Additionally, traversing the Huffman tree to generate codes
and encoding/decoding the text also takes linear time complexity, but the dominant factor is
building the priority queue.
2. Implement N-Queens problem and analyse its time complexity using backtracking.
Algorithm:
Algorithm NQueens(k, n)
{
for i := 1 to n do
if Place(k, i) then
{ x[k]==i;
if (k = n) then write (x1: n);else
NQueens(k + 1, r);
}
// global array whose first (k - 1) values have been set.Abs(r) returns the
absolute value of r.
for i:= 1 to k - 1 do
Code:
#include <iostream>
#include <vector>
using namespace std;
return true;
}
return res;
}
void solveNQueens(int n) {
vector<vector<int>> board(n, vector<int>(n, 0));
if (!solveNQueensUtil(board, 0, n)) {
cout << "No solution exists for N = " << n << endl;
}
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
solveNQueens(n);
return 0;
}
Output:
Time Complexity:
The time complexity of the backtracking algorithm for the N-Queens problem is exponential. In the
worst case, the algorithm needs to explore all possible configurations of queens on the board.
Therefore, the time complexity is O(N!) or O(N^N), where N is the size of the board and represents
the number of queens.
3. Consider a knapsack bag where n = 4, and the values (profits) are given by {10, 12, 12, 18}
and the weights given by {2, 4, 6, 9}. The maximum weight is given by W = 15. The solution is
to find subset of objects such that the total value is maximized, and the sum of weights of
the objects does not exceed a given capacity W .(Note: Taking fraction of objects is not
allowed). Implement the above scenario using Least Cost Branch & Bound method.
Algorithm:
1.Define a struct Item to represent an item with its value, weight, and cost (value/weight ratio).
2.Implement the compare function to compare two items based on their costs.
3. Implement the knapsack function that takes the maximum weight W, a vector of items items, and
the number of items n.
4. Sort the items in descending order based on their costs using the compare function.
6. Iterate through the items and check if adding the current item to the knapsack exceeds the
maximum weight.
7. If it does not exceed, add the item's weight and value to currentWeight and currentValue.
8. If it exceeds, calculate the remaining weight that can be added to the knapsack and add the
proportionate value based on the item's cost.
9. Return the final currentValue as the maximum value that can be obtained.
10. In the main function, define the number of items, maximum weight, values, and weights.
12. Call the knapsack function with the maximum weight and the vector of items.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Item {
int value;
int weight;
double cost;
return currentValue;
}
int main() {
int n = 4; // Number of items
int W = 15; // Maximum weight
cout << "The maximum value that can be obtained is: " << maxValue << endl;
return 0;
}
Output:
Time Complexity:
The time complexity of the Least Cost Branch and Bound approach for the Knapsack problem is O(n
log n), where n is the number of items. This is because the items are sorted based on their costs.