0% found this document useful (0 votes)
11 views54 pages

UNIT 3 - R23

This document discusses various algorithms, focusing on the divide-and-conquer approach for multiplying large integers and matrices, specifically through Strassen's algorithm. It also covers sorting algorithms like Merge Sort and Quick Sort, binary tree properties, and the Closest-Pair problem. Additionally, it explains the Convex Hull problem using Merge Sort, detailing the steps to find the upper and lower tangents of convex hulls.

Uploaded by

windowsbabu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views54 pages

UNIT 3 - R23

This document discusses various algorithms, focusing on the divide-and-conquer approach for multiplying large integers and matrices, specifically through Strassen's algorithm. It also covers sorting algorithms like Merge Sort and Quick Sort, binary tree properties, and the Closest-Pair problem. Additionally, it explains the Convex Hull problem using Merge Sort, detailing the steps to find the upper and lower tangents of convex hulls.

Uploaded by

windowsbabu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Design and Analysis of

Algorithms
UNIT 3

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Divide and Conquer

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Multiplication of Large Integers and Strassen’s Matrix Multiplication

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Suppose we want to multiply two 2-digit

Multiplication of integers, say 23 and 14.

Large Integers 1. Represent the integers: Express each


integer as a combination of its digits and
powers of 10. For example:
1. 23=2×101+3×100
Let's break down the process of 2. 14=1×101+4×100
multiplying two large integers using
2. Apply the multiplication:
the divide-and-conquer technique
23×14=(2×101+3×100)×(1×101+4×
step by step, using a small example. 100)

Expand the expression:


=(2×101×1×101) +
(3×100×1×101)+(2×101×4×100)+(3
×100×4×100)
=2×102+11×101+12×100
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
3. Combine the terms:
1. 23×14=2×102+11×101+12×10 Time Complexity
0
=322 For an n-digit number, we divide it into
two halves of size n/2 and perform 4
So, the result of multiplying 23 and recursive multiplications:
14 is 322. ● T(n)=4T(n/2)+O(n)

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Strassen’s Matrix
Multiplication Let's say we want to
multiply two 2×2 matrices:
Strassen's Matrix Multiplication is a
method to multiply two matrices ● A=,
more efficiently compared to the
brute-force approach. Here's a step- ● B=
by-step explanation of how it works
with an example:

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Formulas for Strassen's Algorithm:
Using Strassen's formulas, we can
compute the elements of the resulting
Then, we can compute the elements
matrix C=A×B as follows:
of C using these values:
m1​=(a00​+a11​)×(b00​+b11​)
● c00​=m1​+m4​−m5​+m7​
m2​=(a10​+a11​)×b00​
m3​=a00​×(b01​−b11​) ● c01​=m3​+m5​
m4​=a11​×(b10​−b00​) ● c10​=m2​+m4​
m5​=(a00​+a01​)×b11​ ● c11​=m1​+m3​−m2​+m6​
m6​=(a10​−a00​)×(b00​+b01​) m7​=(a01​−a11​
)×(b10​+b11​)

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Step 1: Compute m1​ to m7​
:

Example:Let's say we have m1​=(2+4)×(1+4)=6×5=30


two matrices: m2​=(1+4)×1=5×1=5 m3​
A= =2×(2−4)=2×(−2)=−4 m4​
B= =4×(3−1)=4×2=8 m5​
=(2+3)×4=5×4=20 m6​
=(1−2)×(1+2)=(−1)×3=−3
m7​
=(3−4)×(3+4)=(−1)×7=−7

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Step 2: Compute C:
● c00​=m1​+m4​−m5​+m7​
=30+8−20−7=11
● c01​=m3​+m5​=−4+20=16
● c10​=m2​+m4​=5+8=13
● c11​=m1​+m3​−m2​+m6​
=30−4−5−3=18

