0% found this document useful (0 votes)
21 views

JNTUA Advanced Data Structures & Algorithms PPT Notes - R20 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

JNTUA Advanced Data Structures & Algorithms PPT Notes - R20 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 234

www.android.universityupdates.in | www.universityupdates.in | https://telegram.

me/jntua

UNIT I
ITERATIVE AND RECURSIVE ALGORITHMS

Iterative Algorithms: Measures of Progress and Loop Invariants-Paradigm Shift: Sequence of


Actions versus Sequence of Assertions- Steps to Develop an Iterative Algorithm-Different
Types of Iterative Algorithms-Typical Errors-Recursion-Forward versus Backward- Towers
of Hanoi-Checklist for Recursive Algorithms-The Stack Frame-Proving Correctness with
Strong Induction- Examples of Recursive Algorithms-Sorting and Selecting Algorithms-
Operations on Integers Ackermann’s Function- Recursion on Trees-Tree Traversals-
Examples- Generalizing the Problem -Heap Sort and Priority Queues-Representing
Expressions.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


1

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Iterative Algorithms: Measures of Progress and


Loop Invariants

• An iterative algorithm to solve a computational problem is a


bit like following a road, possibly long and difficult, from your
start location to your destination.
• With each iteration, you have a method that takes you a single
step closer.
• To ensure that you move forward, you need to have a measure
of progress telling you how far you are either from your
starting location or from your destination.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• 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.

IFETCE/M.E(CSE)/IYEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• 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.

IFETCE/M.E(CSE)/IYEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

A Paradigm Shift: A Sequence of Actions vs a


Sequence
of Assertions
• Iterative algorithms requires understanding the difference between a
loop invariant, which is an assertion or picture of the computation at
a particular point in time, and the actions that are required to
maintain such a loop invariant.
• One of the first important paradigm shifts that programmers struggle
to make is from viewing an algorithm as a sequence of actions to
viewing it as a sequence of snapshots of the state of the computer.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 5

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• Programmers tend to fixate on the first view, because code is a


sequence of instructions for action and a computation is a
sequence of actions.
• Imagine stopping time at key points during the computation and
taking still pictures of the state of the computer.
• Then a computation can equally be viewed as a sequence of such
snapshots.
• Having two ways of viewing the same thing gives one both more
tools to handle it and a deeper understanding of it.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 6

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

The Challenge of the Sequence-of-Actions View


• Suppose one is designing a new algorithm or explaining an
algorithm to a friend.
• If one is thinking of it as sequence of actions, then one will
likely start at the beginning: Do this. Do that. Do this.
• Shortly one can get lost and not know where one is.
• To handle this, one simultaneously needs to keep track of how
the state of the computer changes with each new action.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 7

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• In order to know what action to take next, one needs to have a


global plan of where the computation is to go.
• To make it worse, the computation has many IFs and LOOPS
so one has to consider all the various paths that the
computation may take.
Advantages of the Sequence of Snapshots View:
• This new paradigm is useful one from which one can think
about, explain, or develop an algorithm.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


8

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Steps to Develop an Iterative Algorithm

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

10

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

The Code Structure:


• The basic structure of the code is as follows.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 11

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 12

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Steps to Develop an Iterative Algorithm:


1.Specifications
2.Basic Steps
3.Measure of Progress
4.The Loop Invariant
5.Main Steps
6.Make Progress
7.Maintain Loop Invariant
8.Establishing the Loop Invariant
9.Exit Condition
10.Ending

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 13

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

11. Termination and Running Time


12. Special Cases
13. Coding and Implementation Details
14. Formal Proof

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 14

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Different Types of Iterative Algorithms


• More of the Output—Selection Sort
1) Specifications:
The goal is to rearrange a list of n values in no decreasing order.
2) Basic Steps:
We will repeatedly select the smallest unselected element.
3) Measure of Progress:
The measure of progress is the number k of elements selected.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 15

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

4)The Loop Invariant:


The loop invariant states that the selected elements are the k
smallest of the elements and that these have been sorted. The
larger elements are in a set on the side.
5) Main Steps:
The main step is to find the smallest element from among those
in the remaining set of larger elements and to add this newly
selected element to the end of the sorted list of elements.
6) Make Progress:
Progress is made because k increases.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 16

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

7) Maintain Loop Invariant:


• We must prove that loop-invariant & not exit−cond & codeloop
⇒ loop-invariant .
• By the previous loop invariant, the newly selected element is at
least the size of the previously selected elements.
• By the step, it is no bigger than the elements on the side.
• It follows that it must be the k + 1st element in the list.
• Hence, moving this element from the set on the side to the end of
the sorted list ensures that the selected elements in the new list are
the k + 1 smallest and are sorted.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 17

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

8) Establishing the Loop Invariant: We must prove that pre-


cond & codepre-loop ⇒loop-invariant Initially, k = 0 are sorted
and all the elements are set aside.
9) Exit Condition:
Stop when k = n.
10) Ending: We must prove loop-invariant & exit-cond &
codepost-loop ⇒post-cond . By the exit condition, all the
elements have been selected, and by the loop invariant these
selected elements have been sorted.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


18

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

11) Termination and Running Time: We have not considered


how long it takes to find the next smallest element or to handle
the data structures.
Typical Errors:
Be Clear:
• The code specifies the current subinterval A[i.. j ]with two
integers i and j .
• Clearly document whether the sublist includes the end points i
and j or not. It does not matter which, but you must be
consistent. Confusion in details like this is the cause of many
bugs.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 19

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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]

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

20

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Recursion-Forward versus Backward

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).

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


21

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Stack of Stack Frames:


• Recursive algorithms are executed using a stack of stackframes.
Tree of Stack Frames:
• This is a useful way of viewing the entire computation at once.
• It is particularly useful when computing the running time of the
algorithm.
• However, the structure of the computation tree may be very
complex and difficult to understand all at once.

IFETCE/M.E(CSE)/IYEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


22

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Friends, on Strong Induction:


• You construct for each friend an instance of the same
computational problem that is smaller then your own.This is
referred as subinstances.
• Your friends magically provide you with the solutions to these.
• You then combine these subsolutions into a solution for your
original instance.
• I refer to this as the friends level of abstraction.
• If you prefer, you can call it the strong induction level of
abstraction and use the word “recursion” instead of “friend.”

IFETCE/M.E (CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 23

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Forward vs. Backward


• Recursion involves designing an algorithm by using it as if it
already exists. At first this looks paradoxical.
• Suppose, for example, the key to the
• house that you want to get into is in that same house. If you
could get in, you could get the key.
• Then you could open the door, so that you could get in. This is
a circular argument.
• It is not a legal recursive program because the subinstance is
not smaller.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 24

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Working Forward vs. Backward

• An iterative algorithm works forward.


• It knows about house i − 1. It uses a loop invariant to show
that this house has been opened.
• It searches this house and learns that the key within it is that
for house i.
• Because of this, it decides that house i would be a good one to
go to next.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

25

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• A recursion algorithm works backward. It knows about house i. It wants


to get it open.
• It determines that the key for house i is contained in house i − 1.
• Hence, opening house i − 1 is a subtask that needs to be accomplished.
Advantages of recursive algorithms over iterative ones:
• The first is that sometimes it is easier to work backward than forward.
• The second is that a recursive algorithm is allowed to have more than
one subtask to be solved.
• This forms a tree of houses to open instead of a row of houses.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 26

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 27

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• 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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


28

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 29

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Checklist for Recursive Algorithms

• Writing a recursive algorithm is surprisingly hard when you


are first starting out and surprisingly easy when you get it.
• This section contains a list of things to think about to make
sure that you do not make any of the common mistakes.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


30

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

32

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• This is why the friends level of abstraction, which considers one


stack frame at a time, is the best way to understand, explain, and
design a recursive algorithm
• However, it is also useful to have some picture of the entire
computation.
• For this, the tree-of-stack-frames level of abstraction is best.
• The key thing to understand is the difference between a particular
routine and a particular execution of a routine on a particular
input instance.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


33

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• A single routine can at one moment in time have many


executions going on. Each such execution is referred to as a stack
frame.
Stack of Stack Frames:
• The algorithm is actually implemented on a computer by a stack
of stack frames. What is stored in the computer memory at any
given point in time is only a single path down the tree.
• The tree represents what occurs throughout time.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 34

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Using a Stack Frame:


• Recall that a stack is a data structure in which either a new
element is pushed onto the top or the last element to have been
added is popped off
• Let us denote the top stack frame by A. When the execution of A
makes a subroutine call to a routine with some input values, a
stack frame is created for this new instance.
• This frame denoted B is pushed onto the stack after that for A.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


35

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• In addition to a separate copy of the local variables for the


routine, it contains a pointer to the next line of code that A must
execute when B returns.
• When B returns, its stack frame is popped, and A continues to
execute at the line of code that had been indicated within B.
• When A completes, it too is popped off the stack.

IFETCE/M.E(CSE) /I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


36

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Proving Correctness with


Strong Induction
Strong Induction:
• Strong induction is similar to induction, except that instead of
assuming only S(n − 1) to prove S(n), you must assume all of
S(0), S(1), S(2), . . . ,S(n − 1).
A Statement for Each n:
For each value of n ≥ 0, let S(n) represent a Boolean statement.
For some values of n this statement may be true, and for others it
may be false.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 37

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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).

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 38

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Examples of Recursive Algorithms


