You are on page 1of 29

Session 2

• Recursion
• Elementary Data Structures

BITS Pilani, Pilani Campus


Analyzing Recursive Algorithms

We define a procedure P that is allowed to make calls to


itself as a subroutine, provided those calls to P are for
solving sub-problems of smaller size.
The subroutine calls to P on smaller instances are called
"recursive calls.“
A recursive procedure should always define a base case,
which is small enough that the algorithm solve it directly
without using recursion.

2 Elementary Data Structures

BITS Pilani, Pilani Campus


Analyzing Recursive Algorithms

• Analyzing Recursive Algorithms takes a bit of additional


work.
• We use a recurrence equation, which defines
mathematical statements that the running time of a
recursive algorithm must satisfy.
• Let a function T(n) denote the running time of the
algorithm on an input of size n.
• We must write equations that T(n) must satisfy.

Elementary Data Structures 3


BITS Pilani, Pilani Campus
Example

•For example, we can characterize the running time,


T (n), of the recursiveMax algorithm as follows:

4 Elementary Data Structures

BITS Pilani, Pilani Campus


Recurrence equation

T(n ) = 3 if n = 1
T(n - 1) + 7 otherwise,

assuming that we count each comparison, array


reference, recursive call, max calculation, or
return as a single primitive operation.

5 Elementary Data Structures

BITS Pilani, Pilani Campus


Recursion

• Design recursive methods so that it is guaranteed to


terminate at some point.
• Make recursive calls for “smaller” instances of the
problem and handling the “smallest” instance non-
recursively.
• Infinitely recursive functions will use up all the memory
available for the method stack and generate an out-of-
memory error.

BITS Pilani, Pilani Campus


Types of Data structures

1. Linear Data structures – Stacks, Queues, Lists.


Used in sorting, searching
2. Non-linear Data structures- Trees, graphs
Hierarchy in an organization- two employees
reporting to the same manager

Elementary Data Structures


7

BITS Pilani, Pilani Campus


Absract Data type

• ADT provides a specification of the instances as


well as the operations that are to be performed.

• ADT specification is independent of any


representation and programming language we
have in mind.

Elementary Data Structures


8
BITS Pilani, Pilani Campus
The Stack ADT

• The Stack ADT stores arbitrary objects


• Insertions and deletions follow the last-in first-out scheme
• Think of a spring-loaded plate dispenser
• Main stack operations:
– push(object): inserts an element
– object pop(): removes and returns the last inserted element

• Auxiliary stack operations:


– object top(): returns the last inserted element without removing it
– integer size(): returns the number of elements stored
– boolean isEmpty(): indicates whether no elements are stored

BITS Pilani, Pilani Campus


Applications of Stacks

• Page-visited history in a Web browser


• Undo sequence in a text editor
• Chain of method calls in the Java Virtual
Machine or C++ runtime environment
• Auxiliary data structure for algorithms
• Implementation of recursion
• Multiprogramming

10 Elementary Data Structures

BITS Pilani, Pilani Campus


Array-based Stack
• A simple way of implementing the Stack ADT uses an
array
• We add elements from left to right
• A variable t keeps track of the index of the top element
(size is t+1)
Algorithm push(o)
if Size()=N then
indicate FullStackException
else
t t + 1
S[t]  o
11
BITS Pilani, Pilani Campus
Array-based Stack


S
0 1 2 t
Algorithm pop():
if isEmpty() then
throw EmptyStackException
else
e  S[t]
s[t]  null
tt1
return e

BITS Pilani, Pilani Campus


Uses of Stacks

• For execution of Procedure calls


• For execution of recursive functions
• Infix to prefix and postfix conversion

13
BITS Pilani, Pilani Campus
An example of a method stack

Elementary Data Structures 14


BITS Pilani, Pilani Campus
The Queue ADT

• The Queue ADT stores arbitrary objects


• Insertions and deletions follow the
FIRST-IN FIRST-OUT SCHEME
• Insertions are at the rear of the queue and removals are at the
front of the queue
• Main queue operations:
– enqueue(object): inserts an element at the end of the queue
– object dequeue(): removes and returns the element at the front of the
queue

Elementary Data Structures 15


BITS Pilani, Pilani Campus
The Queue ADT

Auxiliary queue operations:


– object front(): returns the element at the front
without removing it
– integer size(): returns the number of elements
stored
– boolean isEmpty(): indicates whether no
elements are stored
Exceptions
– Attempting the execution of dequeue or front
on an empty queue throws an
EmptyQueueException
Elementary Data Structures 16
BITS Pilani, Pilani Campus
A Simple Array-Based Implementation

• An array, Q, with capacity N, for storing its elements.