Result: C=

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Asymptotic Efficiency:
• Let M(n) be the number of multiplications
Analysis: made by Strassen's algorithm for
• Strassen's algorithm performs seven multiplying two n×n matrices.
multiplications (m1​ to m7​) and 18 • The recurrence relation for M(n) is
additions/subtractions to compute M(n)=7M(n/2)+an2 for n>1, with M(1)=1.
the elements of the resulting matrix. • Solving this recurrence yields M(n)= ,
which is approximately n2.807.
• This is in contrast to the brute-force
Conclusion:
approach, which requires eight
• Strassen's algorithm reduces the number
multiplications and four additions.
of multiplications required for matrix
multiplication, leading to better efficiency
• While Strassen's algorithm appears for large matrices.
to have more operations, it offers
better efficiency for large matrices • While other algorithms with even lower
due to its asymptotic superiority. exponents exist, they come with
increased complexity and are not
practically useful due to large
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept multiplicative constants.
In a series of given array A[0….n-1]
divided into two halves A[0….(n/2)-1] and
A[(n/2)-1…n-1], sorting each of the halves
recursively and then merging the two
sorted sub-arrays into a single sorted
sequence of elements.

Merge Sort To sort a sequence of n elements the


following steps are followed

• Divide the given array into two sub-


arrays of length (n/2) and (n/2).
• Recursively sort each of the two sub-
arrays
• Merge the two sorted sub-arrays to
obtain the final sorted array.

Time complexity for 2T(n/2) + cn


computations is O(nlogn)
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Quicksort (A, i, j)
{
if (i<j)
Quick sort {
loc = partition (A, i, j);
Quicksort (A, i, loc-1);
Quicksort (A, loc+1,j);
}
}

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Definition of Binary Tree: A binary
tree is defined as a finite set of nodes
that is either empty or consists of a
root and two disjoint binary trees
Binary Tree Traversals called the left subtree (TL) and right
subtree (TR) of the root.
and Related Properties
Height of a Binary Tree: The height
of a binary tree is defined as the length
of the longest path from the root to a
leaf. It can be computed recursively as
the maximum of the heights of the
root’s left and right subtrees plus 1.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Algorithm Height(T):
• Input: A binary tree T
• Output: The height of T
• If T is empty, return -1
• Else, return max{Height(Tleft),
Height(Tright)} + 1

Analysis of Height Algorithm:


1. The number of comparisons made
to check whether the tree is empty
is C(n)=n+x=2n+1, where n is the
number of internal nodes and x is
the number of external nodes.
2. The number of additions is A(n)=n.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Binary Tree Traversals:
1. There are three classic traversals:
preorder, inorder, and postorder.
2. All three traversals visit nodes of a
a
binary tree recursively, i.e., by
visiting the tree’s root and its left
and right subtrees.
b c 3. They differ only by the timing of the
root’s visit:
e 1. In preorder traversal, the root
d f is visited before the left and
right subtrees are visited (in that
order).
g 2. In inorder traversal, the root is
visited after visiting its left
subtree but before visiting the
right subtree.
3. In postorder traversal, the
root is visited after visiting the
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
left and right subtrees (in that
order).
Input: An array of n points P[]

Output: The smallest distance between two


points in the given array.

The Closest-Pair •

Find the closest pair in the LEFT (dl)
Find the closest pair in the RIGHT (dr)

problem • Check if there is a pair such that one


point is on the LEFT region, other point is
on the RIGHT region, and the distance
between them is < d = min(dl,dr)

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


1) Find the middle point in the sorted array,
we can take P[n/2] as the middle point.

2) Divide the given array into two halves.


The first subarray contains points from P[0]
to P[n/2]. The second subarray contains
points from P[n/2+1] to P[n-1].

3) Recursively find the smallest distances in


both subarrays. Let the distances be dl and
dr. Find the minimum of dl and dr. Let the
minimum be d.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


4) From the above 3 steps, we have an
upper bound d of minimum distance.

Now we need to consider the pairs


such that one point in pair is from the
left half and the other is from the right
half.

Consider the vertical line passing


through P[n/2] and find all points
whose x coordinate is closer than d to
the middle vertical line. Build an array
strip[] of all such points.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