Sorting and Selecting Algorithms:
• The classic divide-and-conquer algorithms are merge sort and
quick sort. They bothhave the following basic structure.
General Recursive Sorting Algorithm:
• Take the given list of objects to be sorted (numbers, strings,
student records, etc.).
• Split the list into two sublists.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


39

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• Recursively have friends sort each of the two sublists.


• Combine the two sorted sublists into one entirely sorted list.
• This process leads to four different algorithms, depending on the
following factors,
• Sizes
• Work
Operations on Integers:
• Raising an integer to a power bN, multiplying x × y, and matrix
multiplication each have surprising divide-and-conquer
algorithms.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


40

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Ackermann’s Function

• If you are wondering just how slowly a program can run,


consider the algorithm below.
• Assume the input parameters n and k are natural numbers.
Recurrence Relation:
• Let Tk (n) denote the value returned by A(k, n). This gives
T0(n) = 2 +n, T1(0) = 0, Tk (0) = 1 for k ≥ 2, and Tk (n) =
Tk−1(Tk (n − 1)) for k > 0 and n > 0.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 41

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


42

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

43

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Recursive Definition of Tree: A tree is either:


– an empty tree (zero nodes) or
– a root node with some subtrees as children.
– A binary tree is a special kind of tree where each node has
a right and a left subtree
Tree Traversals:
A task one needs to be able to perform on a binary tree is to
traverse it, visiting each node once, in one of three defined
orders.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


44

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

• Before one becomes familiar with recursive programs, one


tends to think about computation iteratively, “I visit this node
first, then this one, then this one, and so on.”
• Each iteration, the program says “I just visited this node, so
now let me find the next node to visit.” Surprisingly, such a
computation is hard to code.
• The reason is that binary trees by their very nature have a
recursive structure.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


45

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Examples

• Here is a list of problems involving binary trees.


• 1. Return the maximum of data fields of nodes.
• 2. Return the height of the tree.
• 3. Return the number of leaves in the tree. (A harder one.)
• 4. Copy the tree.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2

46

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Generalizing the Problem


• When writing a recursive algorithm for a problem it is easier to
solve a more general version of the problem, providing more
information about the original instance or asking for more
information about subinstances.
Subinstance:
• It is better to combine the IsBSTtree and the Min and Max
routines into one routine so that the tree only needs to be
traversed once.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


47

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 48

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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, [−∞,∞]).

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


49

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


50

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Heap Sort and Priority Queues


• Heap sort is a fast sorting algorithm that is easy to implement.
• Like quick sort, it has the advantage of being done in place in
memory, whereas merge and radix–counting sorts require an
auxiliary array of memory to transfer the data to.
Completely Balanced Binary Tree:
• The values being sorted as stored in a binary tree that is
completely balanced, i.e., every level of the tree is completely
full except for the bottom level, which is filled in fromthe left.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 51

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


52

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

The Heapify Problem:


Specifications:
• Precondition: The input is a balanced binary tree such that its
left and right subtrees are heaps. (That is, it is a heap except that
its root might not be larger than that of its children)
• Postcondition: Its values are rearranged in place to make it
complete heap.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


53

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 54

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

The MakeHeap Problem:


Specifications:
• Precondition: The input is an array of numbers, which can be
viewed as a balanced binary tree of numbers.
• Postcondition: Its values are rearranged in place to make it
heap.
The Heap Sort Problem:
Specifications:
• Precondition: The input is an array of numbers.
• Postcondition: Its values are rearranged in place to be in
sorted order.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


55

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 56

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 57

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 58

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Representing Expressions with Trees

• consider how to represent multivariate expressions using binary


trees.
• We will develop the algorithms to evaluate, copy, differentiate,
simplify, and print such an expression.
• Though these are seemingly complex problems, they have
simplerecursive solutions.

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2


59

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Recursive Definition of an Expression:


• Single variables x, y, and z and single real values are themselves
expressions.
• If f and g are expressions, then f + g, f − g, f ∗ g, and f/g are also
expressions.
Tree Data Structure: The recursive definition of an expression
directly mirrors that of a binary tree. Because of this, a binary tree is
a natural data structure for storing an expression. (Conversely, you
can use an expression to represent a binary tree.)

IFETCE/M.E(CSE)/I YEAR/I SEM/ADS/UNIT-I/PPT/VER 1.2 60

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

Non-Linear Data Structure


Tree Part-3
 Height/Weight – Balanced Tree
 Multiway Search Tree (B-Tree)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Balanced Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Height Balanced Tree (AVL Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 AVL tree is a height-balanced binary search tree.


 AVL tree is also a binary search tree but it is a balanced tree. A binary tree is said to be balanced
if, the difference between the heights of left and right subtrees of every node in the tree is either -
1, 0 or +1.
 In other words, a binary tree is said to be balanced if the height of left and right children of every
node differ by either -1, 0 or +1.
 In an AVL tree, every node maintains an extra information known as balance factor.
 The AVL tree was introduced in the year 1962 by G.M. Adelson-Velsky and E.M. Landis.
 An AVL tree is defined as follows...
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every node is either -1, 0 or +1.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Balance Factor
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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 the following explanation, we calculate as follows...


Balance factor = heightOfLeftSubtree – heightOfRightSubtree

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 4
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Example of AVL Tree

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 5
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Example of AVL Tree

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 6
Example of AVL Tree - Unbalanced
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 7
AVL Tree Rotations
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 In AVL tree, after performing operations like insertion and deletion


 we need to check the balance factor of every node in the tree.
 If every node satisfies the balance factor condition then we conclude the operation otherwise we
must make it balanced.
 Whenever the tree becomes imbalanced due to any operation we use rotation operations to make
the tree balanced.
Rotation operations are used to make the tree balanced.
 Rotation is the process of moving nodes either to left or to right to make the tree balanced.

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 8
AVL Tree Rotations
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

There are basically four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A

2. R R rotation : Inserted node is in the right subtree of right subtree of A

3. L R rotation : Inserted node is in the right subtree of left subtree of A

4. R L rotation : Inserted node is in the left subtree of right subtree of A

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 9
AVL Tree Rotations
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 10
Single Left Rotation (LL Rotation)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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...

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 11
Single Right Rotation (RR Rotation)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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...

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 12
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Left Right Rotation (LR Rotation)


 The LR Rotation is a sequence of single left rotation followed by a single right rotation.

 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...

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 13
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Right Left Rotation (RL Rotation)


The RL Rotation is sequence of single right rotation followed by single left rotation.
In RL Rotation, at first every node moves one position to right and one position to left from the
current position.
 To understand RL Rotation, let us consider the following insertion operation in AVL Tree...

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree| https://telegram.me/jntua
Part-3) 14
Construct AVL Search Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct AVL Search tree by inserting following elements in order of


their occurrence 6, 5, 4, 3, 2, 1
Insert 6 Insert 3 Insert 2
5 L 5
B 6 L 5
Critical Node 4 6 Case 1 B 3 B 6
Insert 5
L 4 B 6
Right Rotation
of Node 4
L 6 L 3 B 2 B 4
B 3

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

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

Construct AVL Search tree by inserting following elements in order of


their occurrence 64, 1, 44, 26, 13, 110, 98, 85
Insert 64 Insert 26 , 13 Case 3: Right Left Rotation
Right Rotation of Right Child 26
B 64 Followed By
L 44 44 L 44
Left Rotation of Parent 1
Critical
Insert 1
Node
R 1 64 1 64 B 13 B 64

L 64 Right Rotation Left Rotation


B L’ 26 of Right Child 26 13 of Parent 1 B 1 B 26

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

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

Construct AVL Search tree by inserting following elements in order of


