Professional Documents
Culture Documents
DAA UNIT 3 - Finalneed To Edit PDF
DAA UNIT 3 - Finalneed To Edit PDF
- IT
UNIT III
COMPILED BY
VERIFIED BY
UNIT III
PART A
1. Define the Single source shortest path algorithm.[N/D2019] Define the single
source shortest path problem. [M/J 2016]
2. State Assignment problem. (M/J 2016).(N/D2019)
3. State principles of optimality.[M/J 12,14,N/D 10,13,14][A/M 2019]
4. What is the constraint for binary search tree insertion.[A/M2019]
5. Write down the optimization technique used for Warshall's algorithm. State
the rules and assumptions which are implied behind that. [A/M 15]
6. List out memory functions used under dynamic programming. [A/M 15]
7. What is knapsack problem? [A/M 13, M/J 13]
8. Write control abstraction for the ordering paradigm. [A/M 13]
9. Differentiate between subset paradigm and ordering paradigm. [N/D 12]
10. What is the drawback of Greedy algorithm? [A/M 12]
11. Differentiate greedy method and dynamic programming. [M/J 12]
12. What is 0/1 knapsack problem. [N/D 13,12,14]
13. List advantages of dynamic programming. [M/J 14]
14. How efficient is Prim's algorithm. [N/D 06]
15. What do you meant by Huff man code? [N/D 06]
16. Write the pseudo code for Warshall’s algorithm. [N/D 08]
17. What is Greedy Algorithm?
18. What are the applications of Greedy Method?
19. What is Minimum Spanning Tree?
20. What are the algorithms to solve MST?
21. What is Prim’s algorithm?
22. What is kruskal’s algorithm?
PART B
1. i) Given the mobile numeric keypad. You can only press buttons that are
up,left,right or down ot the first number pressed to obtain the subsequent numbers.
You are not allowed to press bottom row corner buttons (i.e * and #). Given a
number N, how many key strokes will be involved to press the given number. What
is the length of it? Which dynamic programming technique could be used to find
solution for this? Explain each step with the help of a pseudo code and derive its
time complexity. [A/M 15] (12)
ii) How do you construct a minimum spanning tree using Kruskal's algorithm?
Explain. [A/M 15](4)[N/D2019]
2. i) Construct a Huffman tree by using these nodes.
Value A B C D E F
Frequency 5 25 7 15 4 12
4
c
a
2 6 1
b 3 d
NOTES
UNIT III
PART A
1. Define the Single source shortest path algorithm.[N/D2019] Define the single
source shortest path problem. [M/J 2016]
Dijkstra's algorithm solves the single source shortest path problem of finding
shortest paths from a given vertex( the source), to all the other vertices of a weighted
graph or digraph. Dijkstra's algorithm provides a correct solution for a graph with non
negative weights.
2. State Assignment problem. (M/J 2016).(N/D2019)
There are n people who need to be assigned to execute n jobs, one person per
job. (That is, each person is assigned to exactly one job and each job is assigned to
exactly one person.) The cost that would accrue if the ith person is assigned to the jth
job is a known quantity C[i,j] for each pair i,j = 1, 2, ..., n. The problem is to find an
assignment with the minimum total cost.
6. List out memory functions used under dynamic programming. [A/M 15]
If we are given n objects and a knapsack or a bag in which the object i that has
weight wi is to be placed. The knapsack has capacity W.then the profit can be earned is
pixi the objective is to obtain filling of knapsack with maximum profit earned.
Algorithm Store(n,limit)
{
j=0;
for(i<-1 to n) do
{
Write(“Append program”,i);
Wrtie(“Permutation for tape”,j);
j=(j+1) mod limit;
}
}
Subset paradigm:
At each step the decision about the input is made.That means at each step it
is decided whether the particular input is in an optimal solution or not.
Ordering paradigm:
0/1 Knapsack Problem: In this item cannot be broken which means thief
should take the item as a whole or should leave it. That's why it is called 0/1
knapsack Problem. Each item is taken or not taken. Cannot take a fractional
amount of an item taken or take an item more than once
16. Write the pseudo code for Warshall’s algorithm. [N/D 08]
35. Mention the time efficiency and space efficiency of Warshall’s Algorithm.
Solution:
In order to understand the problem let us draw the numeric keypad as follows
If we start with number 0 valid numbers will be 00,08 terefore count = 2.
If we start with number 1 valid numbers will be 11,12,14 therefore count = 3.
If we start with number 2 valid numbers will be 22,21,23,25 therefore count = 4.
If we start with number 3 valid numbers will be 33,32,36 therefore count = 3. Counting in
this fashion, we will get different counts for each number.
In pressing the valid numbers, we need to traverse from one key to another in left,right,up
and down. This leads to many repeated traversals on smaller paths to find all possible
longer paths.
Let us assume length of the path as N
If N= 4,then traversal for the button 8 will be
8 -> 5 -> 2
8 -> 7 -> 4
8 -> 9 -> 6
In this problem is solved using dynamic programming then two properties optimal
substructure and overlapping sub problem are used.
ii) How do you construct a minimum spanning tree using Kruskal's algorithm?
Explain. [A/M 15](4)[N/D2019]
The another greedy algorithm for the minimum spanning tree problem that also
always yielda an optimal solution. It is named Kruskal’s algorithm.
Algorithm:
ALGORITHM Kruskal(G)
//Kruskal’s algorithm for constructing minimum spanning tree
//Input: A weighted connected graph G = <V,E>
//Output: ET <- 0: ecounter <- 0
k <- 0
while ecounter < l V l – 1 do
k <- k + 1
if ET U { eik } isacyclic
ET <- ET U { eik } ; ecounter <- ecounter = 1
return ET
Example:
2 Construct a Huffman tree by using these nodes.
Value A B C D E F
Frequency 5 25 7 15 4 12
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with frequency equal to the sum of the two nodes
frequencies. Make the first extracted node as its left child and the other extracted node as
its right child. Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is
the root node and the tree is complete.
Value A B C D E F
Frequency 5 25 7 15 4 12
Solution:
Step 1: According to the Huffman coding we arrange all the elements (values) in
ascending order of the frequencies.
Value E A C F D B
Frequency 4 5 7 12 15 25
Value C EA F D B
Frequency 7 9 12 15 25
Step 4: Next elements are F and D so we construct another subtree for F and D.
Value CEA B FD
Frequency 16 25 27
Step 5: Taking next value having smaller frequency then add it with CEA and
insert it at correct place.
Value FD CEAB
Frequency 27 41
Step 6: We have only two values hence we can combined by adding them.
Huffman Tree
Value FDCEAB
Frequency 68
Now the list contains only one element i.e. FDCEAB having frequency 68 and this
element (value) becomes the root of the Huffman tree.
ii). Write an algorithm to construct the optimal binary search tree given the roots
r(i,j), 0<= i<=j<=n. Also prove that this could be performed in time O(n). [A/M
08,11,15 N/D 13,14]
Problem:
Given n keys a1 < …< an and probabilities p1, …,searching for them, find a BST with
a minimum average number of comparisons in successful search. Since total number of
BSTs with n nodes is given by C(2n,n)/(n+1), which grows exponentially, brute force is
hopeless.
Example:
What is an optimal BST for keys A, B, C, and search probabilities 0.1, 0.2, 0.4, and 0.3,
respectively?
B D
Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys
ai < …< aj , where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all BSTs with some ak (i ≤ k
≤ j ) as their root; T[i,j] is the best among them.
ak
z Optimal Optimal
BST for BST for
a i , ..., ak-1 a k+1 , ..., aj
B D
key A B C D
probability 0.1 0.2 0.4 0.3
The tables below are filled diagonal by diagonal: the left one is filled using the recurrence
the right one, for trees’ roots, records k’s values giving the minimam .
0 1 2 3 4 0 1 2 3 4
1 0 .1 .4 1.1 1.7 1 1 2 3 3
2 0 .2 .8 1.4 2 2 3 3
3 0 .4 1.0 3 3 3
4 0 .3 4 4
5 0 5
Optimal BST:
• Recall that if we can make the lower bound higher then we will get more pruning
• Note that in order to complete the tour we need to leave node B, C, D, and E
– The min edge we can take leaving B is min(14, 7, 8, 7) = 7
– Similarly, C=4, D=2, E=4
• This implies that at best the future underestimate can be 7+4+2+4=17
• 17 + current cost of 14 = 31
– This is much higher than 8 + 14 = 22
4. Explain Warshall’s Algorithm for Transitive Closure of directed graph.
Constructs transitive closure T as the last matrix in the sequence of n-by-n matrices
R(0), … , R(k), … , R(n) where R(k)[i,j] = 1 iff there is nontrivial path from i to j with only the
first k vertices allowed as intermediate Note that R(0) = A (adjacency matrix), R(n) = T
(transitive closure)
On the k-th iteration, the algorithm determines for every pair of vertices i, j if a path exists
from i and j with just vertices 1,…,k allowed as intermediate R(k)[i,j] = R(k-1)[i,j]
(path using just 1 ,…,k-1) R(k-1)[i,k] and R(k-1)[k,j] (path from i to k and from k to j using
just 1 ,…,k-1)
Example:
Analysis:
Floyd’s Algorithm:
Matrix generation:
Pseudo code :
Application:
Analysis:
Time efficiency: 0(n2)
Space efficiency: Matrices can be written over their predecessors.
6. Explain Knapsack problem and Memory functions in dynamic programming.
Algorithm:
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in
shortest path tree, i.e., whose minimum distance from source is calculated and finalized.
Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as
INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
a) Pick a vertex u which is not there in sptSetand has minimum distance value.
b) Include u to sptSet.
c) Update distance value of all adjacent vertices of u. To update the distance values,
iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value
of u (from source) and weight of edge u-v, is less than the distance value of v, then update
the distance value of v.
DIJKSTRA(G, w, s)
The set sptSetis initially empty and distances assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with minimum
distance value. The vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After
including 0 to sptSet, update distance values of its adjacent vertices. Adjacent vertices of 0
are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8. Following subgraph
shows vertices and their distance values, only the vertices with finite distance values are
shown.
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). The vertex 1 is picked and added to sptSet. So sptSet now becomes {0, 1}.
Update the distance values of adjacent vertices of 1. The distance value of vertex 2
becomes 12.
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}. Update the distance values
of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15 and 9
respectively).
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}. Update the distance
values of adjacent vertices of 6. The
distance value of vertex 5 and 8 are
updated.
We repeat the above steps until sptSet doesn’t include all vertices of given graph. Finally,
we get the following Shortest Path Tree (SPT).
8. Discuss about the algorithm and pseudocode to find the Minimum Spanning Tree
using Prim's Algorithm. Find the Minimum Spanning Tree for the graph shown
below. Am Discuss about the efficiency of the algorithm. (16) [M/J2016]
4
c
a
2 6 1
b 3 d