5) Sort the array strip[] according to y Time Complexity Let the Time complexity of
coordinates. This step is O(nLogn). It can be the above algorithm be T(n). Let us assume
optimized to O(n) by recursively sorting and that we use an O(nLogn) sorting algorithm.
merging.
• The above algorithm divides all points in
6) Find the smallest distance in strip[]. This two sets and recursively calls for two sets.
is tricky. From the first look, it seems to be a • After dividing, it finds the strip in O(n)
O(n^2) step, but it is actually O(n). time, sorts the strip in O(nLogn) time and
finally finds the closest points in strip in
It can be proved geometrically that for O(n) time.
every point in the strip, we only need to
check at most 7 points after it (note that So T(n) can expressed as follows
strip is sorted according to Y coordinate).
T(n) = 2T(n/2) + O(n) + O(nLogn) + O(n)
7) Finally return the minimum of d and T(n) = 2T(n/2) + O(nLogn)
distance calculated in the above step (step The overall time complexity is O(n log(n)).
6)

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Convex Hull Using Quick Hull or Merge sort

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


The steps that we’ll follow to solve the problem
are:

1. First, we’ll sort the vector containing points


in ascending order (according to their x-
coordinates).

2. Next, we’ll divide the points into two


halves S1 and S2. The set of points S1

Convex-Hull Problem contains the points to the left of the


median, whereas the set S2 contains all

using merge sort the points that are right to the median.

3. We’ll find the convex hulls for the set S1


and S2 individually. Assuming the convex
hull for S1 is C1, and for S2, it is C2.

4. Now, we’ll merge C1 and C2 such that we


get the overall convex hull C.

Here the only tricky part is merging the two


Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
convex hulls C1 and C2.
● The idea here is that the two
tangents of the left and the right
convex hulls (C1 and C2) will make
the final convex hull C.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Finding the upper tangent and lower
tangent.

● Let L1 be the line that joins the rightmost


point of the left convex hull C1 and the
leftmost point of the right convex hull C2.

● As L1 passes through the polygon C2, we


take the anti-clockwise next point on C2
and label the line L2. The line is above the
polygon C2, but it crosses the polygon C1,
so we move to the clockwise next point,
making the line L3. This again crosses the
left polygon, so we move to line L4. This
line is crossing the right polygon, so we
move to line L5. Now, this final line is
crossing neither of the points.

● Hence we get the upper tangent for


Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept the given two convex hulls.
● For finding the lower tangent, we
need to move inversely through the
hulls, i.e., if the line is crossing the
convex hull C2, we move to clockwise
next and to anti-clockwise next if the
line is crossing the convex hull C1

● Here, PQRSUVW is the convex hull of


the set of given points.

● https://www.gorillasun.de/blog/quickh
ull-algorithm-for-convex-hulls/

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Transform and conquer

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Presorting is a technique used in
computer science to simplify and
optimize various algorithms by sorting
the input data beforehand.

Presorting 1. Introduction to Presorting:


1. Presorting involves sorting the
input data before performing
further operations.

2. Sorting makes it easier to solve


certain problems and can lead to
more efficient algorithms.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example 1: Checking Element
Uniqueness:
• Problem: Determine if there are any
duplicate elements in an array.
• Brute-force approach: Compare every
PresortElementUniqueness(A[0..n − 1])
pair of elements in the array, which
has a worst-case efficiency of O(n2). sort the array A
for i ← 0 to n − 2 do
• Presorting approach:
if A[i] = A[i + 1] return false
• Sort the array first.
• Check consecutive elements for return true
equality.
• The efficiency of this approach is
O(nlogn) due to sorting.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example 2: Searching Problem: Conclusion:
• Problem: Search for a value in an 1. Presorting is a powerful technique that
array of sortable elements. simplifies and optimizes algorithms by
• Brute-force approach: Use sequential sorting input data.
search, which has a worst-case
efficiency of O(n). 2. It's commonly used in various
• Presorting approach: algorithms, including those dealing
with geometric problems and directed
• Sort the array first.
acyclic graphs.
• Use binary search, which has a
worst-case efficiency of O(logn).
3. Presorting can significantly improve the
• The efficiency of this approach is efficiency of algorithms, especially
O(nlogn) due to sorting and when combined with other techniques
searching. like binary search.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Gaussian elimination is a method used to
solve systems of linear equations by
transforming the system into an equivalent
upper-triangular form.