• Since the main rule with the queue ADT is that we insert
and delete objects according to the FIFO principle, we
must decide how we are going to keep track of the front
and rear of the queue.
• To avoid moving objects once they are placed in Q, we
define two variables f and r, which have the following
meanings:
• f is an index to the cell of Q storing the first element of
the queue (which is the next candidate to be removed by
a dequeue operation), unless the queue is empty (in
which case f = r)
Elementary Data Structures 17
BITS Pilani, Pilani Campus
A Simple Array-Based Implementation

• R is an index to the next available array cell in Q.


• Initially, we assign f = r = 0, and we indicate that the
queue is empty by the condition f = r.
• Now, when we remove an element from the front of the
queue, we can simply increment f to index the next cell.
• when we add an element, we can simply increment r to
index the next available cell in Q.
• We have to be a little careful not to overflow the end of
the array, however.
• Consider, for example, what happens if we repeatedly
enqueue and dequeue a single element N different
times. We would have f = r = N
Elementary Data Structures 18
BITS Pilani, Pilani Campus
A Simple Array-Based Implementation

• If we were then to try to insert the element just one more


time, we would get an array-out-of-bounds error (since
the N valid locations in Q are from Q[O] to Q[N-1]), even
though there is plenty of room in the queue in this case.
• To avoid this problem and be able to utilize all of the
array Q, we let the f and r indices "wrap around" the end
of Q.
• That is, we now view Q as a "circular array" that goes
from Q[O] to Q[N -1] and then immediately back to Q[O]
again.

Elementary Data Structures 19


BITS Pilani, Pilani Campus
A Simple Array-Based Implementation

Elementary Data Structures 20


BITS Pilani, Pilani Campus
Implementing the circular view of Q

• Each time we increment for r, we simply compute


this increment as “(f + 1) mod N" or "(r + 1) mod N“

• The operator "mod" is the modulo operator, which


is computed by taking the remainder after an integral
division, so that, if y is nonzero, then,

Elementary Data Structures 21


BITS Pilani, Pilani Campus
Dequeue & enqueue

Elementary Data Structures 22


BITS Pilani, Pilani Campus
Comlexity of the Algorithm - Queues

• As with array-based stack implementation, each of the queue


methods in the array realization executes a constant number of
statements involving arithmetic operations, comparisons, and
assignments.

• Hence each method in this implementation runs in O(1) time.

• Disadvantage of the array-based queue implementation :


we artificially set the capacity of the queue to be some number
N. In a real application, we may actually need more or less
queue capacity than this, but if we have a good estimate of the
number of elements that will be in the queue at the same time,
then the array-based implementation is quite efficient.
Elementary Data Structures 23
BITS Pilani, Pilani Campus
Applications of Queues

– Waiting lines
– Access to shared resources (e.g., printer)
– Multiprogramming
– Protocol Implementation in networks etc.,

Elementary Data Structures 24


BITS Pilani, Pilani Campus
Using Queues for Multiprogramming

Multiprogramming is a way of achieving a limited


form of parallelism, even on a computer that has
only one CPU.
This mechanism allows us to have multiple tasks
or computational threads running at the same
time, with each thread being responsible for
some specific computation.
Example : Graphical Applications

Elementary Data Structures 25


BITS Pilani, Pilani Campus
Using Queues for Multiprogramming

Even if the computer has only one CPU, these different


computational threads can all seem to be running at the
same time because:
l . The CPU is so fast relative to our perception of time.
2. The operating system is providing each thread with a
different "slice" of the CPU's time. The time slices given
to each different thread occur with such rapid succession
that the different threads appear to be running
simultaneously, in parallel.

Elementary Data Structures 26


BITS Pilani, Pilani Campus
Example : Java Threads

• Java has a built-in mechanism for achieving


multiprogramming Java threads.
• Java threads are computational objects that can
cooperate and communicate with one another to share
other objects in memory, the computer's screen, or other
kinds of resources and devices.
• Switching between different threads in a Java program
occurs rapidly because each thread has its own Java
stack stored in the memory of the Java Virtual Machine.

Elementary Data Structures 27


BITS Pilani, Pilani Campus
Queues in OS

• The operating systems utilize a queue to allocate CPU time to the


runnable threads in the round-robin protocol.
• The main idea of the round-robin protocol is to store all runnable
threads in a queue Q.
• When the CPU is ready to provide a time slice to a thread, it
performs a dequeue operation on the queue Q to get the next
available runnable thread; let's call it T.
• Before the CPU actually begins executing instructions for T,
however, it starts a timer running in hardware set to expire a fixed
amount of time later.
• The CPU now waits until either (a) thread T blocks itself (by one of
the blocking methods mentioned above), or (b) the timer expires

Elementary Data Structures 28


BITS Pilani, Pilani Campus
Any Questions ?

BITS Pilani, Pilani Campus

You might also like