their occurrence 64, 1, 44, 26, 13, 110, 98, 85
Insert 110 , 98 Case 3: Right Left Rotation
Right Rotation of Right Child 110
B 44 Followed By 44 B 44
Left Rotation of Parent 64
B 13 R 64 Critical Node 13 64 B 13 B 98
Right Rotation Left Rotation
B 1 B 26 B L’ 110 1 26 98 of Parent 64 B 1 B 26 B 64 B 110
of Right Child 110

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

Construct AVL Search tree by inserting following elements in order of


their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 60 Insert 76 ,79 Insert 82

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

Construct AVL Search tree by inserting following elements in order of


their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 300 , 0 Insert 5 Case 2: Left Right Rotation,
76 Critical Left Rotation of Left Child 0, 76
76 B
Followed By
73 81 Right Rotation of Parent 60 73 81
L 73 81 R
60 75 79 82 5 75 79 82
L 60 75 79 82 R
B B 0 60
R 0 300 300
0 B B 300

B 5

Insert 73

Can not Insert 73 as duplicate key found

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deleting node from AVL Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deleting node from AVL Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deleting node from AVL Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Delete 73, 74 Delete 73


73
L 74
73
13 75
L 13 75 R
10 28 74 89
L 10 B 28 74 89 B
5 In-Order Traversal
5, 10, 13, 28, 73, 74, 75, 89 5 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Weight Balanced Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Weight Balanced Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Probability

P 23

20 21
E T