Here's a step-by-step explanation of how


Gaussian elimination works, along with an
example:
Gaussian Elimination Introduction to Gaussian Elimination:
1. Gaussian elimination transforms a
system of linear equations into an
equivalent system with an upper-
triangular coefficient matrix.
2. The upper-triangular form simplifies
the process of finding the solution by
allowing for easy back substitution.
3. The method involves a series of
elementary row operations.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Here's the pseudocode for forward
Forward Elimination:
elimination:
• The first step is to perform forward
elimination to introduce zeros below
the diagonal. for i from 1 to n do
augment A[i, n + 1] with b[i] //
• We start with the first column as the append b to A as the last column
pivot column and eliminate the for j from i + 1 to n do
coefficients below it. if |A[j, i]| > |A[pivotrow, i]| then
pivotrow = j
• We eliminate the coefficients below the swap rows i and pivotrow
diagonal by subtracting multiples of for j from i + 1 to n do
the pivot row from the rows below. temp = A[j, i] / A[i, i]
for k from i to n + 1 do
• If the pivot element is zero, we perform
A[j, k] = A[j, k] - A[i, k] * temp
partial pivoting by swapping rows to
ensure a non-zero pivot.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Back Substitution: Forward Elimination:
• Once the system is in upper-triangular Step 1: Choose the Pivot Column
form, we can solve it by back We start with the first column as the pivot
column.
substitution.

Step 2: Eliminate Coefficients Below the Pivot


• We start with the last equation to find
the value of the last variable, then
Eliminate x1​ coefficients below the first
substitute it back into the previous
equation:
equations to find the values of the
R2- R1(4/2)
other variables. 2x1 – x2 + x3 = 1 R3- R1(1/2)
4x1 + x2 – x3 = 5
Example System of Equations: x1 + x2 +x3 = 0
Consider the system of equations:
● 2x1 – x2 + x3 = 1 ● 2x1 – x2 + x3 = 1
● 4x1 + x2 – x3 = 5 ● 0x1 + 3x2 – 3x3 = 3
● x1 + x2 +x3 = 0 ● 0x1 + 3/2x2 + 1/2x3 = -1/2

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Step 3: Move to the Next Pivot Column
Back Substitution:
Step 1: Solve for x3​
From the last equation, we can directly
● Now, we move to the second column solve for x3​:
as the pivot column. 2x3 = -2 => x3 = -1
Eliminate the x2​coefficient below the
second equation: Step 2: Substitute x3​to Solve for x2​
Substituting x3​=−1 into the second
2x1 – x2 + x3 = 1 equation:
0x1 + 3x2 – 3x3 = 3 3x2​−3(−1)=3⟹3x2​+3=3⟹x2​=0
0x1 + 3/2x2 + 1/2x3 =R3
-1/2
– R2(1/2)
Step 3: Substitute x3​and x2​to Solve for x1​
Substituting x3​=−1 and x2​=0 into the first
● 2x1 – x2 + x3 = 1 equation:
● 0x1 + 3x2 – 3x3 = 3 2x1​−0+(−1)=1⟹2x1​−1=1⟹2x1​=2⟹x1​
● 0x1 + 0x2 + 2x3 = -2 =1

So, the solution to the system of equations


