Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

COL106: Data Structures and Algorithms: Ragesh Jaiswal, IIT Delhi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

COL106: Data Structures and Algorithms

Ragesh Jaiswal, IIT Delhi

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction

How do Data Structures play a part in making computational


tasks efficient?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary Search Recursive Functions Divide and Conquer

Problem
Multiplying two n-bit numbers: Given two n-bit numbers, A and B,
Design an algorithm to output A B.
Algorithm
Karatsuba(A, B)
- If (|A| = |B| = 1) return(A B)
- Split A into AL and AR
- Split B into BL and BR
- P Karatsuba(AL , BL )
- Q Karatsuba(AR , BR )
- R Karatsuba(AL + AR , BL + BR )
- return(Combine(P, Q, R))
Recurrence relation: T (n) 3 T (n/2) + cn; T (1) c.
What is the solution of this recurrence relation?
T (n) O(nlog2 3 )
Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Problem
Given an array of unsorted integers, output a sorted array.
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
How do we argue correctness?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
How do we argue correctness?
Proof of correctness of Divide and Conquer algorithms are usually
by induction.
Base case: This corresponds to the base cases of the algorithm.
For the MergeSort, the base case is that the algorithm correctly
sorts arrays of size 1.
Inductive step: In general, this corresponds to correctly combining
the solutions of smaller subproblems. For MergeSort, this is just
proving that the Merge routine works correctly. This may again be
done using induction and is left as an exercise.
Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
What is the running time of MergeSort?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Recurrence relation for running time: T (n) 2 T (n/2) + cn for
all n 2 and T (1) c for some constant c.
Obtain the solution to the above recurrence relation by unrolling
the recursion.

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary search Recursive functions Merge Sort

Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Recurrence relation for running time: T (n) 2 T (n/2) + cn for
all n 2 and T (1) c for some constant c.
Obtain the solution to the above recurrence relation by unrolling
the recursion. T (n) = O(n log n)

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Introduction
Digression: Binary Search Recursive Functions Solving Recurrences

Consider the recurrence relation for the running time of the


MergeSort algorithm:
T (n) 2 T (n/2) + cn for all n 2 ; T (2) c
Another way to solve the recurrence relation is substitution:
1
2

Guess the bound on T (n), and


Show that this bound holds using induction.

Let our guess be T (n) cn log n for all n 2. We will now prove
this by induction
Base case: T (n) cn log n when n = 2 since we are given that
T (2) c.
Inductive step: Suppose the bound holds for n = 2, ..., k 1, we
will show that the bound also holds for n = k.
We know T (k) 2T (k/2) + ck.
So, using induction hypothesis, we get:
T (k) 2c(k/2) log(k/2) + ck = ck log k.
Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Introduction

How do Data Structures play a part in making computational


tasks efficient?
We saw an example where organising and accessing data plays
an important role in determining the efficiency of the
computational task.

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Introduction

How do Data Structures play a part in making computational


tasks efficient?
We saw an example where organising and accessing data plays
an important role in determining the efficiency of the
computational task.
In certain computational tasks of limited nature where a
specific way of organizing and accessing data makes sense.
The nature of the computational task itself guides the kind of
data structure that is most appropriate.

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

How do Data Structures play a part in making computational


tasks efficient?
We saw an example where organising and accessing data plays
an important role in determining the efficiency of the
computational task.
In certain computational tasks of limited nature where a
specific way of organizing and accessing data makes sense.
The nature of the computational task itself guides the kind of
data structure that is most appropriate.

Suppose you have to automate the queuing service at the


local Doctors office.
The main requirement for such a service is First In First Out
(FIFO in short).
As people come to the Doctors office, they enter their names
into the computer. The Doctor asks the computer to return
the name of the next person as per the order in which they
turned up.
Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Problem
Automate the queueing service at a Doctors office. As people
come to the Doctors office, they enter their names into the
computer. The Doctor asks the computer to return the name of
the next person as per the order in which they turned up.
The data structure that you design needs to support only the
following two operations:
1
2