K 15 S V
14 13
11 G 10 M

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Weight Balanced Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Multiway Search Tree (B - Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Multiway Search Tree (B - Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 An M- way tree contains multiple keys in a node


 This leads to reduction in overall height of the tree
 If a node of M-way tree holds K keys then it will have k+1 children

No of Keys = 3

K1, K2, K3
No of Ways or children = 4

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Multiway Search Tree (B - Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 A tree of order M is a M-way search tree with the following properties


1. The Root can have 1 to M-1 keys
2. All nodes (except Root) have (M-1)/2 to (M-1) keys
3. All leaves are at the same level
4. If a node has ‘t’ number of children, then it must have ‘t-1’ keys
5. ​Keys of the nodes are stored in ascending order

K0, K1, K2, ……… ,Kn-1

P0 P1 P2 Pn-1 Pn

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Multiway Search Tree (B - Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

K0, K1, K2, ……… ,Kn-1

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
 ………..

 ………..

 Kn-1 >= all keys of sub-tree Pn-1


 Kn-1 < all keys of sub-tree Pn

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Multiway Search Tree (B - Tree)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

20

10, 15 25, 40, 50

2, 8 11 16, 18 22 30, 35 42, 44 55, 60

B-Tree of Order 4 (4 way Tree)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Insertion of Key in B-Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

1. If Root is NULL, construct a node and insert key


2. If Root is NOT NULL
I. Find the correct leaf node to which key should be added
II. If leaf node has space to accommodate key, it is inserted and sorted
III. If leaf node does not have space to accommodate key, we split node into two parts

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Split Node (5 way Tree, max 4 Keys)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

P
5, 10, 15, 20 1, 5, 9, 11

Insert - 3 Insert - 3 Overflow


3, 5, 10, 15, 20 1, 3, 5, 9, 11
Overflow
5
10
1, 3 9, 11
3, 5 15, 20

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Split Node (5 way Tree, max 4 Keys)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35 42, 45, 48

Insert - 38

10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35 ,38 42, 45, 48

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Split Node (5 way Tree, max 4 Keys)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

Insert 7

10, 20, 30, 40

3, 5, 6, 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

Overflow

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Split Node (5 way Tree, max 4 Keys)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

6, 10, 20, 30, 40 Overflow

3, 5 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

20

6, 10 30, 40

3, 5 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct M-Way Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct 5 Order (5 Way) Tree from following data


1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
We are asked to create 5 Order Tree (5 Way Tree) maximum 4 records can be accommodated in a node

Insert 1 Insert 7 Insert 6 Insert 2 Insert 11


1, 2, 6, 7, 11 6
1 1, 7 1, 6, 7 1, 2, 6, 7
Overflow 1, 2 7, 11
Insert 5 Insert 10 Insert 13
6 6 6

1,2,5 7,11 1,2,5 7,10,11 1,2,5 7,10,11,13

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct M-Way Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct 5 Order (5 Way) Tree from following data


1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
Insert 12 Insert 20, 16, 24

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct M-Way Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct 5 Order (5 Way) Tree from following data


1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
Insert 18,19,14
3, 6, 11, 16

1,2 4,5 7,10 12, 13, 14 18, 19, 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deletion - M-Way Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

Deletion is also performed at the leaf nodes.


The node which is to be deleted can either be a leaf node or an internal node.
Following algorithm needs to be followed in order to delete a node from a B tree.
1. Locate the leaf node.
2. If there are more than m/2 keys in the leaf node then delete the desired key from the
node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element
from right or left sibling.
3.1 If the left sibling contains more than m/2 elements then push its largest element
up to its parent and move the intervening element down to the node where the
key is deleted.
3.2 If the right sibling contains more than m/2 elements then push its smallest
element up to the parent and move intervening element down to the node where
the key is deleted.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deletion - M-Way Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Deletion in B-Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

For deletion in b tree we wish to remove from a leaf. T


There are three possible case for deletion in b tree.

Let k be the key to be deleted, x the node containing the key.

Then the cases are:

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Case-I
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
6 deleted GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Case-II
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702
If key k is in node x and x is an internal node, there are three cases to consider:

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
13 deleted GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702
Time and Space complexity of Binary Search Tree (BST)

OPERATION WORST CASE AVERAGE CASE BEST CASE SPACE

Search O(N) O(logN) O(1) O(N)


Insert O(N) O(logN) O(1) O(N)
Delete O(N) O(logN) O(N) O(N)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702
Time & Space Complexity of AVL Tree operations

OPERATION BEST CASE AVERAGE WORST CASE


CASE
Insert O (log n) O (log n) O (log n)
Delete O (log n) O (log n) O (log n)
Search O (1) O (log n) O (log n)
Traversal O (log n) O (log n) O (log n)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702
B-Tree : Searching and Insertion

Complexity

•Worst case search time complexity: Θ(logn)

•Average case search time complexity: Θ(logn)

•Best case search time complexity: Θ(logn)

•Average case Space complexity: Θ(n)

•Worst case Space complexity: Θ(n)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

Non-Linear Data Structure


Tree Part-2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Red - Black Tree


 We can d
 Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.
Define a Red Black Tree as follows...
Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Preorder Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Preorder traversal of a binary tree is defined as follow


1. Process the root node A ✓

2. Traverse the left subtree in preorder


3. Traverse the right subtree in preorder B ✓ D ✓

 If particular subtree is empty (i.e., node has no left or


C ✓ E ✓ G ✓
right descendant) the traversal is performed by doing
nothing.
F ✓
 In other words, a null subtree is considered to be fully
traversed when it is encountered. Preorder traversal of a given tree as

A B C D E F G

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Inorder Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Inorder traversal of a binary tree is defined as follow


A ✓
1. Traverse the left subtree in Inorder
2. Process the root node
B ✓ D ✓
3. Traverse the right subtree in Inorder

C ✓ E ✓ G ✓

F ✓

Inorder traversal of a given tree as

C B A E F D G

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Postorder Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Postorder traversal of a binary tree is defined as follow


A ✓
1. Traverse the left subtree in Postorder
2. Traverse the right subtree in Postorder
B ✓ D ✓
3. Process the root node

C ✓ E ✓ G ✓

F ✓

Postorder traversal of a given tree as


C B F E G D A

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Converse Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Write Pre/In/Post Order Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Linked Representation of Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

T
LPTR DATA RPTR

Typical node of Binary Tree A

A
B D

B D

C E G
C E G

F
F

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Algorithm of Binary Tree Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Preorder Traversal - Procedure: RPREORDER(T)


 Inorder Traversal - Procedure: RINORDER(T)
 Postorder Traversal - Procedure: RPOSTORDER(T)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Procedure: RPREORDER(T)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 This procedure traverses the tree in preorder, in a recursive manner.


 T is root node address of given binary tree LPTR DATA RPTR

 Node structure of binary tree is described as below Typical node of Binary Tree

1. [Check for Empty Tree] 3. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RPREORDER (RPTR (T))
return 4. [Finished]
ELSE write (DATA(T)) Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPREORDER (LPTR (T))

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Procedure: RINORDER(T)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 This procedure traverses the tree in InOrder, in a recursive manner.


 T is root node address of given binary tree. LPTR DATA RPTR

 Node structure of binary tree is described as below. Typical node of Binary Tree

1. [Check for Empty Tree] 4. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RINORDER (RPTR (T))
return 5. [Finished]
2. [Process the Left Sub Tree] Return
IF LPTR (T) ≠ NULL
THEN RINORDER (LPTR (T))
3. [Process the Root Node]
write (DATA(T))

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Procedure: RPOSTORDER(T)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 This procedure traverses the tree in PostOrder, in a recursive manner.


 T is root node address of given binary tree. LPTR DATA RPTR

 Node structure of binary tree is described as below. Typical node of Binary Tree

1. [Check for Empty Tree] 4. [Process the Root Node]


IF T = NULL write (DATA(T))
THEN write (‘Empty Tree’) 5. [Finished]
return Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPOSTORDER (LPTR (T))
3. [Process the Right Sub Tree]
IF RPTR (T) ≠ NULL
THEN RPOSTORDER (RPTR (T))

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct Binary Tree from Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct Binary Tree from Traversal
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Linked Representation of Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

T
LPTR DATA RPTR

Typical node of Binary Tree A

A
B D

B D

C E G
C E G

F
F

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Method 1:- Represent thread a Negative address


 Method 2:- To have a separate Boolean flag for each of left and right pointers, node structure for
this is given below
LPTR LTHREAD DATA RTHREAD RPTR
Typical node of Threaded Binary Tree

• LTHREAD = true = Denotes leaf thread link


HEAD
• LTHREAD = false = Denotes leaf structural link
• RTHREAD = true = Denotes right threaded link
• RTHREAD = false = Denotes right structural link

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

HEAD
A

B D
A

C E G
B D

C E G
Inorder Traversal

C B A E F D G
F

Fully In-Threaded Binary Tree


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct Right In-Threaded Binary Tree of given Tree


A
HEAD

B E

A
C D F H

B E
G

Inorder Traversal
C D F H
CBDAFGEH
G

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Advantages of Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Inorder traversal is faster than unthreaded version as stack is not required.


 Effectively determines the predecessor and successor for inorder traversal, for unthreaded tree
this task is more difficult.
 A stack is required to provide upward pointing information in binary tree which threading
provides without stack.
 It is possible to generate successor or predecessor of any node without having over head of
stack with the help of threading.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Disadvantages of Threaded Binary Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 Threaded trees are unable to share common sub trees.


 If Negative addressing is not permitted in programming language, two additional fields are
required.
 Insertion into and deletion from threaded binary tree are more time consuming because both
thread and structural link must be maintained.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Binary Search Tree (BST)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Construct Binary Search Tree (BST)
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Construct binary search tree for the following data


50 , 25 , 75 , 22 , 40 , 60 , 80 , 90 , 15 , 30

50

25 75

22 40 60 80

15 30 90

Construct binary search tree for the following data


10, 3, 15, 22, 6, 45, 65, 23, 78, 34, 5
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
Search a node in Binary Search Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 To search for target value.


 We first compare it with the key at root of the tree.
 If it is not same, we go to either Left sub tree or Right sub tree as appropriate and repeat the
search in sub tree.
 If we have In-Order List & we want to search for specific node it requires O(n) time.
 In case of Binary tree it requires O(Log2n) time to search a node.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Delete node from Binary Search Tree
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Delete node a

a Delete from Leaf Node

a Delete node a b

Delete from Non Terminal (Empty Left Sub Tree)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


Delete node from BST
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Delete node a

a C

b C

Delete from Non Terminal (Neither Sub Tree is Empty)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

THANK YOU

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Data Structures (DS)
GTU # 3130702

Non-Linear Data Structure


Tree Part-2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Red - Black Tree


 Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.
We can define a Red Black Tree as follows...
Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.

 Every Red Black Tree has the following properties.


Properties of Red Black Tree
 Property #1: Red - Black Tree must be a Binary Search Tree.
 Property #2: The ROOT node must be colored BLACK.
 Property #3: The children of Red colored node must be colored BLACK. (There should not be two consecutive RED nodes).
 Property #4: In all the paths of the tree, there should be same number of BLACK colored nodes.
 Property #5: Every new node must be inserted with RED color.
 Property #6: Every leaf (e.i. NULL node) must be colored BLACK.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 5
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 6
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 7
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 8
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 9
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 10
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 11
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 12
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 13
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 14
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 15
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Dr. Pradyumansinh U. Jadeja #3130702 (DS)  Unit| 3www.previousquestionpapers.com


www.android.previousquestionpapers.com – Non-Linear Data Structure (Tree Part-2)
| https://telegram.me/jntua 16
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Analysis and Design of Algorithms (ADA)
GTU # 3150703

Unit-3:
Divide and Conquer
Algorithms

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Methods to Solve Recurrence


 Substitution
 Homogeneous (characteristic equation)
 Inhomogeneous
 Master method
 Recurrence tree
 Intelligent guess work
 Change of variable
 Range transformations

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Substitution Method – Example 1


 We make a guess for the solution and then we use mathematical induction to prove the guess
is correct or incorrect.
Time to solve the
Example 1: instance of size 𝑛 − 1

𝑻(𝒏) = 𝑻(𝒏 − 𝟏) + 𝒏 1

 Replacing 𝑛Time
by 𝑛to − 1 and
solve the 𝑛 − 2, we can write following equations.
instance of size 𝑛
𝑻 𝒏−𝟏 =𝑻 𝒏−𝟐 +𝒏−𝟏 2

𝑻 𝒏−𝟐 =𝑻 𝒏−𝟑 +𝒏−𝟐 3

 Substituting equation 3 in 2and equation 2 in 1we have now,


𝑻(𝒏) = 𝑻(𝒏 − 𝟑) + 𝒏 − 𝟐 + 𝒏 − 𝟏 + 𝒏 4

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Substitution Method – Example 1

𝑻(𝒏) = 𝑻(𝒏 − 𝟑) + 𝒏 − 𝟐 + 𝒏 − 𝟏 + 𝒏 4

 From above, we can write the general form as,


𝑻 𝒏 = 𝑻 𝒏 − 𝒌 + (𝒏 − 𝒌 + 𝟏) + (𝒏 − 𝒌 + 𝟐) + … + 𝒏

 Suppose, if we take 𝑘 = 𝑛 then,

𝑻 𝒏 = 𝑻 𝒏 − 𝒏 + (𝒏 − 𝒏 + 𝟏) + (𝒏 − 𝒏 + 𝟐) + … + 𝒏

𝑻 𝒏 = 𝟎 + 𝟏 + 𝟐 + …+ 𝒏

𝒏 𝒏+𝟏
𝑻 𝒏 = = 𝑶 𝒏𝟐
𝟐
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Substitution Method – Example 2


𝑐1 𝑖𝑓 𝑛 = 0
𝑡 𝑛 =ቊ
𝑐2 + 𝑡 𝑛 − 1 𝑜/𝑤

 Rewrite the equation, 𝑡 𝑛 = 𝑐2 + 𝑡(𝑛 − 1)


 Now, replace 𝐧 by 𝐧 – 𝟏 and 𝐧 − 𝟐
𝑡 𝑛 − 1 = 𝑐2 + 𝑡(𝑛 − 2) ∴ 𝑡 𝑛 − 1 = 𝑐2 + 𝑐2 + 𝑡(𝑛 − 3)
𝑡 𝑛 − 2 = 𝑐2 + 𝑡(𝑛 − 3)
 Substitute the values of 𝐧 – 𝟏 and 𝐧 − 𝟐
𝑡 𝑛 = 𝑐2 + 𝑐2 + 𝑐2 + 𝑡(𝑛 − 3)
 In general,
𝑡 𝑛 = 𝑘𝑐2 + 𝑡(𝑛 − 𝑘)
 Suppose if we take 𝑘 = 𝑛 then,
𝑡 𝑛 = 𝑛𝑐2 + 𝑡 𝑛 − 𝑛 = 𝑛𝑐2 + 𝑡 0
𝑡(𝑛) = 𝑛𝑐2 + 𝑐1 = 𝑶 𝒏
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Substitution Method Exercises


 Solve the following recurrences using substitution method.

1 if n = 0 or 1
1. T n = ቊ
T n − 1 + n − 1 o/w

2. T (n) = T (n − 1) + 1 and T (1) = θ (1).

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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,
𝒌

𝒕𝒏 = ෍ 𝒄𝒊 𝒓𝒏𝒊
𝒊=𝟏

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Homogeneous Recurrence – Example 1 : Fibonacci Series


 Fibonacci series Iterative Algorithm

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

Homogeneous Recurrence – Example 1 : Fibonacci Series


 If the value of 𝒏 is large, then time needed to execute addition operation increases linearly with
the length of operand.
 At the end of 𝑘𝑡ℎ iteration, the value of 𝒊 and 𝒋 will be 𝒇𝒌−𝟏 and 𝒇𝒌.
 As per De Moivre’s formula the size of 𝒇𝒌 is in 𝜽(𝒌).
 So, 𝒌𝒕𝒉 iteration takes time in 𝜽(𝒌). let 𝒄 be some constant such that this time is bounded
above by 𝒄𝒌 for all 𝒌 ≥ 𝟏.
 The time taken by fibiter algorithm is bounded above by,
𝑛 𝑛
𝑛 𝑛+1
෍ 𝑐. 𝑘 = 𝑐. ෍ 𝑘 = 𝑐.
2
𝑘=1 𝑘=1

𝑻 𝒏 = 𝜽 𝒏𝟐
Case 2

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Homogeneous Recurrence – Example 1 : Fibonacci Series


 Recursive Algorithm for Fibonacci series,

Function fibrec(n)
if n < 2 then return n
else return fibrec (n – 1) + fibrec (n – 2)

 The recurrence equation of above algorithm is given as,


𝒏 𝒊𝒇 𝒏 = 𝟎 𝒐𝒓 𝟏
𝑻(𝒏) = ቊ
𝑻 𝒏 − 𝟏 + 𝑻 𝒏 − 𝟐 𝒐/𝒘

 The recurrence can be re-written as,


𝑇 𝑛 −𝑇 𝑛−1 −𝑇 𝑛−2 = 0
 The characteristic polynomial is,
𝑥2 − 𝑥 − 1 = 0
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Homogeneous Recurrence – Example 1 : Fibonacci Series


 Find the roots of characteristic polynomial,
𝑥2 − 𝑥 − 1 = 0 −𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝒙 =
𝟐𝒂
 The roots are, Here, 𝒂 = 𝟏, 𝒃 = 𝟏 and
1+ 5 1− 5 𝒄=𝟏
𝑟1 = and 𝑟2 =
2 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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Homogeneous Recurrence – Example 1 : Fibonacci Series


 Substituting the values of roots and constants in general solution,

𝑻𝒏 = 𝒄𝟏 𝒓𝒏𝟏 + 𝒄𝟐 𝒓𝒏𝟐

𝑛 𝑛
1 1+ 5 1− 5
𝑇𝑛 = − … … … de Moivre′ s formula
5 2 2

𝒏
𝑻𝒏 ∈ 𝑶 ∅

 Time taken for recursive Fibonacci algorithm grows Exponentially.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Example 2 : Tower of Hanoi


tower 1 tower 2 tower 3

tower 1 tower 2 tower 3

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Example 2 : Tower of Hanoi


 The number of movements of a ring required in the tower of Hanoi problem is given by,
𝟎 𝒊𝒇 𝒎 = 𝟎
𝒕 𝒎 = ቊ
𝟐𝒕 𝒎 − 𝟏 + 𝟏 𝒐/𝒘

 The equation can be written as,


𝒕 𝒎 − 𝟐𝒕 𝒎 − 𝟏 = 𝟏 (𝟏) Inhomogeneous equation

 To convert it into a homogeneous equation, multiply with −𝟏 and replace 𝐦 by 𝐦 − 𝟏,


−𝒕 𝒎 − 𝟏 + 𝟐𝒕 𝒎 − 𝟐 = −𝟏 (𝟐)

 Solving equations (1) and (2), we have now


𝒕 𝒎 − 𝟑𝒕 𝒎 − 𝟏 + 𝟐𝒕(𝒎 − 𝟐) = 𝟎

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Example 2 : Tower of Hanoi


 The characteristic polynomial is, 𝒕 𝒎 − 𝟑𝒕 𝒎 − 𝟏 + 𝟐𝒕(𝒎 − 𝟐) = 𝟎
𝒙𝟐 − 𝟑𝒙 + 𝟐 = 𝟎
 Whose roots are,
𝒓𝟏 = 𝟐 and 𝒓𝟐 = 𝟏
 The general solution is therefore of the form,
𝒕𝒎 = 𝒄𝟏 𝟏𝒎 + 𝒄𝟐 𝟐𝒎
 Substituting initial values 𝒎 = 𝟎 and 𝒎 = 𝟏
𝒕𝟎 = 𝒄𝟏 + 𝒄𝟐 = 𝟎 𝟏
𝒕𝟏 = 𝒄𝟏 + 𝟐𝒄𝟐 = 𝟏 (𝟐)

 Solving these linear equations we get 𝒄𝟏 = −𝟏 and 𝒄𝟐 = 𝟏.


 Therefore, time complexity of tower of Hanoi problem is given as,

𝒕 𝒎 = 𝟐 𝒎 − 𝟏 = 𝑶 𝟐𝒎
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Homogeneous Recurrence Exercises


 Solve the following recurrences

𝑛 𝑖𝑓 𝑛 = 0 𝑜𝑟 1
1. 𝑡𝑛 = ቊ5𝑡
𝑛−1 − 6𝑡𝑛−2 𝑂/𝑊

𝑛 𝑖𝑓 𝑛 = 0, 1 𝑜𝑟 2
2. 𝑡𝑛 = ቊ
5𝑡𝑛−1 − 8𝑡𝑛−2 + 4𝑡𝑛−3 𝑜/𝑤

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Master Theorem
 The master theorem is a cookbook method for solving recurrences.
Time to divide &
 Suppose you have a recurrence of the form recombine
𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)

Number of sub- Time required to


problems solve a sub-problem

 This recurrence would arise in the analysis of a recursive algorithm.


 When input size 𝒏 is large, the problem is divided up into 𝒂 sub-problems each of size 𝒏/𝒃.
Sub-problems are solved recursively and results are recombined.
 The work to split the problem into sub-problems and recombine the results is 𝒇(𝒏).

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Master Theorem – Example 1

𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)


 There are three cases:
1. 𝑐𝑎𝑠𝑒 1: 𝑖𝑓 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝑶 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≤ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎
2. 𝑐𝑎𝑠𝑒 2: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝜽 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 = 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑙𝑔𝑛
3. 𝑐𝑎𝑠𝑒 3: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 Ω 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≥ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑓(𝑛)

 Example 1: 𝑇(𝑛) = 2𝑇(𝑛/2) + θ(𝑛) Merge sort


 Here 𝑎 = 2, 𝑏 = 2. So, 𝑛𝑙𝑜𝑔𝑏 𝑎 = 𝑛
 Also, 𝑓(𝑛) = 𝜃(𝑛) = c𝑛
 Case 2 applies: 𝑻 𝒏 = 𝜽 𝒏 𝒍𝒈𝒏

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Master Theorem – Example 2

𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)


 There are three cases:
1. 𝑐𝑎𝑠𝑒 1: 𝑖𝑓 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝑶 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≤ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎
2. 𝑐𝑎𝑠𝑒 2: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝜽 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 = 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑙𝑔𝑛
3. 𝑐𝑎𝑠𝑒 3: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 Ω 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≥ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑓(𝑛)

 Example 2: 𝑇(𝑛) = 𝑇(𝑛/2) + θ(1) Binary Search


 Here 𝑎 = 1, 𝑏 = 2. So, 𝑛𝑙𝑜𝑔𝑏 𝑎 = 𝑛log2 1 = 𝑛0 = 1
𝑓 𝑛 =θ 1 =1
 Case 2 applies: the solution is 𝜽(𝒏𝒍𝒐𝒈𝒃 𝒂 𝒍𝒐𝒈𝒏)
 𝑻 𝒏 = 𝜽(𝒍𝒐𝒈𝒏)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Master Theorem – Example 3

𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)


 There are three cases:
