Daa Detention Work
Daa Detention Work
Daa Detention Work
Knapsack Problem:
• Given a knapsack with a weight limit and items with specific weights and values, the
objective is to maximize the total value without exceeding the weight. There are two
main versions:
o 0/1 Knapsack: Each item is either fully included or excluded (binary choice),
solved using dynamic programming.
o Fractional Knapsack: Items can be split into fractions, so it's possible to take
only a part of an item. This is solved greedily by taking items with the highest
value-to-weight ratio until the knapsack is full.
4) Explain binary search? Write algorithm for it. Consider the following elements of
array.
Index 1 2 3 4 5 6 7 8 9 10
Element -15 -10 0 7 12 30 45 58 82 104
• Binary search works on sorted arrays, reducing the search space by half each time,
which gives a time complexity of O(logn)O(\log n)O(logn).
• Algorithm:
1. Start with low at the start and high at the end of the array.
2. Find the middle element at mid = (low + high) / 2.
3. If array[mid] is the target, return mid.
4. If array[mid] is greater than the target, search in the left half by setting high =
mid - 1.
5. Otherwise, search in the right half by setting low = mid + 1.
6. Repeat until the target is found or low > high.
Quick Sort:
• Quick sort is an efficient sorting algorithm that works by selecting a pivot element and
partitioning the array around it, placing elements smaller than the pivot to the left and
larger to the right. This process is repeated recursively on each partition. The choice
of pivot greatly affects performance, but on average, the time complexity is O(nlogn).
• Divide and Conquer divides a problem into independent subproblems that don’t
overlap (e.g., merge sort). Each subproblem is solved separately, and results are
combined.
• Dynamic Programming is used when subproblems overlap (e.g., Fibonacci sequence)
by storing solutions to subproblems, thus reducing the number of calculations.
9)What is backtracking?
• Backtracking:
• In this problem, given a set of numbers, the goal is to find subsets that sum to a
specific target value. Using recursion or dynamic programming, you decide for each
element to include it in the subset or exclude it, testing if any combination meets the
target. Dynamic programming helps store solutions to subproblems (e.g., for each
subset sum up to the target).
• This problem seeks the shortest path between every pair of nodes in a weighted graph.
The Floyd-Warshall algorithm solves it using dynamic programming by checking if
the path between each pair can be minimized through an intermediate node. The
algorithm iterates through each node as an intermediary and updates path lengths.
12)Define:
i. P-class problem
ii. Np hard problem
iii. Np complete problem
• Asymptotic Notations:
15)Evaluate 9T(n/3) + n.
• Evaluate 9T(n/3) + n:
• This recurrence can be evaluated using the Master theorem, which gives solutions for
recurrences of the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)T(n)=aT(n/b)+f(n),
allowing us to determine asymptotic behavior based on values of aaa, bbb, and
f(n)f(n)f(n).
16)Describe an algorithm for Merge Sort and find its time complexity.
• Merge Sort splits an array into halves, recursively sorts each half, and merges them.
This requires O(nlogn)O(n \log n)O(nlogn) time because it divides the array
O(logn)O(\log n)O(logn) times and merges takes O(n)O(n)O(n) at each level.
17) Solve using Strassen’s matrix multiplication, and Calculate its time complexity.
• Strassen’s algorithm splits matrices into quadrants and recursively computes sums and
products to reduce operations. It’s faster than conventional methods, particularly for
large matrices, due to its complexity of O(n2.81)O(n^{2.81})O(n2.81).
18)Apply branch and bound technique to solve travelling salesman problem for the graph
whose matrix is
20 30 10 11
15 16 4 2
3 5 2 4
19 6 18 3
16 4 7 16
• Branch and Bound optimizes the traveling salesman problem by exploring paths and
using bounds (cost limits) to prune paths that exceed known solutions. This approach
prevents unnecessary path calculations.
• This problem assigns colors to graph vertices so no two adjacent vertices share the
same color. For example, in scheduling exams for courses with overlapping students,
each course is a vertex, and a color represents a timeslot.
20) Solve the Fractional Knapsack problem Given n = 5 objects and a knapsack
• Items are sorted by value-to-weight ratio, and the knapsack is filled by taking items
with the highest ratios first. If the knapsack reaches capacity with only part of an item,
that fraction is taken. This greedy approach ensures optimal total value.
• In this problem, jobs must be scheduled to maximize profit within given deadlines.
Jobs are first sorted by profit, and then scheduled in the latest possible time slots
before their deadlines, maximizing total profit within the given time constraints.
0 4 5
2 0
-3 0
• Huffman Coding:
• Huffman coding builds a binary tree from character frequencies, assigning shorter
codes to more frequent characters. Each character's frequency determines its path
length, so the total length of encoded data is minimized, making it a powerful
technique for data compression.