is
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
x1 =1 x2 = 0 and x3 = -1
Time Complexity Analysis:
• The number of operations performed
Implications of Cubic Time Complexity:
during forward elimination grows with
the size of the input (number of
equations and variables). Performance Considerations:
1. Cubic time complexity implies that the
• As each row requires operations algorithm's runtime increases
proportional to the number of remaining significantly as the size of the input
rows, the total number of operations can grows.
be represented by a cubic function of the 2. For large systems of equations,
input size. Gaussian elimination may become
computationally expensive and
• Therefore, the time complexity of impractical due to its cubic time
Gaussian elimination is typically complexity.
expressed as O(n ), where 'n' represents
3

the number of equations or variables in


the system.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Introduction to Balanced Search Trees:
• Balanced search trees are data
structures designed to maintain the
logarithmic efficiency of dictionary
operations (searching, insertion,
deletion) while preventing worst-case
degeneracy.
Balanced Search Trees • They address the limitation of classical
binary search trees, where unbalanced
trees could lead to linear time
complexity in worst-case scenarios.

Instance-Simplification Approach:
• This approach involves transforming an
unbalanced binary search tree into a
balanced one, hence termed self-
balancing trees.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Introduction to 2-3 Trees:
• A 2-3 tree is a type of balanced search Node Structure and Property:
tree that allows nodes to contain either • In a 2-node, the left child contains
one or two keys. keys smaller than the node's key, and
the right child contains keys larger
• Introduced by John Hopcroft in 1970, it than the node's key.
addresses the issue of unbalanced
trees by maintaining perfect height • In a 3-node, the left child contains
balance. keys smaller than the first key, the
middle child contains keys between
• Nodes in a 2-3 tree can be of two
the first and second keys, and the
types: 2-nodes containing one key and
right child contains keys larger than
two children, and 3-nodes containing
the second key.
two keys and three children.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Height Balancing:
• A crucial property of 2-3 trees is that all
leaves must be at the same level, ensuring
perfect height balance.
Insertion Operation:
• This property is maintained by allowing
nodes to contain multiple keys, thus
• Inserting a new key in a 2-3 tree
avoiding degenerate trees. involves finding the appropriate leaf
node through a search operation.
Searching in 2-3 Trees:
• Searching for a key in a 2-3 tree starts from • If the leaf is a 2-node, the new key is
the root and progresses down the tree.
inserted to maintain order.
• If the root is a 2-node, the search continues
recursively based on key comparisons. • If the leaf is a 3-node, it is split into
two nodes, and the middle key is
• If the root is a 3-node, the search narrows promoted to the parent node.
down to one of the three subtrees based on
key comparisons.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


3-node
2-node
K1, K2
K

<K >K < K1 (K1,K2) > K2

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Height Bounds and Efficiency:
1. The height of a 2-3 tree is bounded by
the logarithmic function of the number of
nodes.
Example of 2-3 Tree Construction:
• Figure illustrates the construction 2. Both upper and lower bounds on the
of a 2-3 tree for a given list of keys: height ensure that searching, insertion,
9, 5, 8, 3, 2, 4, 7. and deletion operations have logarithmic
• Each step in the construction Ө(nlogn) time complexity in both worst
involves inserting a key into the and average cases.

tree while maintaining balance and


3. This is because, for each of the n
order. elements, you may need to traverse
O(logn) levels to reach it.

Generalization to B-Trees:
4. B-trees are a significant generalization of
2-3 trees, allowing for more keys per
node and greater flexibility in tree
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept structure.
An AVL tree is a type of binary search

AVL Trees
tree (BST) that maintains its balance to
ensure efficient search, insertion, and
deletion operations. It was invented in
1962 by two Russian scientists, G. M.
Adelson-Velsky and E. M. Landis —
that's where the name AVL comes from.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


What Makes a Tree an AVL Tree?