1. 𝑐𝑎𝑠𝑒 1: 𝑖𝑓 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝑶 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≤ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎
2. 𝑐𝑎𝑠𝑒 2: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 𝜽 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 = 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑙𝑔𝑛
3. 𝑐𝑎𝑠𝑒 3: 𝑓 𝑛 𝑖𝑠 𝑖𝑛 Ω 𝑛𝑙𝑜𝑔𝑏 𝑎 𝑓 𝑛 ≥ 𝑛log𝑏 𝑎 𝑡ℎ𝑒𝑛 𝑇 𝑛 = 𝜃 𝑓(𝑛)

 Example 3: 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛


 Here 𝑎 = 4, 𝑏 = 2. So, 𝑙𝑜𝑔𝑏𝑎 = 2 and 𝑛logba = 𝑛2
 𝑓 𝑛 = 𝑛,
 So, 𝑓(𝑛) ≤ 𝑛2 ⇒ 𝑓(𝑛) is in 𝑂(𝑛logba )
 Case 1 applies: 𝑻(𝒏) = 𝜽(𝒏𝟐)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Master Theorem Exercises


 Example 4: 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛2
 Example 5: 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛3
 Example 6: 𝑇(𝑛) = 9𝑇(𝑛/3) + 𝑛 (Summer 17, Summer 19)
 Example 7: 𝑇(𝑛) = 𝑇(2𝑛/3) + 1 (Summer 17)
 Example 8: 𝑇 𝑛 = 7𝑇 𝑛Τ2 + 𝑛3 (Winter 18)
 Example 9: 𝑇(𝑛) = 27𝑇(𝑛2 ) + 16𝑛 (Winter 19)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Recurrence Tree Method


 In recurrence tree, each node represents the cost of a single sub-problem in the set of
