Unit3
Unit3
Unit3
Unit: 3
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/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 12
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
• MergeSort Algorithm
• The MergeSort function repeatedly divides
the array into two halves until we reach a
stage where we try to perform MergeSort on
a subarray of size 1 i.e. p == r.
• After that, the merge function comes into
play and combines the sorted arrays into
larger arrays until the whole array is merged.
Average Case :
A[i]≤p A[i]>p
M1 + M4 - M 5 + M7 M 3 + M5
=
M2 + M4 M1 + M3 - M 2 + M6
• The first step is to find out the farthest two points in the plane:
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
Greedy Algorithm
• To begin with, the solution set (containing answers) is
empty.
• At each step, an item is added to the solution set until
a solution is reached.
• If the solution set is feasible(which is satisfying the
condition given in question ), the current item is kept.
• Else, the item is rejected and never considered again.
Item or object : 1 2 3 4 5
Weight : 5 10 15 22 25
Value or profit: 30 40 45 77 90
5 25 90 3.6
60 Ø 0
45-25=20 1, 2, 5 160
// wt.20 can be
added out of 22
from item 4 .//
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 54
Greedy Algorithm : Knapsack (CO3)
Now,
• Knapsack weight left to be filled is 20 kg but item-4
has a weight of 22 kg.
• Since in fractional knapsack problem, even the fraction
of any item can be taken.
• So, knapsack will contain the following items-
• < 1 , 2 , 5 , (20 out of 22) 4 >
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/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 59
Greedy Algorithm (CO3)
Algorithm:
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[].
5 9 a1
1 2 a2
3 4 a3
0 6 a4
5 7 a5
8 9 a6
Solution :
Step 1: Sort the given activities in ascending order
according to their finishing time.
The table after we have sorted it:
Start Finish Activity
Time (s) Time (f) Name
1 2 a2
3 4 a3
0 6 a4
5 7 a5
5 9 a1
12/15/2024
8 9
ANKITA SHARMA ACSE0401
a6
UNIT 3 71
Greedy Algorithm : Activity Selection (CO3)
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.
A B C D E
Deadline:- 2 1 2 1 3
Penalty :- 100 19 27 25 15
(**have to make least penalty)
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 80
Task Scheduling-Greedy Algorithm
Solution: According to the Greedy algorithm we
sort the jobs in decreasing order of their penalties so
that minimum of penalties will be charged.Let’s
Schedule A first , because if it gets miss Penalty of
100 will occur.
TASK : A C D B E
DEADLINE: 2 2 1 1 3
PENALTY : 100 27 25 19 15
0 1 2 3 4 5
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 81
Task Scheduling-Greedy Algorithm
0 1 2 3 4 5
C , A , E were the part of schedule but we get the
penalty for D , B
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 83
Task Scheduling-Greedy Algorithm
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
Step 1 :
Step 2:
Step 3 :
Step 4 :
Step 5 :
Step 6 :
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.
In data structures,
•Shortest path problem is a problem of finding
the shortest path(s) between vertices of a given
graph.
•Shortest path between two vertices is a path
that has the least cost as compared to all other
existing paths.
● E.g., a road map: what is the shortest path from Chapel Hill to
Charlottesville?
Applications-
// n is num of vertices.
• If we relax all it will cover all possible paths.
If d[v] > d[u] +w(u, v)
then d[v] ← d[u] +w(u, v)
• The Bellman-Ford algorithm uses the bottom-
up approach.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 152
Bellman Ford (CO3)
Bellman Ford (G, W, S)
• First iteration:
Let’s take (A, B), here d(u) is 0, d(v) is ∞ and w(u,
v) is 6.
putting, these value in “Principle of relaxation”
If, [0 + 6 < ∞]…… True
So, [d(B) = 6]
Now, let’s take (A, C)
On putting “Principle of relaxation”
[ d(C) = 4 ]
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.
Problem-
Solution : Step-01:
Solution : Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
3. Create a new internal node with a 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.
12/15/2024 ANKITA SHARMA ACSE0401 UNIT 3 203
Huffman Coding -Greedy Algorithm (CO3)
)
Huffman (C)
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)
• 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.
Huffman Coding
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