1. An AVL tree is a binary search tree When a tree becomes unbalanced, it can be
(BST) — meaning: fixed using four types of rotations:
1. All values in the left subtree of a 1. Single Right Rotation (R-rotation):
node are smaller than the node's 1. Used when the node’s left subtree is
value. heavier than the right subtree.
2. All values in the right subtree of a 2. Single Left Rotation (L-rotation):
node are greater than the node's 1. Used when the node’s right subtree is
value. heavier than the left subtree.
3. Left-Right Rotation (LR-rotation):
2. The difference in height between the left 1. Used when the right subtree of the
and right subtrees of any node (called left child causes imbalance.
the balance factor) is always between - 2. First, left-rotate the left child → Then,
1, 0, or +1. right-rotate the root.
1. Balance factor = Height of left 4. Right-Left Rotation (RL-rotation):
subtree – Height of right subtree 1. Used when the left subtree of the
2. If the balance factor becomes +2 or right child causes imbalance.
-2 after an insertion or deletion, the 2. First, right-rotate the right child →
tree becomes unbalanced and needs Then, left-rotate the root.
to be fixed by rotation.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Example: Balance Factor
Balance factor = Height of left subtree –
Height of right subtree 25

10

22 30

5 15

20

2 9 20 28 40
12

3 17 30 5
5

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example: Left Rotation
Insert the following elements on an AVL Tree:
50, 30, 75, 80, 92
50
50

30 75 Left Rotation 30 80

80
75 92

92

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example: Right Rotation
Insert the following elements on an AVL Tree:
65, 90, 20, 80, 72

65
65

90 20 80
20

Right Rotation
80
72 90

72

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example: Left-Right (LR) Rotation First, left-rotate the left child → Then, right-rotate the root

3 3
3

1 1
2

1 3

2 2
After LR Rotation
Tree is balanced
Tree is imbalanced 1
LL Rotation
because node 3 has balance factor 2

RR Rotation

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Example: Right-Left (RL) Rotation First, right-rotate the right child → Then, left-rotate the root.

1 1 1

2
3 3 2

1 3
2 2 3

After RL Rotation
LL Rotation Tree is balanced
RR Rotation
Tree is imbalanced
because node 1 has balance factor -2

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


1. Algorithm Overview:
1. Heapsort is a sorting algorithm that
consists of two stages:
1. Stage 1: Construct a heap from
the given array.
2. Stage 2: Perform maximum
deletions (extracting the
maximum element from the

Heap Sort
heap) n−1 times, resulting in a
sorted array.

2. Heap Construction:
1. In Stage 1, a heap is constructed
from the given array using the
bottom-up heap construction
algorithm.
2. This stage has a time complexity of
O(n), where n is the number of
elements in the array.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


3. Maximum Deletions: • Heapify property is applied from the last
element to the first element i.e. from
• In Stage 2, the root element
Right to Left.
(maximum element) is deleted
• Adjustment of nodes can be done from
from the heap n−1 times.
downwards.
• Each deletion operation rearranges • In a complete Binary tree, leaf nodes are
the heap to maintain the heap from ⌊n/2⌋ +1 to n
property. • Heapify property starts at the internal
• The deleted elements are placed at node that has the highest array index i.e
the end of the array. ⌊n/2⌋

Example: 2,9,7,6,5,8 Heap sort contains two phases


1. Construct max heap
2. Sorting Operation

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Efficiency Analysis:
• The time complexity of Stage 2
depends on the number of key
comparisons needed to delete the root
element each time.
• The number of key comparisons, C(n),
can be bounded by 2log2​(n−1)+2log2​
(n−2)+...+2log2​(1).
• This sum can be simplified and
bounded by 2nlog2​n.
• Thus, the time complexity of Stage 2
is O(nlogn).
• Combining both stages, the overall
time complexity of heapsort is O(n)
+O(nlogn)=O(nlogn).

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Horner’s Rule ALGORITHM Horner(P[0..n],x)
// Evaluates a polynomial at a given point
by Horner’s rule
// Input: An array P[0..n] of coefficients of
• Horner's rule is a technique used for a polynomial of degree n,
efficiently evaluating polynomials. //store from the lowest to the highest and
a number x
• It's particularly useful because it
// Output: The value of the polynomial at
reduces the number of arithmetic
x
operations required to compute the
value of a polynomial at a given
point.
p ← P[n]
• This process allows us to avoid the for i ← n−1 down to 0 do
need for explicit expansion of the p ← x * p + P[i]
polynomial, making it a powerful tool
return p
in polynomial evaluation.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Example:
Explanation:
• Initialization: We initialize the coefficients Problem: p(x)=2x4−x3+3x2+x−5 at x=3.
of the polynomial and the value of x.
Step 1: Initialize Coefficients and x
• Horner's Rule: We apply Horner's rule by Coefficients: 2 -1 3 1 -5
successively multiplying the current result by
x=3
x and adding the next coefficient until we
reach the end.
Step 2: Apply Horner's Rule
• Interpretation: Each step in the calculation
corresponds to evaluating a term of the • We multiply the previous result by the
polynomial, resulting in the final value of p(x) value of x, which is 3.
at the given point x=n.
• Initial result: 2