recursive function invocations.
 We sum the costs within each level of the tree to obtain a set of per level costs.
 Then we sum the all the per level costs to determine the total cost of all levels of the recursion.
 Here while solving recurrences, we divide the problem into sub-problems of equal size.
 E.g., 𝑇(𝑛) = 𝑎 𝑇(𝑛/𝑏) + 𝑓(𝑛) where 𝑎 > 1 , 𝑏 > 1 and 𝑓(𝑛) is a given function.
 𝐹(𝑛) is the cost of splitting or combining the sub problems.

𝒏Τ𝒃 𝒏Τ𝒃

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Example 1: 𝑻(𝒏) = 𝟐𝑻(𝒏/𝟐) + 𝒏
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.
𝑛
 The bottom level has 2log 𝑛 nodes, each
contributing the cost 𝑇(1).
𝑛Τ2 𝑛Τ2 𝒏
𝒍𝒐𝒈𝟐 𝒏  We have 𝑛 + 𝑛 + 𝑛 +
…… log 𝑛𝑡𝑖𝑚𝑒𝑠
𝒍𝒐𝒈𝟐 𝒏−𝟏
𝑛Τ22 𝑛Τ22 𝑛Τ22 𝑛Τ22 𝒏
𝑻(𝒏) = ෍ 𝒏 + 𝟐𝒍𝒐𝒈 𝒏 𝑻(𝟏)
𝒊=𝟎

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua
Example 3: 𝑻(𝒏) = 𝟐𝑻(𝒏/𝟐) + 𝒄. 𝒏𝟐
Recurrence Tree Method
The recursion tree for this recurrence is  Sub-problem size at level 𝑖 is 𝑛Τ2𝑖
2
 Cost of problem at level 𝑖 Is 𝑛
Τ2𝑖
 Total cost,
𝒍𝒐𝒈𝟐 𝒏−𝟏 𝒊
𝟏
𝑛2 𝑻 𝒏 ≤ 𝒏𝟐 ෍
𝟐
𝒊=𝟎


𝑛 Τ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

Recurrence Tree Method - Exercises


 Example 1: 𝑇(𝑛) = 𝑇(𝑛/4) + 𝑇(3𝑛/4) + 𝑐. 𝑛
 Example 2: 𝑇(𝑛) = 3𝑇(𝑛/4) + 𝑐. 𝑛2
 Example 3: 𝑇(𝑛) = 𝑇(𝑛/4) + 𝑇(𝑛/2) + 𝑛2
 Example 4: 𝑇(𝑛) = 𝑇(𝑛/3) + 𝑇(2𝑛/3) + 𝑛

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

D&C Running Time Analysis


 The running-time analysis of such divide-and-conquer (D&C) algorithms is almost automatic.
 Let 𝑔(𝑛) be the time required by D&C on instances of size 𝑛.
 The total time 𝒕(𝒏)taken by this divide-and-conquer algorithm is given by recurrence equation,
𝑡 𝑛 = 𝑙𝑡 𝑛/𝑏 + g 𝑛 𝐓(𝐧) = 𝐚𝐓(𝐧/𝐛) + 𝐟(𝐧)

 The solution of equation is given as,


𝜃 𝑛𝑘 𝑖𝑓 𝑙 < 𝑏 𝑘
𝑡 𝑛 = 𝜃 𝑛𝑘 𝑙𝑜𝑔𝑛 𝑖𝑓 𝑙 = 𝑏 𝑘
𝜃 𝑛𝑙𝑜𝑔𝑏 𝑙 𝑖𝑓 𝑙 > 𝑏 𝑘
where 𝑘 is the power of 𝑛 in 𝑔(𝑛)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search Example

Input: sorted array of integer values. 𝒙 = 𝟕

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

Find approximate midpoint


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search Example


Step 2:
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

Search for the target in the area before midpoint.


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search Example


Step 4:
1 2 3 4 5 6 7 8 9 𝒙=𝟕
1 3 7 9 11 32 52 74 90

Find approximate midpoint


Step 5:
1 2 3 4 5 6 7 8 9

1 3 7 9 11 32 52 74 90

𝟕>value of midpoint? YES.


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search Example


Step 6:
1 2 3 4 5 6 7 8 9 𝒙=𝟕
1 3 7 9 11 32 52 74 90

Search for the 𝒙in the area after midpoint.

Step 7:
1 2 3 4 5 6 7 8 9

1 3 7 9 11 32 52 74 90

Find approximate midpoint.


Is 𝒙 = midpoint value? YES.
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search – Iterative Algorithm


Algorithm: Function biniter(T[1,…,n], x)
𝑛=7 𝑥𝑥==33
7
if x > T[n] then return n+1
i ← 1; i 3
j ← n; 6
while i < j do
7
k ← (i + j ) ÷ 2
k 11
if x ≤ T [k] then j ← k
else i ← k + 1 32
return i 33
j 53

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search – Recursive Algorithm


Algorithm: Function binsearch(T[1,…,n], x)
if n = 0 or x > T[n] then return n + 1
else return binrec(T[1,…,n], x)
Function binrec(T[i,…,j], x)
if i = j then return i
k ← (i + j) ÷ 2
if x ≤ T[k] then
return binrec(T[i,…,k],x)
else return binrec(T[k + 1,…,j], x)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search - Analysis


 Let 𝑡(𝑛) be the time required for a call on binrec( 𝑇[𝑖, … , 𝑗], 𝑥 ), where 𝑛 = 𝑗 – 𝑖 + 1 is the
number of elements still under consideration in the search.
 The recurrence equation is given as,
𝒕(𝒏) = 𝒕(𝒏/𝟐) + 𝜽(𝟏) 𝑻(𝒏) = 𝒂𝑻(𝒏/𝒃) + 𝒇(𝒏)
 Comparing this to the general template for divide and conquer algorithm, 𝑎 = 1, 𝑏 =
2 𝑎𝑛𝑑 𝑓 𝑛 = 𝜃 1 .
∴ 𝒕(𝒏) ∈ 𝜽(𝒍𝒐𝒈 𝒏)
 The complexity of binary search is 𝜽(𝒍𝒐𝒈 𝒏)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Binary Search – Examples


1. Demonstrate binary search algorithm and find the element 𝒙 = 𝟏𝟐 in the following array. [3 /
4]
2, 5, 8, 12, 16, 23, 38, 56, 72, 91

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.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Multiplying Large Integers – Introduction


 Multiplying two 𝑛 digit large integers using divide and conquer method.
 Example: Multiplication of 𝟗𝟖𝟏 by 𝟏𝟐𝟑𝟒.
1. Convert both the numbers into same length nos. and split each operand into two parts:

𝟎𝟗𝟖𝟏 𝟏𝟐𝟑𝟒

𝒘 = 𝟎𝟗 𝒙 = 𝟖𝟏 𝒚 = 𝟏𝟐 𝒛 = 𝟑𝟒
2. We can write as,

102𝑤 + 𝑥 𝟎𝟗𝟖𝟏 = 𝟏𝟎𝟐 𝒘 + 𝒙 𝟏𝟐𝟑𝟒 = 𝟏𝟎𝟐 𝒚 + 𝒛


= 102 09 + 81
= 900 + 81
=981

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Multiplying Large Integers – Example 1


 Now, the required product can be computed as,