Enqueue(Name): Insert the name of the person.


Dequeue(): Remove and return the name of the person who
came first and has not been served yet.

Such an Abstract Data Type (ADT in short) is called a


Queue.
Abstract Data Type: A Mathematical model of a data
structure that specifies the type of data stored, the operations
supported on them, and the types of parameters of the
operations.
Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Problem
Automate the queueing service at a Doctors office. As people
come to the Doctors office, they enter their names into the
computer. The Doctor asks the computer to return the name of
the next person as per the order in which they turned up.
The data structure that you design needs to support only the
following two operations:
1
2

Enqueue(e): Add element e to the back of the queue.


Dequeue(): Remove and return the first element of the queue
(or null if the queue is empty).

Such an Abstract Data Type (ADT in short) is called a Queue.


Question: Can you implement a queue using an array?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Consider a sequence of function calls in some programming


language:
f (a){g (a); ...}
g (a){h(a); ...}
h(a){...}

What happens when we make a call f (10):


f makes a call to g : We need some data structure to save the
state of the function f so that when the call to g returns, we
should be able to execute the remaining instructions of f .
g makes a call to h: Similarly, we need to save the state of g
to be able to run remaining instructions of g

We need a data structure where we can store the state of a


function and extract these saved states in a Last In First
Out (LIFO) order.

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Consider a sequence of function calls in some programming language:


f (a){g (a); ...}
g (a){h(a); ...}
h(a){...}

What happens when we make a call f (10):


f makes a call to g : We need some data structure to save the state of
the function f so that when the call to g returns, we should be able to
execute the remaining instructions of f .
g makes a call to h: Similarly, we need to save the state of g to be
able to run remaining instructions of g

We need a data structure where we can store the state of a function


and extract these saved states in a Last In First Out (LIFO) order.
Such a Data Structure needs to support the following two main
operations:
1
2

Push(e): Add element e


Pop(): Removes and returns the in LIFO order (or null if the stack is
empty).

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

We need a data structure where we can store the state of a


function and extract these saved states in a Last In First
Out (LIFO) order.
Such a Data Structure needs to support the following two
main operations:
1
2

Push(e): Add element e


Pop(): Removes and returns the in LIFO order (or null if the
stack is empty).

Such an abstract data type is called a Stack.

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

We need a data structure where we can store the state of a


function and extract these saved states in a Last In First
Out (LIFO) order.
Such a Data Structure needs to support the following two
main operations:
1
2

Push(e): Add element e


Pop(): Removes and returns the in LIFO order (or null if the
stack is empty).

Such an abstract data type is called a Stack.


Can you implement a stack using an array?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

We need a data structure where we can store the state of a


function and extract these saved states in a Last In First
Out (LIFO) order.
Such a Data Structure needs to support the following two
main operations:
1
2

Push(e): Add element e


Pop(): Removes and returns the in LIFO order (or null if the
stack is empty).

Such an abstract data type is called a Stack.


Can you implement a stack using an array? What is the
running time for each operation?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Can you implement a stack using an array? What is the


running time for each operation?
Can you implement a stack using a queue? What is the
running time for each operation?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Can you implement a stack using an array? What is the


running time for each operation?
Can you implement a stack using a queue? What is the
running time for each operation?
Can you implement a queue using a stack?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Can you implement a stack using an array? What is the


running time for each operation?
Can you implement a stack using a queue? What is the
running time for each operation?
Can you implement a queue using a stack?
Can you implement a queue using two stacks?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

Data Structures
Queue and Stack

Can you implement a stack using an array? What is the


running time for each operation?
Can you implement a stack using a queue? What is the
running time for each operation?
Can you implement a queue using a stack?
Can you implement a queue using two stacks? What is the
running time for each operation?

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

End

Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms

You might also like