Unit 3
Unit 3
Unit 3
Unit: 1
Qualification:
1. https://www.youtube.com/watch?v=0IAPZzGSbME
2. https://www.youtube.com/watch?v=yOl-EpY39kM
• First, we will start with the internet which is very much important for our daily life and we
cannot even imagine our life without the internet and it is the outcome of clever and
creative algorithms. Numerous sites on the internet can operate and falsify this huge
number of data only with the help of these algorithms.
• The everyday electronic commerce activities are massively subject to our data, for
example, credit or debit card numbers, passwords, OTPs, and many more. The centre
technologies used incorporate public-key cryptocurrency and digital signatures which
depend on mathematical algorithms.
• Even an application that doesn't need algorithm content at the application level depends
vigorously on the algorithm as the application relies upon hardware, GUI, networking, or
object direction and all of these create a substantial use of algorithms.
• There are some other vital use cases where the algorithm has been used such as if we
watch any video on YouTube then next time we will get related-type advice as
recommended videos for us.
CO4 To analyze and apply different optimization techniques like Knowledge, Analysis And
dynamic programming, backtracking and Branch & Bound to Apply
solve the complex problems
CO5 To understand the advanced concepts like NP Completeness and Knowledge, Analysis and
Fast Fourier Transform, to analyze and apply String Matching, Apply
Approximation and Randomized Algorithms to solve the
complex problems
PO1
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO12
1
ACSE0401.1 3 3 3 3 2 - - - 2 2 - 3
ACSE0401.2 3 3 3 3 2 2 - 1 1 1 - 3
ACSE0401.3 3 3 2 3 3 2 - 2 1 1 2 3
ACSE0401.4 3 3 3 3 2 2 - 2 2 1 3 3
ACSE0401.5 2 2 2 2 2 2 - 2 1 1 1 2
Average 2.8 2.8 2.6 2.8 2.2 1.6 - 1.8 1.4 1.2 1.2 2.8
B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
Design and Analysis of Algorithms
Time: 3 Hours Total Marks:
100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1.Q.No.
Attempt all questions in brief.
Question 2 x 10 =CO
Marks 20
1 2
2 2
. .
10 2
SECTION B
2. Attempt any three of the following: 3 x 10 = 30
SECTION C
3. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10
12/22/2024 Sachin Singh DAA Unit III 14
End Semester Question Paper Templates
4. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10
5. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10
1 10
2 10
• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.
• Recap
• Flow Chart
• Algorithm
• Its basic idea is to decompose a given problem into two or more similar, but
simpler, subproblems, to solve them in turn, and to compose their solutions to
solve the given problem.
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
• Merge Sort
• To sort an array A[p . . r]:
• Divide
– Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
• Conquer
– Sort the subsequences recursively using merge sort
– When the size of the sequences is 1 there is nothing more to do
• Combine
– Merge the two sorted subsequences
p q r
1 2 3 4 5 6 7 8
Algorithm.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
Divide 5 2 4 7 1 3 2 6 q=4
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
Conquer 1 2 2 3 4 5 6 7
and
Merge 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
• Divide:
– compute q as the average of p and r: D(n) = (1)
• Conquer:
– recursively solve 2 subproblems, each of size n/2 2T
(n/2)
• Combine:
– MERGE on an n-element subarray takes (n) time
C(n) = (n)
(1) if n =1
T(n) = 2T(n/2) + (n) if n > 1
A[i]≤p A[i]>p
Binary Search
Binary search is the most popular Search algorithm.It is efficient
and also one of the most commonly used techniques that is used to
solve problems.
• Divide the points in two subsets S1 and S2 of n/2 points by drawing a vertical
line at x=c.
• Select the closest pair among the closest pair of the left subset (distance d 1),
of the closest pair of the right subset (distance d 2), and the closest pair with
points from both sides.
Convex Hull
• This is actually a half-space hull finder
– You start with the leftmost (A) and rightmost
(B) points and all the points above them
– It finds the half hull to the top side
• One can then find the entire hull by running the same
algorithm on the bottom points
Convex Hull
• Recursively conquer each list of points.
Convex Hull
• Finding point farthest away from line P1P2 can be done in linear time
• This gives same efficiency as quicksort:
– Worst case: Θ(n2)
– Average case: Θ(n log n)
M1 + M4 - M 5 + M7 M 3 + M5
=
M2 + M4 M1 + M3 - M 2 + M6
• Sub Matrices
• M1 = (A00 + A11) * (B00 + B11)
• M2 = (A10 + A11) * B00
• M3 = A00 * (B01 - B11)
• M4 = A11 * (B10 - B00)
• M5 = (A00 + A01) * B11
• M6 = (A10 - A00) * (B00 + B01)
• M7 = (A01 - A11) * (B10 + B11)
2. A SORTING TECHNIQUE THAT GUARANTEES, THAT RECORDS WITH THE SAME PRIMARY KEY OCCURS, IN
THE SAME ORDER IN THE SORTED LIST AS IN THE ORIGINAL UNSORTED LIST IS SAID TO BE:
a) Stable
b) Consistent
c) External
d) Linear
• Prerequisite
• Graph
• Different operations
• Notations
• Recap
– Algorithm analysis
– Analysis of different sorting and searching algorithms using
divide and conquer.
• Graph
• Directed Graph
• Undirected Graph
• Degree of Graph
• Graph Representation
• Spanning Tree
• Kruskal’s Method
• Prims Method
• Single-Source Shortest Paths
• Dijkstra's Algorithm
• Knapsack Problem
Directed graph (or digraph) It is a graph with directed edges. Edges have
directions so they are represented by arrows. Each edge leaves a vertex
and enters a vertex.
In an undirected graph,
The out-degree and the in-
degree are not defined.
Only the degree of a vertex is
defined. The degree of vertex 2
is 3.
1. Adjacency-list representation
2. Adjacency-matrix representation
An array of |V| lists, one for each vertex. For vertex u, its adjacency
list contains all vertices adjacent to u. The adjacency-list
representation is usually preferred, because it provides a compact
way to represent sparse graph- those for which |E| is much less
than |V|2 .
Adjacency-matrix representation
An Adjacency-matrix representation may be preferred, when the
graph is dense -|E| is close to |V|2 or when we need to be able to tell
quickly if there is an edge connecting two given vertices.
Algorithm of Kruskal's
For any graph, G=(V,E), finding an MST using Kruskal's algorithm involves
the following steps –
• Sort all the edges of the graph in ascending order of their weights.
• Check the edge with minimum weight, if including it in the answer forms
a cycle discard it, otherwise include it in the answer.
Prims Algorithm
The tree starts from an arbitrary root vertex r and grows until the tree
spans all the vertices in V.
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that
has minimum weight.
Step 4: Add the selected edge and the vertex to the minimum spanning
tree T.
[END OF LOOP]
Step 5: EXIT
B 5 C
Select the shortest
edge connected to
3 any vertex already
6 4 connected.
8
8 DC 4
A F D
7
5
4 2
B 5 C
3 Select the shortest
6 4 edge connected to
8 any vertex already
8 connected.
A F D
7 EF 5
5
4 2
● E.g., a road map: what is the shortest path from Chapel Hill to
Charlottesville?
Example
Let us understand the algorithm with following example graph.
Let the given source vertex be 0. Initialize all distances as infinite, except the
distance to the source itself. Total number of vertices in the graph is 5, so all edges
must be processed 4 times.
Let all edges are processed in the following order: (B, E), (D, B), (B, D), (A, B), (A, C),
(D, C), (B, C), (E, D). We get the following distances when all edges are processed the
first time. The first row shows initial distances. The second row shows distances
when edges (B, E), (D, B), (B, D) and (A, B) are processed. The third row shows
distances when (A, C) is processed. The fourth row shows when (D, C), (B, C) and (E,
D) are processed.
The first iteration guarantees to give all shortest paths which are at most 1 edge long.
We get the following distances when all edges are processed second time (The last
row shows final values).
The second iteration guarantees to give all shortest paths which are at most 2
edges long. The algorithm processes all edges 2 more times. The distances are
minimized after the second iteration, so third and fourth iterations don’t update
the distances.
Knapsack Problem
There are two versions of the problem:
•Each item i has some weight wi and benefit value bi (all wi and W are
integer values)
Algorithm:
Example-
Let us consider that the capacity of the knapsack W = 60 and the list of
provided items are shown in the following table −
Item A B C D
Weight 40 10 20 24
Ratio (piwi)
(piwi) 7 10 6 5
he provided items are not sorted based on pi/wi. After sorting, the items are as shown in the following t
Item B A C D
Weight 10 40 20 24
Ratio (piwi)
(piwi) 10 7 6 5
Solution
After sorting all the items according to pi/wi. First all of B is chosen as weight of B is less
than the capacity of the knapsack. Next, item A is chosen, as the available capacity of
the knapsack is greater than the weight of A. Now, C is chosen as the next item.
However, the whole item cannot be chosen as the remaining capacity of the knapsack is
less than the weight of C.
Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item
can be selected.
And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
This is the optimal solution. We cannot gain more profit selecting any different
combination of items.
12/22/2024 Sachin Singh DAA Unit III 98
Greedy Algorithm (CO3)
Huffman Coding
• we make a new node and then pick the first two nodes from the
sorted character and make the first node left child of the new
node and second node as the right child of the new node.
• The value of the new node is the summation of the values of the
children nodes.
1. n ←[C]
2. Q ←C
3. For i ← 1 to n-1
4. do allocate a new node Z
5. left[z] ← x ← EXTRACT-MIN(Q)
6. Right[z] ← y ← EXTRACT-MIN(Q)
7. F[z] ← F[x] + F[y]
8. Insert (Q, Z)
9. Return EXTRACT-MIN(Q)
Huffman Coding
a)Find a time slot i, such that slot is empty and i < deadline and i is greatest.Put the
job in this slot and mark this slot filled.
1 2 3 4 5 6 7
di 4 2 4 3 1 4 6
wi 70 60 50 40 30 20 10
Solution: According to the Greedy algorithm we sort the jobs in decreasing order of their
penalties so that minimum of penalties will be charged.
In this problem, we can see that the maximum time for which uniprocessor machine will
run in 6 units because it is the maximum deadline.
w5 + w6 = 30 + 20 = 50 (2 3 4 1 7 5 6)
(2 4 1 3 7 5 6)
There can be many other schedules but (2 4 1 3 7 5 6) is optimal.
1. m ←i +1
2. While m < j and sm < fi
3. do m ← m + 1
4. if m < j
5. then return {am} U Recursive Activity Selector
(s, f, m, j)
6. else Return ∅
Activity Selection
Greedy Activity Selector (s, f)
1. n ←length[s]
2. A ← {a1}
3. i←1
4. For m ← 2 to n
5. do if sm >= fi
6. then A ← A U {am}
7. i←m
8. Return A
A person can perform at most two activities. The maximum set of activities that can be
executed is {0, 2} [ These are indexes in start[] and finish[].
A person can perform at most four activities. The maximum set of activities that can be
executed is {0, 1, 3, 4} [ These are indexes in start[] and finish[].
Q1 Solve the 0-1 knapsack problem where you have five objects whose
weights are 1, 2, 5, 6, 7 and whose values are 1, 8, 6, 12, 18 and
maximum weight is 12. [CO3]
Q2 Solve the 0-1 knapsack problem where you have five objects whose
weights are 6, 7, 1, 2, 5 and whose values are 12, 18, 1, 8, 6 and
maximum weight is 13. [CO3]
Q3 What is the optimal Huffman Code for the following set of
frequencies? a: 05, b: 48, c: 07, d: 17, e: 10, f: 13
[CO3]
Q4 Find the optimal solution for the knapsack 0/1 problem by using the
dynamic programming approach? Consider, N = 4, W =5, (w1, w2,
w3, w4) = (2, 3, 4, 5) and (b1, b2, b3, b4) = (3, 4, 5, 6) [CO3]
Thank You