0981 × 1234 = 102 𝑤 + 𝑥 × 102 𝑦 + 𝑧 𝑤 = 09
= 104 𝑤 ∙ 𝑦 + 102 𝑤 ∙ 𝑧 + 𝑥 ∙ 𝑦 + 𝑥 ∙ 𝑧 𝑥 = 81
= 1080000 + 127800 + 2754 𝑦 = 12
= 1210554 𝑧 = 34

 The above procedure still needs four half-size multiplications:


𝑖 𝑤 ∙ 𝑦 𝑖𝑖)𝑤 ∙ 𝑧 𝑖𝑖𝑖 𝑥 ∙ 𝑦 (𝑖𝑣 𝑥 ∙ 𝑧
 The computation of (𝑤 ∙ 𝑧 + 𝑥 ∙ 𝑦)can be done as,
𝒓= 𝒘+𝒙 × 𝒚+𝒛 =𝒘∙𝒚+ 𝒘∙𝒛+𝒙∙𝒚 +𝒙∙𝒛

 Only one multiplication is required instead of two.


Additional terms

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Multiplying Large Integers – Example 1


𝟏𝟎𝟒 𝒘 ∙ 𝒚 + 𝟏𝟎𝟐 𝒘 ∙ 𝒛 + 𝒙 ∙ 𝒚 + 𝒙 ∙ 𝒛 𝑤 = 09
𝑥 = 81
 Now we can compute the required product as follows: 𝑦 = 12
𝑧 = 34
𝒑 = 𝒘 ∙ 𝒚 = 𝟎𝟗 ∙ 𝟏𝟐 = 𝟏𝟎𝟖
𝒒 = 𝒙 ∙ 𝒛 = 𝟖𝟏 ∙ 𝟑𝟒 = 𝟐𝟕𝟓𝟒
𝒓 = 𝒘 + 𝒙 × 𝒚 + 𝒛 = 𝟗𝟎 ∙ 𝟒𝟔 = 𝟒𝟏𝟒𝟎

𝒓 = 𝒘+𝒙 × 𝒚+𝒛 = 𝒘∙𝒚+ 𝒘∙𝒛+𝒙∙𝒚 +𝒙∙𝒛

981 × 1234 = 104𝑝 + 102 (𝑟 − 𝑝 − 𝑞) + 𝑞


= 1080000 + 127800 + 2754
= 1210554.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Multiplying Large Integers – Analysis


 981 × 1234 can be reduced to three multiplications of two-figure numbers (09∙12, 81∙34 𝑎𝑛𝑑
90∙46) together with a certain number of shifts, additions and subtractions.
 Reducing four multiplications to three will enable us to cut 25% of the computing time required
for large multiplications.
 We obtain an algorithm that can multiply two 𝑛-figure numbers in a time,
𝑻(𝒏)= 𝟑𝒕 (𝒏/𝟐) + 𝒈(𝒏), 𝑻(𝒏) = 𝒂𝑻(𝒏/𝒃) + 𝒇(𝒏)
 Solving it gives,
𝑻 𝒏 ∈ 𝜽 𝒏𝒍𝒈𝟑 | 𝒏 𝒊𝒔 𝒂 𝒑𝒐𝒘𝒆𝒓 𝒐𝒇 𝟐

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Multiplying Large Integers – Example 2


 Example: Multiply 𝟖𝟏𝟏𝟒 with 𝟕𝟔𝟐𝟐 using divide & conquer method.
 Solution using D&C

Step 1: 𝒘 = 𝟖𝟏 𝒙 = 𝟏𝟒 𝒚 = 𝟕𝟔 𝒛 = 𝟐𝟐

Step 2: Calculate 𝑝, 𝑞 and 𝑟

𝑝 = 𝑤 ∙ 𝑦 = 81 ∙ 76 = 6156
𝑞 = 𝑥 ∙ 𝑧 = 14 ∙ 22 = 308
𝑟 = (𝑤 + 𝑥) ∙ (𝑦 + 𝑧) = 95 ∙ 98 = 9310
8114 × 7622 = 𝟏𝟎𝟒𝒑 + 𝟏𝟎𝟐 (𝒓 − 𝒑 − 𝒒) + 𝒒
= 61560000 + 284600 + 308
= 61844908

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Merge Sort – Example

Unsorted Array
724 521 2 98 529 31 189 451
1 2 3 4 5 6 7 8

Step 1: Split the selected array


1 2 3 4 5 6 7 8
724 521 2 98 529 31 189 451

724 521 2 98 529 31 189 451


1 2 3 4 1 2 3 4

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Merge Sort – Example


Select the left subarray and Split Select the right subarray and Split
1 2 3 4 1 2 3 4
724 521 2 98 529 31 189 451

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

521 724 2 98 31 529 189 451


Merge
2 98 521 724 31 189 451 529

2 31 98 189 451 521 529 724


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Merge Sort – Algorithm


Procedure: mergesort(T[1,…,n]) Procedure:
merge(U[1,…,m+1],V[1,…,n+1],T[1,…,m+n])
if n is sufficiently small then
insert(T) i ← 1;
else j ← 1;
array U[1,…,1+n/2],V[1,…,1+n/2] U[m+1], V[n+1] ← ∞;
U[1,…,n/2] ← T[1,…,n/2] for k ← 1 to m + n do
V[1,…,n/2] ← T[n/2+1,…,n] if U[i] < V[j]
mergesort(U[1,…,n/2]) then T[k] ← U[i];
mergesort(V[1,…,n/2]) i ← i + 1;
merge(U, V, T) else T[k] ← V[j];
j ← j + 1;

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Merge Sort - Analysis


 Let 𝑻(𝒏) be the time taken by this algorithm to sort an array of 𝑛 elements.
 Separating 𝑇 into 𝑈 & 𝑉 takes linear time; 𝑚𝑒𝑟𝑔𝑒(𝑈, 𝑉, 𝑇) also takes linear time.

𝑇(𝑛) = 𝑇(𝑛/2) + 𝑇(𝑛/2) + 𝑔(𝑛) where 𝑔(𝑛) ∈ θ(𝑛).


𝑇(𝑛) = 2𝑡(𝑛/2) + θ(𝑛) 𝒕 𝒏 = 𝒍𝒕 𝒏/𝒃 + 𝐠 𝒏
 Applying the general case, 𝑙 = 2, 𝑏 = 2, 𝑘 = 1

 Since 𝑙 = 𝑏𝑘 the second case applies so, 𝑡(𝑛) ∈ θ(𝑛𝑙𝑜𝑔𝑛).


 Time complexity of merge sort is 𝛉(𝒏𝒍𝒐𝒈𝒏). 𝜽 𝒏𝒌 𝒊𝒇 𝒍 < 𝒃𝒌
𝒕 𝒏 = 𝜽 𝒏𝒌 𝒍𝒐𝒈𝒏 𝒊𝒇 𝒍 = 𝒃𝒌
𝜽 𝒏𝒍𝒐𝒈𝒃 𝒍 𝒊𝒇 𝒍 > 𝒃𝒌

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

 To multiply 2 × 2 matrices, total 8 (23) scalar multiplications are required.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Matrix Multiplication
 In general, 𝐴 and 𝐵 are two 2 × 2 matrices to be multiplied.
𝐴11 𝐴12 𝐵11 𝐵12
𝐴= and 𝐵 =
𝐴21 𝐴21 𝐵21 𝐵22

𝐶11 𝐶12 𝐴11 𝐴12 𝐵11 𝐵12


𝐶= = ∙
𝐶21 𝐶22 𝐴21 𝐴22 𝐵21 𝐵22

𝐶11 = 𝐴11 ∙ 𝐵11 + 𝐴12 ∙ 𝐵21


𝐶12 = 𝐴11 ∙ 𝐵12 + 𝐴12 ∙ 𝐵22
𝐶21 = 𝐴21 ∙ 𝐵11 + 𝐴22 ∙ 𝐵21
𝐶22 = 𝐴21 ∙ 𝐵12 + 𝐴22 ∙ 𝐵22
 Computing each entry in the product takes 𝒏 multiplications and there are 𝒏𝟐entries for a total
of 𝑶(𝒏𝟑 ).

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Strassen’s Algorithm for Matrix Multiplication


 Consider the problem of multiplying two 𝑛 × 𝑛 matrices.
 Strassen’s devised a better method which has the same basic method as the multiplication of
long integers.
 The main idea is to save one multiplication on a small problem and then use recursion.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Strassen’s Algorithm for Matrix Multiplication


𝐴11 𝐴12 𝐵 𝐵12
𝐴= and 𝐵 = 11
Step 1 Step 2 Step 3 𝐴21 𝐴21 𝐵21 𝐵22

𝑆1 = 𝐵12 − 𝐵22 𝑃1 = 𝐴11 ∙ 𝑆1


Final Answer:
𝑆2 = 𝐴11 + 𝐴12 𝑃2 = 𝑆2 ∙ 𝐵22
𝐶11 𝐶12
𝑃3 = 𝑆3 ∙ 𝐵11 𝐶=
𝑆3 = 𝐴21 + 𝐴22 𝐶21 𝐶22
𝑆4 = 𝐵21 − 𝐵11 𝑃4 = 𝐴22 ∙ 𝑆4 Where,
𝑆5 = 𝐴11 + 𝐴22 𝑃5 = 𝑆5 ∙ 𝑆6 𝐶11 = 𝑃5 + 𝑃4 − 𝑃2 + 𝑃6
𝑆6 = 𝐵11 + 𝐵22 𝑃6 = 𝑆7 ∙ 𝑆8 𝐶12 = 𝑃1 + 𝑃2
𝑆7 = 𝐴12 − 𝐴22 𝑃7 = 𝑆9 ∙ 𝑆10 𝐶21 = 𝑃3 + 𝑃4
𝑆8 = 𝐵21 + 𝐵22 All above 𝐶22 = 𝑃5 + 𝑃1 − 𝑃3 − 𝑃7
𝑆9 = 𝐴11 − 𝐴21 operations No multiplication is
involve only one required here.
𝑆10 = 𝐵11 + 𝐵12 multiplication.
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Strassen’s Algorithm - Analysis


 It is therefore possible to multiply two 2 × 2 matrices using only seven scalar multiplications.
 Let 𝑡(𝑛) be the time needed to multiply two 𝑛 × 𝑛 matrices by recursive use of equations.
𝒕(𝒏) = 𝟕𝒕(𝒏/𝟐) + 𝒈(𝒏) 𝒕 𝒏 = 𝒍𝒕 𝒏/𝒃 + 𝐠 𝒏
Where 𝑔(𝑛) ∈ 𝑂(𝑛2).

 The general equation applies with 𝑙 = 7, 𝑏 = 2 and 𝑘 = 2.

 Since 𝑙 > 𝑏 𝑘 , the third case applies and 𝑡 𝑛 ∈ 𝑂 𝑛𝑙𝑔7 .


 Since 𝑙𝑔7 > 2.81, it is possible to multiply two 𝑛 × 𝑛 matrices in a time𝑶(𝒏𝟐.𝟖𝟏 ).

𝜽 𝒏𝒌 𝒊𝒇 𝒍 < 𝒃𝒌
𝒕 𝒏 = 𝜽 𝒏𝒌 𝒍𝒐𝒈𝒏 𝒊𝒇 𝒍 = 𝒃𝒌
𝜽 𝒏𝒍𝒐𝒈𝒃 𝒍 𝒊𝒇 𝒍 > 𝒃𝒌
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua
www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Example


Procedure pivot(T[i,…,j]; var l) 0 1 2 3 4 5 6 7 8 9
p ← T[i] 42 23 74 11 65 58 94 36 99 87
k ← i; l ← j+1 k l
Repeat
k ← k+1 until T[k] > p or k ≥ j Swap
Repeat 42 23 36
74 11 65 58 94 74
36 99 87
l ← l-1 until T[l] ≤ p k l
While k < l do
Swap
Swap T[k] and T[l]
Repeat k ← k+1 until 11
42 23 36 42
11 65 58 94 74 99 87
T[k] > p k l
Repeat l ← l-1 until
T[l] ≤ p LB = 0, UB = 9
Swap T[i] and T[l] p = 42
k = 0, l = 10

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Example


Procedure pivot(T[i,…,j]; var l) 0 1 2 3 4 5 6 7 8 9
p ← T[i] 11 23 36 42 65 58 94 74 99 87
k ← i; l ← j+1 LB UB
Repeat
k ← k+1 until T[k] > p or k ≥ j 11 23 36
Repeat
k l
l ← l-1 until T[l] ≤ p
While k < l do LB UB
Swap T[k] and T[l] 11 23 36 42 65 58 94 74 99 87
Repeat k ← k+1 until
T[k] > p
Repeat l ← l-1 until
23 36
T[l] ≤ p k l
Swap T[i] and T[l]
11 23 36 42 65 58 94 74 99 87

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Example


LB UB
Procedure pivot(T[i,…,j]; var l) 0 1 2 3 4 5 6 7 8 9
p ← T[i] 11 23 36 42 65 58 94 74 99 87
k ← i; l ← j+1
Swap
Repeat
k ← k+1 until T[k] > p or k ≥ j 65 65
58 58 94 74 99 87
Repeat
k l
l ← l-1 until T[l] ≤ p
While k < l do
Swap T[k] and T[l] 58 65 94 74 99 87
Repeat k ← k+1 until
T[k] > p
Repeat l ← l-1 until LB UB
T[l] ≤ p 11 23 36 42 58 65 94 74 99 87
Swap T[i] and T[l]

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Example


LB UB
Procedure pivot(T[i,…,j]; var l) Swap

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]

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Algorithm