• Multiply by x=3: 2×3=6
Efficiency: Horner's rule allows us to
evaluate the polynomial with only n • We then add the next coefficient,
multiplications and n additions, making it which is −1.
highly efficient compared to brute-force • Add coefficient: 6+(−1)= 5
methods.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


• Next, we multiply the previous result (5)
by x=3 again.
• Multiply by x=3: 5×3=15
• We add the next coefficient, which is 3.
• Add coefficient: 15+3=18
Final Result:
• After performing the above iterations
• Next, we multiply the previous result (18)
by x=3 again. for all coefficients, the final result is
• Multiply by x=3: 18×3= 54 160.
• We add the next coefficient, which is 1. • This final result represents the value
• Add coefficient: 54+1=55 of the polynomial p(x) at x=3, which
is p(3)=160.
• Next, we multiply the previous result (55)
by x=3 again.
• Multiply by x=3: 55×3=165
• We add the next coefficient, which is -5.
• Add coefficient: 165+(-5)=160

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


Binary exponentiation is a highly efficient
algorithm used for computing large powers of a
number.

It is particularly useful in scenarios where


traditional exponentiation methods, such as
repeated multiplication, become
computationally expensive, especially for large
exponents.
Binary Exponentiation
Efficiency:
• Binary exponentiation has a time
complexity of O(logn), where n is the
exponent.
• This makes it significantly faster than the
naive approach of repeated multiplication,
which has a time complexity of O(n).
• As a result, binary exponentiation is much
faster, especially for large exponents.

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept Two Approaches: Left to Right, Right to Left
Right-to-Left Binary Exponentiation
Left-to-Right binary exponentiation
Algorithm:
Algorithm:

Step 1: Initialize values:


Step 1: Convert Exponent to Binary
• term is initialized to a
​Step 2: Initialize
• If the rightmost bit (LSB) is 1, set
• Start with the accumulator (product) set
product = a; otherwise, set product = 1
to a.
• Read the binary digits from left to right
Step 2: Process each bit of the binary
(from the most significant bit).
• representation from right to left:
For each bit:
• Square the accumulator. 1. If the current bit = 1, multiply product by
• If the bit is 1, multiply the term
2. Square term after each step
accumulator by a.
• If the bit is 0 square the accumulator.
Step 3: Repeat until all bits are
processed

Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept


1. Identify the Problem: Start by understanding
the problem you need to solve. This could be a

Problem Reduction
complex task or a challenge that you're facing.

2. Find a Known Problem: Look for a problem that


you already know how to solve or one that is
simpler to solve compared to the original problem.
This known problem should be related to the
Problem reduction is a problem- original problem in some way.
solving strategy that involves
3. Reduce the Original Problem: Develop a
simplifying a complex problem by
strategy to reduce the original problem to the
reducing it to a known or easier known or simpler problem. This may involve
problem. Here's a step-by-step transforming the original problem or breaking it
explanation with an example: down into smaller, more manageable parts.

4. Apply the Solution: Solve the known or simpler


problem using an appropriate algorithm or method
that you have available.

5. Translate the Solution: Once you have solved


the simpler problem, translate the solution back to
the original problem domain, if necessary.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept

You might also like