0% found this document useful (0 votes)
27 views2 pages

Week 5 Tutorial

The document outlines a tutorial assignment for a BTech course on Design and Analysis of Algorithms, covering topics such as the Knapsack Problem, Huffman coding, and the Traveling Salesman Problem. It includes specific questions requiring optimal solutions, Huffman tree construction, and scheduling classes without conflicts. The assignment aims to enhance understanding of algorithmic concepts through practical problems.

Uploaded by

hemaj903
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Week 5 Tutorial

The document outlines a tutorial assignment for a BTech course on Design and Analysis of Algorithms, covering topics such as the Knapsack Problem, Huffman coding, and the Traveling Salesman Problem. It includes specific questions requiring optimal solutions, Huffman tree construction, and scheduling classes without conflicts. The assignment aims to enhance understanding of algorithmic concepts through practical problems.

Uploaded by

hemaj903
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course/ Batch: BTech/ SCSET Course code: CSET244

Tutorial Assignment: Week 5 Semester: Even

Session: 2024-2025 Course Type: Core

Course Name: Design and Analysis of Algorithms

Tutorial Title: Knapsack Problem, Huffman code, Activity selection problem

Q1. Consider the weights and values of the items listed below. The task is to pick a subset of these
items such that their total weight is no more than 15 Kgs and their total value is maximized.
Moreover, a fraction of each item may be taken. Find the optimal solution to the given problem.

Item Weight (Kg) Value

A 4 40

B 7 42

C 5 25

D 3 24

E 6 30

(i) Find the optimal selection of items.


(ii) Write the final maximum value obtained.
(iii) Mention which item is taken fractionally (if any).

Q2. i) How many bits are at least required for encoding the message “BANANAAPPLE” using
Huffman coding?
Construct the Huffman tree for optimal coding.

ii) Decode the bit string:1011001010110


using the Huffman code you generated.

iii) Find the time complexity of constructing an optimal Huffman code using Huffman’s algorithm.

Q3: In the Traveling Salesman Problem (TSP), a salesman wants to visit each of n cities and return to
his starting point while traveling the shortest possible distance. The conditions of this tour are as
follows:

• The salesman can visit each city exactly once.


• The goal is to find the best route that minimizes travel distance (or cost).

Consider the following distances between four cities:


City A B C D

A 0 12 18 25

B 12 0 30 15

C 18 30 0 20

D 25 15 20 0

The above matrix is symmetric in which every element denotes the distance between two cities, e.g.,
(i, j) denotes the distance between city i and j. Every city is connected to other cities.

Consider the following greedy method:

1. Select a starting city.

2. Find the nearest city to your current one and go there if it was not visited earlier.

3. If there are still cities not yet visited, repeat step 2. Else, return to the starting city.

If the salesman wants to start his tour from city A, find the minimum total travel distance of his tour
using the greedy method described above. Write the sequence of the cities that the salesman would
visit.

Q4(A). Consider the weights and values of the items listed below. The task is to pick a subset of these
items such that their total weight is no more than 20 Kgs and their total value is maximized.
Moreover, a fraction of each item may be taken. Find the optimal solution to the given problem.

Item Weight (Kg) Value

1 5 30

2 10 40

3 15 45

4 4 20

Q5 (a). How many bits are at least required for encoding the message“AABBBCCCCDDDD”

using Huffman coding? Construct the Huffman tree for optimal coding.

(b) Using the Huffman code generated in Q2(A), decode the following bit string: 000110111010

(c) Find the time complexity of constructing an optimal code using Huffman’s algorithm.

Q6. Given a set of 10 classes along with their start time (si) and finish time (fi), where si < fi. Ai =
{C1, C2, C3, C4, C5, C6, C7, C8, C9, C10} Si = (1, 2, 3, 4, 7, 8, 9, 9, 11, 12) Fi = (3, 5, 4, 7, 10, 9,
11, 13, 12, 14) Suppose that you are in charge of scheduling a class into classrooms. At a time, only
one class can be run into a classroom. You are allowed to schedule a maximum number of classes in a
classroom. Find the minimum number of classrooms required to schedule all 10 classes without any
conflict (non-overlapping classes).

Common questions

Powered by AI

Huffman coding is solved using a greedy algorithm that constructs an optimal prefix-free binary tree. The time complexity is O(n log n), where n is the number of unique characters or symbols to encode, due to the priority queue operations required to repeatedly select and merge the two least frequent nodes .

Merging the two nodes with the least frequency ensures that less common characters appear deeper in the tree, and more common characters have shorter codes. This minimizes the total weighted path of the tree, used to encode the message with the shortest possible bit string length, adhering to the greedy algorithm's principle .

To construct a Huffman tree, determine the frequency of each character in the message. For "BANANAAPPLE", the frequencies are: B=1, A=3, N=2, P=2, L=1, E=1. Create nodes for each character and construct the tree by merging the two least frequent nodes iteratively until one node remains. Then assign binary codes starting from root to leaves. The encoded message requires at least 29 bits .

The greedy approach in TSP often does not yield the optimal solution compared to dynamic programming or backtracking. It simply chooses the local optimal solution at each step (nearest neighbor), which can lead to longer total paths overall. Dynamic programming considers multiple possible paths at each stage, providing a better approximation to the optimal tour, though it incurs higher computational costs .

To solve the fractional knapsack problem, first calculate the value per kg for each item, then sort items in descending order of this value. Start picking items with the highest value per kg until the weight limit is reached. If the limit is exceeded, take the item fractionally. For the given weights and values (A: 40, B: 42, C: 25, D: 24, E: 30), the optimal solution is to take items in the order of B, A, and then C fractionally to fill the knapsack to 15 Kg, yielding a maximum value of 92.2 .

Allowing fractional selection of items in the knapsack problem leads to better utilization of available weight capacity compared to 0/1 knapsack problem, as it optimizes the value-to-weight ratio. It provides flexibility to achieve higher overall value, especially when the total weight limit is strictly enforced and cannot accommodate all items fully .

To construct an optimal Huffman code, list character frequencies: A=2, B=3, C=4, D=4. Build a binary tree by iteratively selecting and merging the nodes with the lowest frequencies. Assign binary values to each character by traversing the tree from root to leaf. This results in a minimum encoding of 24 bits .

To determine the minimum number of classrooms needed, first sort classes by their finishing times and then iterate through them, assigning each class to a room that becomes free the earliest. For the given set of start and finish times, 3 classrooms are required. This scheduling ensures each classroom is utilized optimally without overlaps .

Sorting activities by finish time facilitates the selection of the maximum number of non-overlapping activities. This ensures at every step, the earliest possible finishing activity is selected, optimizing the number of activities accommodated since choosing the earliest finishing activity leaves the maximum remaining time for subsequent selections .

Using the greedy approach, which involves selecting the nearest unvisited city, starting from City A results in the sequence A -> B -> D -> C -> A. The total minimum travel distance is 67 units, calculated as follows: start at A, travel to the nearest city B (12 units), then to D (15 units), then to C (20 units), and finally return to A (18 units).

You might also like