Procedure: quicksort(T[i,…,j]) Procedure: pivot(T[i,…,j]; var l)
{Sorts subarray T[i,…,j] into p ← T[i]
ascending order}
k ← i
if j – i is sufficiently small
then insert (T[i,…,j]) l ← j + 1
else repeat k ← k+1 until T[k] > p or k ≥ j
pivot(T[i,…,j],l) repeat l ← l-1 until T[l] ≤ p
quicksort(T[i,…,l - 1]) while k < l do
quicksort(T[l+1,…,j] Swap T[k] and T[l]
Repeat k ← k+1 until T[k] > p
Repeat l ← l-1 until T[l] ≤ p
Swap T[i] and T[l]

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort Algorithm – Analysis


1. Worst Case
 Running time depends on which element is chosen as key or pivot element.
 The worst case behavior for quick sort occurs when the array is partitioned into one sub-array with 𝒏 − 𝟏
elements and the other with 𝟎 element.
 In this case, the recurrence will be,
𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝑇(0) + 𝜃(𝑛)
𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝜃(𝑛)
𝑻(𝒏) = 𝜽(𝒏𝟐)
2. Best Case
 Occurs when partition produces sub-problems each of size n/2.
 Recurrence equation:
𝑇(𝑛) = 2𝑇(𝑛/2) + θ(𝑛)
𝑙 = 2, 𝑏 = 2, 𝑘 = 1, 𝑠𝑜 𝑙 = 𝑏𝑘
𝑻(𝒏) = 𝜽(𝒏𝒍𝒐𝒈𝒏)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort Algorithm – Analysis


3. Average Case
 Average case running time is much closer to the best case.
 If suppose the partitioning algorithm produces a 9:1 proportional split the recurrence will be,
𝑇(𝑛) = 𝑇(9𝑛/10) + 𝑇(𝑛/10) + θ(𝑛)
𝑻(𝒏) = 𝜽(𝒏𝒍𝒐𝒈𝒏)

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

Quick Sort - Examples


 Sort the following array in ascending order using quick sort algorithm.
1. 5, 3, 8, 9, 1, 7, 0, 2, 6, 4
2. 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9
3. 9, 7, 5, 11, 12, 2, 14, 3, 10, 6

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

 This algorithm takes a time in 𝜽(𝒏)since the instruction 𝒓 = 𝒂 ∗ 𝒓 is executed exactly 𝒏 − 𝟏


times, provided the multiplications are counted as elementary operations.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

𝑀(𝑚, 𝑖𝑚 − 𝑖 + 1) and 𝑀(𝑚, 𝑖𝑚)

𝑎 = 5 𝑠𝑜 𝑚 = 1 and 𝑛 = 25 and suppose 𝑖 = 10


The body of loop executes 10𝑡ℎ time as,
𝒓=𝒂 ∗ 𝒓
here 9 times multiplication is already done so 𝒓 = 𝟓𝟗 = 1953125
The size of 𝑟 in the 10th iteration will be between 𝑖𝑚 − 𝑖 + 1 𝑡𝑜 𝑖𝑚, i.e.,
between 𝟏 𝒕𝒐 𝟏𝟎 10-10+1 10

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

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

 If we use the divide-and-conquer multiplication algorithm,


𝑻(𝒎, 𝒏) ∈ 𝜽(𝒎𝒍𝒈𝟑 𝒏𝟐)

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
 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 𝜽 𝒎𝟐 𝒏𝟐 𝜽 𝒎𝒍𝒈𝟑 𝒏𝒍𝒈𝟑

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua


www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntua

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntua

You might also like