Daa PBL
Daa PBL
Answer:
The Greedy Approach is a problem-solving technique that makes a sequence of
choices, each of which looks best at the moment, with the hope of finding a global
optimum. In each step, it selects the option that seems to be the most beneficial without
considering future consequences.
Q.1 (b) What is the Hamiltonian Cycle Problem? Explain with an Example.
Answer:
The Hamiltonian Cycle Problem is a problem of determining whether there exists a
cycle in an undirected or directed graph that visits each vertex exactly once and returns
to the starting vertex.
Example: Consider a graph with vertices A,B,C,DA,B,C,D and edges connecting them.
If there exists a path like A→B→C→D→AA→B→C→D→A that visits each vertex
exactly once and returns to AA, then it is a Hamiltonian Cycle.
Q.1 (c) Compare Merge Sort and Quick Sort w.r.t Their Time Complexity and
Space Complexity.
Answer:
Merge Sort is stable and performs better with large data sets, while Quick Sort is
generally faster for small and medium datasets but has a higher worst-case time
complexity without optimizations.
Answer:
Given:
T(n)=2T(n−1)+O(n)T(n)=2T(n−1)+O(n)
T(n)=2T(n−1)+c⋅nT(n)=2T(n−1)+c⋅n
T(n)=2(T(n−2)+c⋅(n−1))+c⋅nT(n)=2(T(n−2)+c⋅(n−1))+c⋅n=22T(n−2)+2⋅c⋅(n−1)+c⋅n=22
T(n−2)+2⋅c⋅(n−1)+c⋅n
Second Iteration:
T(n)=22(T(n−3)+c⋅(n−2))+2⋅c⋅(n−1)+c⋅nT(n)=22(T(n−3)+c⋅(n−2))+2⋅c⋅(n−1)+c⋅n=23T
(n−3)+22⋅c⋅(n−2)+2⋅c⋅(n−1)+c⋅n=23T(n−3)+22⋅c⋅(n−2)+2⋅c⋅(n−1)+c⋅n
T(n)=2kT(n−k)+c(n⋅20+(n−1)⋅21+(n−2)⋅22+⋯+(n−k+1)⋅2k−1)T(n)=2kT(n−k)+c(n⋅20+(n
−1)⋅21+(n−2)⋅22+⋯+(n−k+1)⋅2k−1)
We stop expanding when we reach the base case, which we assume is T(0)T(0) (for
simplicity, or we can assume T(1)=dT(1)=d if given).
Let's say the base case is T(0)=dT(0)=d. This means we need n−k=0n−k=0, which gives
k=nk=n.
T(n)=2nT(0)+c(n⋅20+(n−1)⋅21+(n−2)⋅22+⋯+1⋅2n−1)T(n)=2nT(0)+c(n⋅20+(n−1)⋅21+(n−
2)⋅22+⋯+1⋅2n−1)
T(n)=2nd+c∑j=0n−1(n−j)⋅2jT(n)=2nd+cj=0∑n−1(n−j)⋅2j
Conclusion
The asymptotic solution to the recurrence relation is:
T(n)=O(2n)T(n)=O(2n)
Q.2 (a) How Strassen’s Algorithm Uses Divide and Conquer to Reduce the Time
Complexity of Matrix Multiplication?
Answer:
Given:
1. Initialize Nodes: Create a node for each character with its frequency:
○ a:50%a:50%, b:5%b:5%, c:10%c:10%, d:32%d:32%, e:3%e:3%.
2. Sort Nodes: Sort the nodes in ascending order based on frequency.
○ Order: e:3%e:3%, b:5%b:5%, c:10%c:10%, d:32%d:32%, a:50%a:50%.
3. Build the Tree:
○ Combine the two lowest frequencies:
■ e+b=3%+5%=8%e+b=3%+5%=8%.
○ Updated list: {8%,c:10%,d:32%,a:50%}{8%,c:10%,d:32%,a:50%}.
○ Combine the two lowest again:
■ 8%+10%=18%8%+10%=18%.
○ Updated list: {18%,d:32%,a:50%}{18%,d:32%,a:50%}.
○ Combine the two lowest:
■ 18%+32%=50%18%+32%=50%.
○ Updated list: {50%,a:50%}{50%,a:50%}.
○ Combine the last two nodes:
■ 50%+50%=100%50%+50%=100%.
○ This completes the Huffman Tree.
Step 2: Assign Codes Assign binary codes based on the tree structure, with shorter
codes for more frequent characters.
1. Fixed-Length Encoding:
○ For 5 characters, each character would require ⌈log2(5)⌉=3⌈log2(5)⌉=3
bits.
○ Total bits required for 100 characters: 100×3=300100×3=300 bits.
2. Variable-Length (Huffman) Encoding:
○ Calculate the total bits required using the Huffman encoding for each
character based on frequency.
3. Space Savings:
○ Calculate the difference in bits between fixed-length and variable-length
encoding.
○ Calculate the percentage savings:Savings=Fixed-Length Bits−Huffman
BitsFixed-Length Bits×100%Savings=Fixed-Length BitsFixed-Length
Bits−Huffman Bits×100%
Question 3(a): Define various asymptotic notations with the help of graph and
mathematical function.
Expansion:
● Asymptotic Notations:
○ Asymptotic notations are mathematical tools used to describe the running
time complexity of algorithms in terms of input size nn as n→∞n→∞.
○ Big-O Notation O(g(n))O(g(n)): Describes an upper bound.
f(n)=O(g(n))f(n)=O(g(n)) if there exist constants c>0c>0 and n0n0such
that f(n)≤c⋅g(n)f(n)≤c⋅g(n) for all n≥n0n≥n0.
○ Big-Omega Notation Ω(g(n))Ω(g(n)): Describes a lower bound.
f(n)=Ω(g(n))f(n)=Ω(g(n)) if there exist constants c>0c>0 and n0n0such
that f(n)≥c⋅g(n)f(n)≥c⋅g(n) for all n≥n0n≥n0.
○ Big-Theta Notation Θ(g(n))Θ(g(n)): Describes a tight bound.
f(n)=Θ(g(n))f(n)=Θ(g(n)) if f(n)=O(g(n))f(n)=O(g(n)) and
f(n)=Ω(g(n))f(n)=Ω(g(n)).
Q3(b): Single Source Shortest Path using Dijkstra's Algorithm
Answer:
Assume that the nodes in the graph are labeled AA, BB, CC, DD, and EE, and the
weights of the edges are as provided in the image. Here’s a summary of the edges for
easy reference:
Our goal is to find the shortest path from node AA to all other nodes using Dijkstra’s
Algorithm.
Step-by-Step Execution
After executing Dijkstra’s Algorithm, the shortest distances from node AA to all other
nodes are:
● dist(A)=0dist(A)=0
● dist(B)=2dist(B)=2
● dist(C)=3dist(C)=3
● dist(D)=6dist(D)=6
● dist(E)=7dist(E)=7
Shortest Paths
The shortest paths from AA to each node can also be traced back through the nodes’
predecessors:
● A→BA→B: Distance = 2
● A→CA→C: Distance = 3
● A→B→DA→B→D: Distance = 6
● A→B→EA→B→E: Distance = 7
Q.4 (a) Define Various Operations on Disjoint Sets. How the Time Complexity of
Operations on Disjoint Sets Can Be Reduced Using Heuristics.
Answer:
Operations on Disjoint Sets:
Optimizations:
● Union by Rank: Attach smaller tree under the root of the deeper tree.
● Path Compression: Make nodes in the path point directly to the root.
Answer:
Part 1:
Part 2:
We place the first queen in the first row, starting from the left. Let's put the queen in
column 0 of row 0.
In row 1, we cannot place a queen in column 0, because it would be in the same column
as the queen in row 0. We also cannot place it in column 1 because it’s on the diagonal
with the first queen.
The first safe position in row 1 is column 2, so we place the queen there.
Step 4: Move to the Next Row (Row 2)
In row 2, columns 0 and 2 are unavailable due to the queens in rows 0 and 1, and
column 3 is unavailable due to the diagonal. The only safe position is column 1, so we
place the queen there.
In row 3, columns 0, 1, and 2 are either in the same column or diagonal with previously
placed queens. We place the last queen in column 3, which is the only safe position.
This configuration satisfies the N-Queens constraints: no two queens share the same
row, column, or diagonal. So, we have found one solution.
To find other solutions, we backtrack by moving the queens to other possible positions
and checking if they satisfy the constraints. Here’s another valid solution: