JNTUA Advanced Data Structures & Algorithms PPT Notes - R20 (1)
JNTUA Advanced Data Structures & Algorithms PPT Notes - R20 (1)
me/jntua
UNIT I
ITERATIVE AND RECURSIVE ALGORITHMS
• You cannot expect to know exactly where the algorithm will go,
so you need to expect some weaving and winding.
• On the other hand, you do not want to have to know how to
handle every ditch and dead end in the world.
• A compromise between these two is to have a loop invariant,
which defines a road (or region) that you may not leave.
• As you travel, worry about one step at a time.
• You must know how to get onto the road from any start
location.
• From every place along the road, you must know what actions
you will take in order to step forward while not leaving the
road.
• Finally, when sufficient progress has been made along the
road, you must know how to exit and reach your destination in
a reasonable amount of time.
Iterative Algorithms:
• A good way to structure many computer programs is to store
the key information you currently know in some data structure
• And then have each iteration of the main loop take a step
towards your destination by making a simple change to this
data.
Loop Invariant:
• A loop invariant expresses important relationships among the
variables that must be true at the start of every iteration and
when the loop terminates.
• If it is true, then the computation is still on the road.
• If it is false, then the algorithm has failed.
10
Proof of Correctness:
• Naturally, you want to be sure your algorithm will work on all
specified inputs and give the correct answer.
Running Time:
• You also want to be sure that your
algorithm completes in a reasonable
amount of time.
The Most Important Steps:
• If you need to design an algorithm, do not start by typing in code
without really knowing how or why the algorithm works.
• Instead, first accomplishing the following tasks.
Math Details:
• Small math operations like computing the index of the middle
element of the subinterval A(i.. j ) are prone to bugs.
• Check for yourself that the answer is mid = [i+j/2]
20
Recursion
• Recursion is more than just a programming technique. It has two
other uses in computer science and software engineering, namely:
• a way of describing, defining, or specifying things.
• a way of designing solutions to problems (divide and conquer).
25
Towers of Hanoi
• The towers of Hanoi is a classic puzzle for which the only
possible way of solving it is to think recursively.
• Specification: The puzzle consists of three poles and a stack of
N disks of different sizes.
• Precondition: All the disks are on the first of the three poles.
• Postcondition: The goal is to move the stack over to the last
pole. See the first and the last parts of figure.
• You are only allowed to take one disk from the top of the stack
on one pole and place it on the top of the stack on another
pole.
• Another rule is that no disk can be placed on top of a smaller
disk.
1) Specifications
2) Variables
2.1) Your Input
2.2) Your Output
2.2.1) Every Path
2.2.2) Type of Output
2.3) Your Friend’s Input
2.4) Your Friend’s Output
2.5) Rarely Need New Inputs or
Outputs
2.6) No Global Variables or Global
Effects
2.7) Few Local Variables
IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2
31
3)Tasks to Complete
3.1) Accept Your Mission
3.2) Construct Subinstances
3.3) Trust Your Friend
3.4) Construct Your Solution
3.5) Base Cases
The Stack Frame
• Tree of Stack Frames:
• Tracing out the entire computation of a recursive algorithm,
one line of code at a time, can get incredibly complex.
32
Goal:
Our goal is to prove that it is true for every value of n, namely that ∀n
≥0, S(n).
Proof Outline:
Proof by strong induction on n.
Induction Hypothesis: For each n ≥ 0, let S(n) be the statement that . .
. . (It is important to state this clearly.)
Base Case:
Prove that the statement S(0) is true.
Induction Step: For each n ≥ 0, prove S(0), S(1), S(2), . . . , S(n − 1) ⇒
S(n).
Conclusion:
By way of induction, we can conclude that ∀n ≥ 0, S(n).
Ackermann’s Function
Running Time:
• The only way that the program builds up a big number is by
continually incrementing it by one.
• Hence, the number of times one is added is at least ashuge as
the value Tk (n) returned.
Recursion on Trees:
• One key application of recursive algorithms is to perform
actions on trees, because trees themselves have a recursive
definition
43
Examples
46
Original Instance:
• Another elegant algorithm for the IsBST problem generalizes
the problem in order to provide your friend more information
about your subinstance.
• Here the more general problem, in addition to the tree, will
provide a range of values [min,max] and ask whether the tree
is a BST with values within this range.
• The original problem is solved using
• IsBSTtree(tree, [−∞,∞]).
Definition of a Heap:
• A heap imposes a partial order on the set of values, requiring
that the value of each node be greater than or equal to that of
each of the node’s children.
• There are no rules about whether the left or the right child is
larger.
Maximum at Root:
• An implication of the heap rules is that the root contains the
maximum value.
• The maximum may appear repeatedly in other places as well.
Iterative Algorithm:
• A good loop invariant would be “The entire tree is a heap except
that nodei might not be greater or equal to both of its children.
• As well, the value of i’s parent is at least the value of i and of i’s
children.”
• When i is the root, this is the precondition.
• The algorithm proceeds as in the recursive algorithm.
• Node i follows one path down the tree to a leaf.
• When i is a leaf, the whole tree is a heap.
Priority Queues
• Like stacks and queues, priority queues are an important ADT.
• Definition: A priority queue consists of:
– Data: A set of elements, each of which is associated with
an integer that is referred to as the priority of the element.
Operations:
Insert an Element:
An element, along with its priority, is added to the queue.
Change Priority:
The priority of an element already in the queue is changed. The
routine is passed a pointer to the element within the priority
queue and its new priority. Remove an Element: Removes and
returns an element of the highest priority fromthe queue.
Binary Search Tree gives advantage of Fast Search, but sometimes in few cases we are not able
to get this advantage. E.g. look into worst case BST
Balanced binary trees are classified into two categories
Height Balanced Tree (AVL Tree)
Weight Balanced Tree Worst search time cases for Binary Search Tree
50 20
40 30
30
40
20
50
Balance factor of a node is the difference between the heights of the left and right subtrees of
that node.
The balance factor of a node is calculated either height of left subtree - height of right
subtree (OR) height of right subtree - height of left subtree.
In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation, let us consider the following
insertion operation in AVL Tree...
In RR Rotation, every node moves one position to right from the current position. To understand RR Rotation, let us consider the
following insertion operation in AVL Tree...
In LR Rotation, at first, every node moves one position to the left and one position to right
from the current position.
To understand LR Rotation, let us consider the following insertion operation in AVL Tree...
B 5 B 2
Insert 4 Insert 1
5 Critical Node B 3
Critical Node 6 B 5 L 3 B 6 L 2 B 5
L 5 2
Case 1 B 4 B 6 L 2 B 4 Case 1 B 1 B 4 B 6
Right Rotation Right Rotation
B 4 of Node 6 B 1 4 of Node 5
B 1 B’ 13 26
Insert 44
Case 2: Left Right Rotation
Left Rotation of Left Child 1 64
Critical Node 64 B 44
Followed By
R 1 Right Rotation of Parent 64 44
Right Rotation B 1 B 64
B 44 1 of Parent 64
Left Rotation of Left Child 1
B’ 98 110
Insert 85
R 44
B 13 L 98
B 1 B 26 R 64 B 110
B 85
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
Construct AVL Search Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
B 60 R 73 76 Critical B 76
R 73 Case 4
Left Rotation 73 79 Case 4 B 73 B 81
Insert 73 B 60 75 R Critical of Node 75 B 60 B 76 Left Rotation
60 75 R 81 of Node 79 60 75 79 82
R 60 B 76 R’ B 75 B 79
B B B B
B 73 79 B’ B 82
Insert 75 Insert 81
B 76
B 73 Critical Node
60 Critical Node
73 B 73 R 79
Case 4 B 60 76 R Case 4
R 73 Left Rotation Left Rotation
60 75 B 60 B 75 B 81
of Node 60 of Node 73
B B B 75 R 79
B 75
75
B 81
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
Construct AVL Search Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
B 5
Insert 73
If element to be deleted does not have empty right sub-tree, then element is replaced with its In-
Order successor and its In-Order successor is deleted instead
During winding up phase, we need to revisit every node on the path from the point of deletion up
to the root, rebalance the tree if require
28 B
R 23 B 32
B 22 R 26 R 30 R 34
B 27 B 31 B 36
Delete 30
28
30
31
23 32
23 32 Critical Node
34
22 26 30
22 26 31 34 R
27 31 36
27 36 B
In-Order Traversal
22, 23, 26, 27, 28, 30, 31, 32, 34, 36 Case 4:
Left Rotation of
Delete 28 Node 32
28 B
30 L 31
R 23 32 R 23 R 34 B
B 22 R 26 30
31 R R 34 32 B 36 B
B 22 R 26
27 31 36 27 B
B B B
Delete 74
74 Critical
75 B
13
Case 1: Right Rotation
L 13 75 B
89 of Node 75 L 10 75 B
L 10 28 B 89
28 89 B
B 5 B 28
B 5
In a weight balanced tree, the nodes are arranged on the basis of the knowledge available on the
probability for searching each node
The node with highest probability is placed at the root of the tree
The nodes in the left sub-tree are less in ranking as well as less in probability then the root node
The nodes in the right sub-tree are higher in ranking but less in probability then the root node
Each node of such a Tree has an information field contains the value of the node and count
number of times node has been visited
Probability
P 23
20 21
E T
K 15 S V
14 13
11 G 10 M
M 2
P
G 4 T 3 D G T
M L Z
D 4 L 5 P 6 Z 1
Ordered Tree
P 6
L 5 T 3
G 4 M 2 Z 1
D 4
The nodes in a binary tree like AVL tree contains only one record
AVL tree is commonly stored in primary memory
In database applications where huge volume of data is handled, the search tree can not be
accommodated in primary memory
B-Trees are primarily meant for secondary storage
B-Tree is a M-way tree which can have maximum of M Children
10, 15, 20
2, 3 11, 14 16 21
4 – way Tree
No of Keys = 3
K1, K2, K3
No of Ways or children = 4
P0 P1 P2 Pn-1 Pn
P0 P1 P2 Pn-1 Pn
K0, K1, K2, ……… ,Kn-1 are keys stored in the node
Sub-Trees are pointed by P0, P1, P2, ……… ,Pn then
K0 >= all keys of sub-tree P0
K1 >= all keys of sub-tree P1
………..
………..
20
P
5, 10, 15, 20 1, 5, 9, 11
Insert - 38
Insert 7
Overflow
20
6, 10 30, 40
6 6, 11 6, 11
Overflow Overflow
1,2,5 7,10,11,12,13 1,2,5 7,10 12,13 1,2,5 7,10 12,13,16,20,24
Insert 3,4
3, 6, 11, 16
6, 11, 16 6, 11, 16
Overflow
1,2 4,5 7,10 12,13 20,24
1,2,5 7,10 12,13 20,24 1,2, 3,4,5 7,10 12,13 20,24
Insert 25
3, 6, 11, 16 11
Overflow
1,2 4,5 7,10 12, 13, 14 18, 19, 20, 24, 25 3, 6 16, 20
3, 6, 11, 16, 20 Overflow 1,2 4,5 7,10 12, 13, 14 18, 19 24, 25
4. If neither of the sibling contain more than m/2 elements then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent too.
If the the node which is to be deleted is an internal node, then replace the node with its in-
order successor or predecessor. Since, successor or predecessor will always be on the leaf
node hence, the process will be similar as the node is being deleted from the leaf node
If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too
few keys, then simply remove the key to be deleted. key k is in node x and x is a leaf, simply
delete k from x.
Case-II-a
If the child y that precedes k in node x has at least t keys (more than the
minimum), then find the predecessor key k' in the subtree rooted at y.
Recursively delete k' and replace k with k' in x
Case-II-b
Symmetrically, if the child z that follows k in node x has at least t keys,
find the successor k' and delete and replace as before. Note that
finding k' and deleting it can be performed in a single downward pass.
Complexity
A B C D E F G
C ✓ E ✓ G ✓
F ✓
C B A E F D G
C ✓ E ✓ G ✓
F ✓
If we interchange left and right words in the preceding definitions, we obtain three new traversal
orders which are called
Converse Preorder Traversal: A D G E F B C
Converse Inorder Traversal: G D F E A B C
Converse Postorder Traversal: G F E D C B A
1 50 15
2 3 25 75
3 1
6 22
4 22 40 60 80
5 45
5
15 30 90
23 65
34 78
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
Node structure of binary tree is described as below Typical node of Binary Tree
Node structure of binary tree is described as below. Typical node of Binary Tree
Node structure of binary tree is described as below. Typical node of Binary Tree
Construct a Binary tree from the given Inorder and Postorder traversals
• Step 1: Find the root node
• Preoder Traversal – first node is root node
Inorder : D G B A H E I C F • Postoder Traversal last node is root node
Postorder : G D B H I E F C A • Step 2: Find Left & Right Sub Tree
• Inorder traversal gives Left and right sub tree
Postorder : G D B H I E F C AA
A A
Inorder : D G B AAH E I C F
B C B C
A
D,G H,E,I F D E F
D,G,B H,E,I,C,F
G H I
Preorder : GG B Q A C K F P D E R H Inorder : Q B K C F A GG P E D H R
G G G
QBKCFA PED HR B P B P
D Q A D
Q A
G
C E R
B KCF E HR
P
K F H
Q KCFA ED HR
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
The wasted NULL links in the binary tree storage representation can be replaced by threads
A binary tree is threaded according to particular traversal order. e.g.: Threads for the inorder
traversals of tree are pointers to its higher nodes, for this traversal order
In-Threaded Binary Tree
If left link of node P is null, then this link is replaced by the address of its predecessor
If right link of node P is null, then this link is replaced by the address of its successor
Because the left or right link of a node can denote either structural link or a thread, we must
somehow be able to distinguish them
Head node is simply another node which serves as the predecessor and
successor of first and last tree nodes.
Tree is attached to the left branch of the head node.
HEAD
A
B D
A
C E G
B D
C E G
Inorder Traversal
C B A E F D G
F
B E
A
C D F H
B E
G
Inorder Traversal
C D F H
CBDAFGEH
G
A binary search tree is a binary tree in which each node possessed a key that satisfy the
following conditions
1. All key (if any) in the left sub tree of the root precedes the key in the root
2. The key in the root precedes all key (if any) in the right sub tree
3. The left and right sub trees of the root are again search trees
50
25 75
22 40 60 80
15 30 90
Delete node a
a Delete node a b
Delete node a
a C
b C
THANK YOU
Unit-3:
Divide and Conquer
Algorithms
Outline
Looping
Introduction to Recurrence Equation
Different methods to solve recurrence
Divide and Conquer Technique
Multiplying large Integers Problem
Problem Solving using divide and conquer
algorithm –
Binary Search
Sorting (Merge Sort, Quick Sort)
Matrix Multiplication
Exponential
Introduction
Many algorithms (divide and conquer) are recursive in nature.
When we analyze them, we get a recurrence relation for time complexity.
We get running time as a function of 𝒏 (input size) and we get the running time on inputs of
smaller sizes.
A recurrence is a recursive description of a function, or a description of a function in terms of
itself.
A recurrence relation recursively defines a sequence where the next term is a function of the
previous terms.
𝑻(𝒏) = 𝑻(𝒏 − 𝟏) + 𝒏 1
Replacing 𝑛Time
by 𝑛to − 1 and
solve the 𝑛 − 2, we can write following equations.
instance of size 𝑛
𝑻 𝒏−𝟏 =𝑻 𝒏−𝟐 +𝒏−𝟏 2
𝑻(𝒏) = 𝑻(𝒏 − 𝟑) + 𝒏 − 𝟐 + 𝒏 − 𝟏 + 𝒏 4
𝑻 𝒏 = 𝑻 𝒏 − 𝒏 + (𝒏 − 𝒏 + 𝟏) + (𝒏 − 𝒏 + 𝟐) + … + 𝒏
𝑻 𝒏 = 𝟎 + 𝟏 + 𝟐 + …+ 𝒏
𝒏 𝒏+𝟏
𝑻 𝒏 = = 𝑶 𝒏𝟐
𝟐
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
1 if n = 0 or 1
1. T n = ቊ
T n − 1 + n − 1 o/w
Homogeneous Recurrence
Recurrence equation
𝑎0 𝑡𝑛 + 𝑎1 𝑡𝑛−1 + 𝑎2 𝑡𝑛−2 + ⋯ + 𝑎𝑘 𝑡𝑛−𝑘 = 0
The equation of degree 𝑘 in 𝑥 is called the characteristic equation of the recurrence,
𝑝 𝑥 = 𝑎0 𝑥 𝑘 + 𝑎1 𝑥 𝑘−1 + ⋯ + 𝑎𝑘 𝑥 0
Which can be factorized as,
𝑘
𝑝 𝑥 = ෑ 𝑥 − 𝑟𝑖
𝑖=1
The solution of recurrence is given as,
𝒌
𝒕𝒏 = 𝒄𝒊 𝒓𝒏𝒊
𝒊=𝟏
Function fibiter(n)
i ← 1; j ← 0;
for k ← 1 to n do
j ← i + j;
i ← j – i;
return j
Analysis of Iterative Algorithm: If we count all arithmetic operations at unit cost; the
instructions inside for loop take constant time 𝑐. The time taken by the for loop is bounded
above by 𝑛, 𝑖. 𝑒., 𝒏𝒄 = 𝛉(𝒏)
Case 1
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
𝑻 𝒏 = 𝜽 𝒏𝟐
Case 2
Function fibrec(n)
if n < 2 then return n
else return fibrec (n – 1) + fibrec (n – 2)
𝒌
The general solution is therefore of the form, 𝒏
𝒏 𝒏
𝑻𝒏 = 𝒄𝟏 𝒓𝟏 + 𝒄𝟐 𝒓𝟐 𝑻𝒏 = 𝒄𝒊 𝒓 𝒊
𝒊=𝟏
Substituting initial values 𝑛 = 0 and 𝑛 = 1
𝑇0 = 𝑐1 + 𝑐2 = 0 1
𝑇1 = 𝑐1 𝑟1 + 𝑐2 𝑟2 = 1 (2)
Solving these equations, we obtain
1 1
𝑐1 = 5
and 𝑐2 = − 5
𝑻𝒏 = 𝒄𝟏 𝒓𝒏𝟏 + 𝒄𝟐 𝒓𝒏𝟐
𝑛 𝑛
1 1+ 5 1− 5
𝑇𝑛 = − … … … de Moivre′ s formula
5 2 2
𝒏
𝑻𝒏 ∈ 𝑶 ∅
𝒕 𝒎 = 𝟐 𝒎 − 𝟏 = 𝑶 𝟐𝒎
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
𝑛 𝑖𝑓 𝑛 = 0 𝑜𝑟 1
1. 𝑡𝑛 = ቊ5𝑡
𝑛−1 − 6𝑡𝑛−2 𝑂/𝑊
𝑛 𝑖𝑓 𝑛 = 0, 1 𝑜𝑟 2
2. 𝑡𝑛 = ቊ
5𝑡𝑛−1 − 8𝑡𝑛−2 + 4𝑡𝑛−3 𝑜/𝑤
Master Theorem
The master theorem is a cookbook method for solving recurrences.
Time to divide &
Suppose you have a recurrence of the form recombine
𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)
𝒏Τ𝒃 𝒏Τ𝒃
1 1 1 1 𝑻 𝒏 = 𝒏 𝒍𝒐𝒈 𝒏 + 𝒏
𝑻 𝒏 = 𝑶(𝒏 log 𝒏)
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Example 2: 𝑻(𝒏) = 𝑻(𝒏/𝟑) + 𝑻(𝟐𝒏/𝟑) + 𝒏
Recurrence Tree Method
The recursion tree for this recurrence is When we add the values across the levels of
the recursion tree, we get a value of 𝑛 for
every level.
log3/2 𝑛−1
𝑛
𝑇(𝑛) = 𝑛 + 𝑛log3/2 2 𝑇(1)
𝑖=0
𝑛 Τ3 2𝑛Τ3 𝒏
𝒍𝒐𝒈𝟑 𝒏 𝒍𝒐𝒈𝟑/𝟐 𝒏 𝑻(𝒏) ∈ 𝒏 log 𝟑/𝟐 𝒏
1𝑛 2𝑛 1 2𝑛 2 2𝑛 𝒏
33 33 3 3 3 3
∞
𝑛 Τ2 2
𝑛Τ2 2 1Τ2 𝑛2 𝟏
𝒊
𝟐
𝑻 𝒏 ≤𝒏
𝟐
𝒊=𝟎
𝑛Τ4 2
𝑛Τ4 2
𝑛Τ4 2
𝑛Τ4 2 1Τ4 𝑛2 𝑻 𝒏 ≤ 𝟐𝒏𝟐
𝑻 𝒏 = 𝑶 𝒏𝟐
𝑂 𝑛2
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Introduction
Many useful algorithms are recursive in structure: to solve a given problem, they call
themselves recursively one or more times.
These algorithms typically follow a divide-and-conquer approach:
The divide-and-conquer approach involves three steps at each level of the recursion:
1. Divide: Break the problem into several sub problems that are similar to the original problem but smaller in
size.
2. Conquer: Solve the sub problems recursively. If the sub problem sizes are small enough, just solve the sub
problems in a straightforward manner.
3. Combine: Combine these solutions to create a solution to the original problem.
Introduction
Binary Search is an extremely well-known instance of divide-and-conquer approach.
Let 𝑇[1 . . . 𝑛] be an array of increasing sorted order; that is 𝑇 [𝑖] ≤ 𝑇[𝑗] whenever 1 ≤ 𝑖 ≤
𝑗 ≤ 𝑛.
Let 𝑥 be some number. The problem consists of finding 𝒙in the array 𝑇 if it is there.
If 𝑥 is not in the array, then we want to find the position where it might be inserted.
1 3 7 9 11 32 52 74 90
Step 1:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
IsIs𝟕<𝟕=midpoint
midpointvalue?
value?YES.
No.
Step 3:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
1 3 7 9 11 32 52 74 90
Step 7:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
2. Explain binary search algorithm and find the element 𝒙 = 𝟑𝟏 in the following array. [7]
10, 15, 18, 26, 27, 31, 38, 45, 59
3. Let 𝑻[𝟏. . 𝒏] be a sorted array of distinct integers. Give an algorithm that can find an index
𝒊such that 𝟏 ≤ 𝒊 ≤ 𝒏 and 𝑻[𝒊] = 𝒊, provided such an index exists. Prove that your
algorithm takes time in 𝑂(𝑙𝑜𝑔𝑛) in the worst case.
𝟎𝟗𝟖𝟏 𝟏𝟐𝟑𝟒
𝒘 = 𝟎𝟗 𝒙 = 𝟖𝟏 𝒚 = 𝟏𝟐 𝒛 = 𝟑𝟒
2. We can write as,
Step 1: 𝒘 = 𝟖𝟏 𝒙 = 𝟏𝟒 𝒚 = 𝟕𝟔 𝒛 = 𝟐𝟐
𝑝 = 𝑤 ∙ 𝑦 = 81 ∙ 76 = 6156
𝑞 = 𝑥 ∙ 𝑧 = 14 ∙ 22 = 308
𝑟 = (𝑤 + 𝑥) ∙ (𝑦 + 𝑧) = 95 ∙ 98 = 9310
8114 × 7622 = 𝟏𝟎𝟒𝒑 + 𝟏𝟎𝟐 (𝒓 − 𝒑 − 𝒒) + 𝒒
= 61560000 + 284600 + 308
= 61844908
Introduction
Merge Sort is an example of divide and conquer algorithm.
It is based on the idea of breaking down a list into several sub-lists until each sub list consists
of a single element.
Merging those sub lists in a manner that results into a sorted list.
Procedure
Divide the unsorted list into N sub lists, each containing 1 element
Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. N will now convert
into N/2 lists of size 2
Repeat the process till a single sorted list of all the elements is obtained
Unsorted Array
724 521 2 98 529 31 189 451
1 2 3 4 5 6 7 8
1 2 1 2 Split 1 2 1 2
724 521 2 98 529 31 189 451
1 1 1 1 1 1 1 1
724 521 2 98 529 31 189 451
Matrix Multiplication
Multiply following two matrices. Count how many scalar multiplications are required.
1 3 6 8
∙
7 5 4 2
1×6+3×4 1×8+3×2
𝑎𝑛𝑠𝑤𝑒𝑟 =
7×6+5×4 7×8+5×2
Matrix Multiplication
In general, 𝐴 and 𝐵 are two 2 × 2 matrices to be multiplied.
𝐴11 𝐴12 𝐵11 𝐵12
𝐴= and 𝐵 =
𝐴21 𝐴21 𝐵21 𝐵22
𝜽 𝒏𝒌 𝒊𝒇 𝒍 < 𝒃𝒌
𝒕 𝒏 = 𝜽 𝒏𝒌 𝒍𝒐𝒈𝒏 𝒊𝒇 𝒍 = 𝒃𝒌
𝜽 𝒏𝒍𝒐𝒈𝒃 𝒍 𝒊𝒇 𝒍 > 𝒃𝒌
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Introduction
Quick sort chooses the first element as a pivot element, a lower bound is the first index and an
upper bound is the last index.
The array is then partitioned on either side of the pivot.
Elements are moved so that, those greater than the pivot are shifted to its right whereas the
others are shifted to its left.
Each Partition is internally sorted recursively.
Pivot
Element
0 1 2 3 4 5 6 7 8 9
42 23 74 11 65 58 94 36 99 87
LB UB
p ← T[i] 94 74 99
87 87
99
k ← i; l ← j+1 k l
Repeat
Swap
k ← k+1 until T[k] > p or k ≥ j
Repeat
94 74 94
87 87 99
l ← l-1 until T[l] ≤ p k l
While k < l do LB UB
Swap T[k] and T[l] Swap
Repeat k ← k+1 until 74 87
87 74 94 99
T[k] > p
Repeat l ← l-1 until k l
T[l] ≤ p 11 23 36 42 58 65 74 87 94 99
Swap T[i] and T[l]
Exponentiation - Sequential
Let 𝑎 and 𝑛 be two integers. We wish to compute the exponentiation𝒙 = 𝒂𝒏 .
Algorithm using Sequential Approach:
function exposeq(a, n)
r ← a
for i ← 1 to n - 1 do
r ← a * r
return r
Exponentiation - Sequential
But to handle larger operands, we must consider the time required for each multiplication.
Let 𝒎is the size of operand 𝒂.
Therefore, the multiplication performed the 𝒊𝒕𝒉 time round the loop concerns an integer of size
𝒎and an integer whose size is between 𝒊𝒎 − 𝒊 + 𝟏 and 𝒊𝒎, which takes a time between
Exponentiation - Sequential
The total time 𝑇(𝑚, 𝑛) spent multiplying when computing an with exposeq is therefore,
𝑛−1 𝑛−1
𝑀 𝑚, 𝑖𝑚 − 1 + 1 ≤ 𝑇 𝑚, 𝑛 ≤ 𝑀 𝑚, 𝑖𝑚
𝑖=1 𝑖=1
𝑛−1 𝑛−1
𝑇 𝑚, 𝑛 ≤ 𝑀 𝑚, 𝑖𝑚 ≤ 𝑐𝑚 𝑖𝑚
𝑖=1 𝑖=1
𝑛−1
𝑐𝑚2 𝑖 ≤ 𝑐𝑚2 𝑛2 = 𝜽 𝒎𝟐 𝒏𝟐
𝑖=1
Exponentiation – D & C
Suppose, we want to compute 𝒂𝟏𝟎
We can write as,
𝑎10 = (𝑎5)2 = (𝑎. 𝑎4)2 = (𝑎. (𝑎2 )2)2
In general,
𝑎 𝑖𝑓 𝑛 = 1
2
𝑎𝑛 = ൞ 𝑎𝑛Τ2 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
𝑎 × 𝑎𝑛−1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Algorithm using Divide & Conquer Approach:
function expoDC(a, n)
if n = 1 then return a
if n is even then return [expoDC(a, n/2)]2
return a * expoDC(a, n - 1)
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Exponentiation – D & C
0 𝑖𝑓 𝑛 = 1
Number of operations performed by 𝑁 𝑛 = ൞𝑁 𝑛Τ2 + 1 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
the algorithm is given by, 𝑁 𝑛 − 1 + 1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
0 𝑖𝑓 𝑛 = 1
Time taken by the 𝑇 𝑚, 𝑛 = ൞𝑇 𝑚, 𝑛Τ2 + 𝑀 𝑚 𝑛Τ2, 𝑚 𝑛Τ2 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
algorithm is given by, 𝑇 𝑚, 𝑛 − 1 + 𝑀 𝑚, 𝑛 − 1 𝑚 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Solving it gives, 𝑻(𝒎, 𝒏) ∈ 𝛉 (𝒎𝒍𝒈𝟑 𝒏𝒍𝒈𝟑 )
function expoDC(a, n)
if n = 1 then return a
if n is even then return [expoDC(a, n/2)]2
return a * expoDC(a, n - 1)
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Exponentiation – Summary
Multiplication
Classic D&C
exposeq 𝜽 𝒎𝟐 𝒏𝟐 𝜽 𝒎𝒍𝒈𝟑 𝒏𝟐
expoDC 𝜽 𝒎𝟐 𝒏𝟐 𝜽 𝒎𝒍𝒈𝟑 𝒏𝒍𝒈𝟑