You are on page 1of 217

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
CS F211: Data Structures and Algorithms

Handout Discussion

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students

I Read the textbook:


Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Fourth Edition

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students

I Read the textbook:


Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Fourth Edition
I Solve the exercise problems.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students

I Read the textbook:


Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Fourth Edition
I Solve the exercise problems.
I Attend as many lectures as possible.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students

I Read the textbook:


Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Fourth Edition
I Solve the exercise problems.
I Attend as many lectures as possible.
I Attend all the labs.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students

I Read the textbook:


Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Fourth Edition
I Solve the exercise problems.
I Attend as many lectures as possible.
I Attend all the labs.
I Use PYQs only for solving additional problems.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
I An algorithm for linear search.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
I An algorithm for linear search.
Input : An array A of elements and a key k to be searched.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
I An algorithm for linear search.
Input : An array A of elements and a key k to be searched.
Output : The index of the key k in A, if found; otherwise, -1.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is an Algorithm?
It is a well-defined computational procedure which takes an
input and produces an output.
I An algorithm for linear search.
Input : An array A of elements and a key k to be searched.
Output : The index of the key k in A, if found; otherwise, -1.
1: procedure LinearSearch(A, k)
2: for i ← 1 to length of array A do
3: if A[i] = k then
4: return i . Key found at index i
5: return −1 . Key not found

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.


I Sorting Problem

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.


I Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.


I Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a10 , a20 , . . . , an0 > such that
a10 ≤ a20 ≤ · · · ≤ an0

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.


I Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a10 , a20 , . . . , an0 > such that
a10 ≤ a20 ≤ · · · ≤ an0
I E.g., Input sequence : < 31, 41, 59, 26, 41, 58 >

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm solves a computational problem.


I Sorting Problem
Input: A sequence of n numbers < a1 , a2 , . . . , an >
Output: A permutation < a10 , a20 , . . . , an0 > such that
a10 ≤ a20 ≤ · · · ≤ an0
I E.g., Input sequence : < 31, 41, 59, 26, 41, 58 >
I Sorting algorithm should give the output:
< 26, 31, 41, 41, 58, 59 >

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.
I All these computational problems require different algorithms.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.
I All these computational problems require different algorithms.
e.g. Make cheapest air travel plan between two cities with at
most k connecting flights,

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.
I All these computational problems require different algorithms.
e.g. Make cheapest air travel plan between two cities with at
most k connecting flights,
Solve Sudoku puzzle,

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.
I All these computational problems require different algorithms.
e.g. Make cheapest air travel plan between two cities with at
most k connecting flights,
Solve Sudoku puzzle,
Compile C++ program into machine code etc.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I An algorithm is correct if it halts for every input with the


correct output.
I All these computational problems require different algorithms.
e.g. Make cheapest air travel plan between two cities with at
most k connecting flights,
Solve Sudoku puzzle,
Compile C++ program into machine code etc.
I Are we using any algorithm right now?

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?


A data structure is a way to store and organize data in order
to facilitate access and modification.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?


A data structure is a way to store and organize data in order
to facilitate access and modification.
Can we organize data such that minimum element, maximum
element and any element k can be found efficiently even after
insertion and deletion of elements?

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?


A data structure is a way to store and organize data in order
to facilitate access and modification.
Can we organize data such that minimum element, maximum
element and any element k can be found efficiently even after
insertion and deletion of elements? Binary Search Tree

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?


A data structure is a way to store and organize data in order
to facilitate access and modification.
Can we organize data such that minimum element, maximum
element and any element k can be found efficiently even after
insertion and deletion of elements? Binary Search Tree
Can we organize data such that set membership and set union
operations can be performed efficiently?

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing

I What is a Data Structure?


A data structure is a way to store and organize data in order
to facilitate access and modification.
Can we organize data such that minimum element, maximum
element and any element k can be found efficiently even after
insertion and deletion of elements? Binary Search Tree
Can we organize data such that set membership and set union
operations can be performed efficiently? Disjoint-sets Forest
data structure

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

I There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

I There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
I Computers have limited memory.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

I There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
I Computers have limited memory.
I Only a finite number of instructions can be excecuted per
second.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

I There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
I Computers have limited memory.
I Only a finite number of instructions can be excecuted per
second.
I We should prefer an algorithm that uses lesser memory and
fewer instructions to solve a computational problem.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?

I There can be more than one algorithm that correctly solves a


given computational problem. Which one should we prefer?
I Computers have limited memory.
I Only a finite number of instructions can be excecuted per
second.
I We should prefer an algorithm that uses lesser memory and
fewer instructions to solve a computational problem.
I A good algorithm would be efficient in terms of computing
time and memory that is used.

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200
I Instructions that must be executed by each of the two
algorithms for n = 1 million (106 ≈ 220 )?
c1 n2

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200
I Instructions that must be executed by each of the two
algorithms for n = 1 million (106 ≈ 220 )?
c1 n2 ≈ 2 × 220 × 220

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200
I Instructions that must be executed by each of the two
algorithms for n = 1 million (106 ≈ 220 )?
c1 n2 ≈ 2 × 220 × 220 = 241

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200
I Instructions that must be executed by each of the two
algorithms for n = 1 million (106 ≈ 220 )?
c1 n2 ≈ 2 × 220 × 220 = 241 c2 n lg n ≈ 50 × 220 × 20

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms

I Two algorithms for the sorting problem: Insertion sort and


Merge sort.
I Insertion sort takes roughly c1 n2 machine-level instructions to
solve a sorting problem.
I Merge sort takes roughly c2 n lg n machine-level instructions.
I Let c1 = 2, c2 = 50. (c1 is usually much smaller than c2 .)
I Instructions that must be executed by each of the two
algorithms for n = 8?
c1 n2 = 2 × 8 × 8 = 128 c2 n lg n = 50 × 8 × 3 = 1200
I Instructions that must be executed by each of the two
algorithms for n = 1 million (106 ≈ 220 )?
c1 n2 ≈ 2 × 220 × 220 = 241 c2 n lg n ≈ 50 × 220 × 20 ≈ 230
Insertion sort requires 211 (≈ 2000) times more machine-level
instructions for solving the same problem!
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).
I Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).
I Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
I Let n = 10 million, c1 = 2 and c2 = 50.

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).
I Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
I Let n = 10 million, c1 = 2 and c2 = 50.
2 × (107 )2
I Time taken by faster computer A:
1010 instructions/second

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).
I Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
I Let n = 10 million, c1 = 2 and c2 = 50.
2 × (107 )2
I Time taken by faster computer A:
1010 instructions/second
50 × (107 ) lg 107
I Time taken by slower computer B:
107 instructions/second

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm

I Let us compare a faster computer A (running insertion sort)


with a slower computer B (running merge sort).
I Suppose computer A executes 10 billion instructions per
second and computer B executes 10 million instructions per
second.
I Let n = 10 million, c1 = 2 and c2 = 50.
2 × (107 )2
I Time taken by faster computer A:
1010 instructions/second
50 × (107 ) lg 107
I Time taken by slower computer B:
107 instructions/second
I Faster computer A takes more than 20,000 seconds (5.5
hours), whereas slower computer B takes around 1163 seconds
(< 20 minutes).

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency

I We should learn how to analyse and design efficient


algorithms.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparison of running times

I For each function f (n) and time t in the following table,


determine the largest size n of a problem that can be solved in
time t, assuming that the algorithm to solve the problem
takes f (n) microseconds.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time of Insertion sort

Input: A sequence of n numbers < a1 , a2 , . . . , an >


Output: A permutation < a10 , a20 , . . . , an0 > such that
a10 ≤ a20 ≤ · · · ≤ an0
I E.g., Input sequence : < 31, 41, 59, 26, 41, 58 >
I Sorting algorithm should give the output:
< 26, 31, 41, 41, 58, 59 >

13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insertion sort

I Main idea: Works the way people sort a hand of playing cards.

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insertion sort

I https://visualgo.net/en/sorting
5,2,4,6,1,3

15
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Pseudocode for Insertion sort

I Convention used : Array index starts from 1.

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Pseudocode for Insertion sort

I Convention used : Array index starts from 1.

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

I Loop invariant: At the start of each iteration of the for loop,


the subarray A[1..i − 1] contains elements originally in
A[1..i − 1], but in sorted order.

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

I Loop invariant: At the start of each iteration of the for loop,


the subarray A[1..i − 1] contains elements originally in
A[1..i − 1], but in sorted order.
I We use loop invariant to argue for the correctness of an
algorithm.

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

Loop invariant: At the start of each iteration of the for loop, the
subarray A[1..i − 1] contains elements originally in A[1..i − 1], but
in sorted order.

I Initialization, 18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

Loop invariant: At the start of each iteration of the for loop, the
subarray A[1..i − 1] contains elements originally in A[1..i − 1], but
in sorted order.

I Initialization, Maintenance, 18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Outline for correctness of Insertion sort

Loop invariant: At the start of each iteration of the for loop, the
subarray A[1..i − 1] contains elements originally in A[1..i − 1], but
in sorted order.

I Initialization, Maintenance, Termination 18


BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.
I Input size : depends on the computational problem

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.
I Input size : depends on the computational problem
I Sorting an array

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.
I Input size : depends on the computational problem
I Sorting an array
I Finding shortest path in a graph

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.
I Input size : depends on the computational problem
I Sorting an array
I Finding shortest path in a graph
I Primality test

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Running time of an algorithm is expressed as a function of the


size of its input.
I Input size : depends on the computational problem
I Sorting an array
I Finding shortest path in a graph
I Primality test
I Running time : time needed for the primitive operations or
“steps” executed by the Random-access machine (RAM)
model for an input of size n.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Random-access machine (RAM) is a generic single-processor


model of computation having no concurrent operations.

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Random-access machine (RAM) is a generic single-processor


model of computation having no concurrent operations.
I RAM model contains common operations/instructions (add,
subtract, load, store, conditional branch, subroutine call etc.)

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms

I Random-access machine (RAM) is a generic single-processor


model of computation having no concurrent operations.
I RAM model contains common operations/instructions (add,
subtract, load, store, conditional branch, subroutine call etc.)
I Each statement in the pseudocode takes a constant amount
of time.

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Pseudocode with time costs

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

I What will be the value of ti if the input sequence is already


sorted?

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

I What will be the value of ti if the input sequence is already


sorted?

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

I What will be the value of ti if the input sequence is already


sorted?

I The running time is of the form an + b, where a and b are


constants.

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps

I What will be the value of ti if the input sequence is already


sorted?

I The running time is of the form an + b, where a and b are


constants.
I So, the running time is a linear function of n if the input
sequence is already sorted (best case scenario).
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

I When will the worst case scenario occur?

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

I When will the worst case scenario occur?


I What will be the value of ti in the worst case scenario?

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

I When will the worst case scenario occur?


I What will be the value of ti in the worst case scenario?

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

I Worst case running time is of the form an2 + bn + c

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case

I Worst case running time is of the form an2 + bn + c


I So, the running time is a quadratic function of n if the input
sequence is sorted in the reverse order (worst case scenario).

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation

I What will the average case look like?

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation

I What will the average case look like?


i
I ti ≈ ,
2

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation

I What will the average case look like?


i
I ti ≈ , T (n) will again be a quadratic function of n.
2

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation

I What will the average case look like?


i
I ti ≈ , T (n) will again be a quadratic function of n.
2
I Very often we will be interested in only the worst-case running
time because it gives the upper bound on the running time for
any input.

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions

I We will make simplifying abstractions because we are only


interested in the rate of growth (order of growth) of the
running time for large values of n.

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions

I We will make simplifying abstractions because we are only


interested in the rate of growth (order of growth) of the
running time for large values of n.
I Suppose T (n) = 5n2 + 100n + 500

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions

I We will make simplifying abstractions because we are only


interested in the rate of growth (order of growth) of the
running time for large values of n.
I Suppose T (n) = 5n2 + 100n + 500
I After the simplifying abstractions, we are left with n2 which is
the factor by which T (n) will increase for large values of n.

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions

I We will make simplifying abstractions because we are only


interested in the rate of growth (order of growth) of the
running time for large values of n.
I Suppose T (n) = 5n2 + 100n + 500
I After the simplifying abstractions, we are left with n2 which is
the factor by which T (n) will increase for large values of n.
I We express the above by saying that insertion sort has a
worst-case running time of Θ(n2 ).

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions

I We will make simplifying abstractions because we are only


interested in the rate of growth (order of growth) of the
running time for large values of n.
I Suppose T (n) = 5n2 + 100n + 500
I After the simplifying abstractions, we are left with n2 which is
the factor by which T (n) will increase for large values of n.
I We express the above by saying that insertion sort has a
worst-case running time of Θ(n2 ).
I Is order of growth of Insertion-sort dependent on how it is
implemented?

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Problem

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .
I Approach 1 : Use insertion sort.

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .
I Approach 1 : Use insertion sort. Running time?

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .
I Approach 1 : Use insertion sort. Running time?
I Approach 2 :

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .
I Approach 1 : Use insertion sort. Running time?
I Approach 2 :

L: 4 8 11 25 30

R: 2 3 17 20

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted

Input : Sorted subarrays A[p..q] and A[q + 1..r ] such that


p ≤ q < r.
Output : Combined sorted subarray A[p..r ].
I E.g. . . . , 4, 8, 11, 25, 30, 2, 3, 17, 20 . . .
I Approach 1 : Use insertion sort. Running time?
I Approach 2 :

L: 4 8 11 25 30

R: 2 3 17 20

https://opendsa-server.cs.vt.edu/ODSA/Books/
Everything/html/Mergesort.html
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge Procedure

31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge procedure

I Running time of MERGE procedure is Θ(n)

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort

I Initial call MERGE -SORT (A, 1, n)

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort operations

A = [ 5, 2, 4, 7, 1, 3, 6, 2 ]

34
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort operations

A = [ 5, 2, 4, 7, 1, 3, 6, 2 ]

34
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort operations

A = [ 5, 2, 4, 7, 1, 3, 6, 2 ]

35
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort operations

A = [ 5, 2, 4, 7, 1, 3, 6, 2 ]
https://opendsa-server.cs.vt.edu/ODSA/Books/
Everything/html/Mergesort.html

35
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach

I The merge sort algorithm follows the divide-and-conquer


paradigm

36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach

I The merge sort algorithm follows the divide-and-conquer


paradigm
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each.

36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach

I The merge sort algorithm follows the divide-and-conquer


paradigm
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using merge
sort.

36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach

I The merge sort algorithm follows the divide-and-conquer


paradigm
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using merge
sort.
Combine: Merge the two sorted subsequences to produce the
sorted answer.

36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing divide-and-conquer algorithms

I Recurrence equation:

37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm

I Recurrence for worst-case running time of merge sort:

38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm

I Recurrence for worst-case running time of merge sort:

I Recurrence for worst-case running time of merge sort:

38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree for merge sort

39
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree for merge sort

40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree for merge sort

41
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm

I The recursion tree will have lg n + 1 levels.

42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm

I The recursion tree will have lg n + 1 levels.


I Worst case running time of merge sort:

T (n) = c2 n lg n + c1 n

42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm

I The recursion tree will have lg n + 1 levels.


I Worst case running time of merge sort:

T (n) = c2 n lg n + c1 n
= Θ(n lg n)

42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
When n is not a power of 2

43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
When n is not a power of 2

I The number of levels in which merge operation is performed


will be at least dlg ne − 1.

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
When n is not a power of 2

I The number of levels in which merge operation is performed


will be at least dlg ne − 1.
I So the total running time will be at least:
c2 n(dlg ne − 1) + c1 n ≥ c2 n(lg n − 1) + c1 n .

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
When n is not a power of 2

I The number of levels in which merge operation is performed


will be at least dlg ne − 1.
I So the total running time will be at least:
c2 n(dlg ne − 1) + c1 n ≥ c2 n(lg n − 1) + c1 n .
I So, the total running time will remain Θ(n lg n) even when n
is not a power of 2.

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Effect of Ceiling and Floor operations
n/(2i ) − 1 n/(2i ) n/(2i ) n/(2i ) + 1
       
i
0 999999 1000000 1000000 1000001
1 499999 500000 500000 500001
2 249999 250000 250000 250001
3 124999 125000 125000 125001
4 62499 62500 62500 62501
5 31249 31250 31250 31251
6 15624 15625 15625 15626
7 7811 7812 7813 7814
8 3905 3906 3907 3908
9 1952 1953 1954 1955
10 975 976 977 978
11 487 488 489 490
12 243 244 245 246
13 121 122 123 124
14 60 61 62 63
45
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise

Q. Search an element k in a given sorted array A[1..n]. Algorithm


for linear search is as given below:

46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise

Q. Search an element k in a given sorted array A[1..n]. Algorithm


for linear search is as given below:
1: procedure LinearSearch(A, k)
2: for i ← 1 to length of array A do
3: if A[i] = k then
4: return i . Key found at index i
5: return −1 . Key not found

46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise

Q. Search an element k in a given sorted array A[1..n]. Algorithm


for linear search is as given below:
1: procedure LinearSearch(A, k)
2: for i ← 1 to length of array A do
3: if A[i] = k then
4: return i . Key found at index i
5: return −1 . Key not found
(a) What is the worst case running time of the above algorithm in
Θ(·) notation.

46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise

Q. Search an element k in a given sorted array A[1..n]. Algorithm


for linear search is as given below:
1: procedure LinearSearch(A, k)
2: for i ← 1 to length of array A do
3: if A[i] = k then
4: return i . Key found at index i
5: return −1 . Key not found
(a) What is the worst case running time of the above algorithm in
Θ(·) notation.
(b) Use divide-and-conquer approach to improve the worst case
running time found in part (a).

46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm

Visualization :
https://binary-search-visualization.netlify.app/
16,25,32,47,51,64,79,83,98,103,126,134,147,151
Search for 47, 105 .

47
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm

1: function BinarySearch(A, k, left, right)


2: if left > right then
3: j −1 k
return . Element not found
left+right
4: mid ← 2
5: if A[mid] = k then
6: return mid . Element found, return its index
7: else if A[mid] < k then
8: return BinarySearch(A, k, mid + 1, right)
9: else
10: return BinarySearch(A, k, left, mid − 1)

48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm

1: function BinarySearch(A, k, left, right)


2: if left > right then
3: j −1 k
return . Element not found
left+right
4: mid ← 2
5: if A[mid] = k then
6: return mid . Element found, return its index
7: else if A[mid] < k then
8: return BinarySearch(A, k, mid + 1, right)
9: else
10: return BinarySearch(A, k, left, mid − 1)
(c) Find the recurrence equation for the above algorithm. What is
the worst case running time in Θ(·) notation.

48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm

1: function BinarySearch(A, k, left, right)


2: if left > right then
3: j −1 k
return . Element not found
left+right
4: mid ← 2
5: if A[mid] = k then
6: return mid . Element found, return its index
7: else if A[mid] < k then
8: return BinarySearch(A, k, mid + 1, right)
9: else
10: return BinarySearch(A, k, left, mid − 1)
(c) Find the recurrence equation for the above algorithm. What is
the worst case running time in Θ(·) notation. Best case
running time?

48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm

1: function BinarySearch(A, k, left, right)


2: if left > right then
3: j −1 k
return . Element not found
left+right
4: mid ← 2
5: if A[mid] = k then
6: return mid . Element found, return its index
7: else if A[mid] < k then
8: return BinarySearch(A, k, mid + 1, right)
9: else
10: return BinarySearch(A, k, left, mid − 1)
(c) Find the recurrence equation for the above algorithm. What is
the worst case running time in Θ(·) notation. Best case
running time?
(d) Find the number of array elements compared by each of the
two algorithms in the worst case for an array of size n = 220 .
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).
I We use different asymptotic notations (e.g. Θ notation) for
describing the efficiency of algrithms.

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).
I We use different asymptotic notations (e.g. Θ notation) for
describing the efficiency of algrithms.
I When we say running time T (n) = Θ(n2 ), we mean T (n) is a
function in the set Θ(n2 ).

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).
I We use different asymptotic notations (e.g. Θ notation) for
describing the efficiency of algrithms.
I When we say running time T (n) = Θ(n2 ), we mean T (n) is a
function in the set Θ(n2 ).
I O notation,

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).
I We use different asymptotic notations (e.g. Θ notation) for
describing the efficiency of algrithms.
I When we say running time T (n) = Θ(n2 ), we mean T (n) is a
function in the set Θ(n2 ).
I O notation, Ω notation,

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 3 : Growth of Functions

I Once input size n is large enough, merge sort (Θ(n lg n)) will
beat insertion sort (Θ(n2 )).
I We are interested in studying the asymptotic efficiency of
algorithms (i.e. order of growth when n tends to infinity).
I We use different asymptotic notations (e.g. Θ notation) for
describing the efficiency of algrithms.
I When we say running time T (n) = Θ(n2 ), we mean T (n) is a
function in the set Θ(n2 ).
I O notation, Ω notation, Θ notation

49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I We use O notation to express asymptotic upper bound.

50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I We use O notation to express asymptotic upper bound.

50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)


2
0≤3+ 2 ≤c
n

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)


2
0≤3+ 2 ≤c
n

c = 5, n0 = 1

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)


2
0≤3+ 2 ≤c
n

c = 5, n0 = 1
I Are there other possible values of c and n0 ?

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)


2
0≤3+ 2 ≤c
n

c = 5, n0 = 1
I Are there other possible values of c and n0 ?
n0 = 100,

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 3n2 + 2 ∈ O(n2 ) ?
I To check the above we need to find positive constants c and
n0 such that:

0 ≤ 3n2 + 2 ≤ cn2 , For n ≥ n0 (1)


2
0≤3+ 2 ≤c
n

c = 5, n0 = 1
I Are there other possible values of c and n0 ?
n0 = 100, c = 3.0002

51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)


2000
0 ≤ 1000 + ≤c
n

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)


2000
0 ≤ 1000 + ≤c
n
n0 = 10,

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)


2000
0 ≤ 1000 + ≤c
n
n0 = 10, c = 1200

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)


2000
0 ≤ 1000 + ≤c
n
n0 = 10, c = 1200

I Is 2n3 ∈ O(n2 ) ?

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I Is 1000n2 + 2000n ∈ O(n2 ) ?

0 ≤ 1000n2 + 2000n ≤ cn2 , For n ≥ n0 (2)


2000
0 ≤ 1000 + ≤c
n
n0 = 10, c = 1200

I Is 2n3 ∈ O(n2 ) ?
I Is 2n + 2 ∈ O(n) ?

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I O notation helps to express the running time on every input.

53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I O notation helps to express the running time on every input.


I O notation simplifies the understanding of an algorithm’s
efficiency by focusing on the main idea.

53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I O notation helps to express the running time on every input.


I O notation simplifies the understanding of an algorithm’s
efficiency by focusing on the main idea.
I Running time of merge sort is O(n lg n).
I Can we say, “running time of insertion sort on every input is
O(n3 )”?

53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)

I O notation helps to express the running time on every input.


I O notation simplifies the understanding of an algorithm’s
efficiency by focusing on the main idea.
I Running time of merge sort is O(n lg n).
I Can we say, “running time of insertion sort on every input is
O(n3 )”?
I Running time of binary search algorithm on every input is
.

53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I Provides an asymptotic lower bound.

54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I Provides an asymptotic lower bound.

54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I Provides an asymptotic lower bound.

54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I Is 3n2 − 200 ∈ Ω(n2 ) ?

55
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I Is 3n2 − 200 ∈ Ω(n2 ) ?


I Is 3n2 − 200 ∈ Ω(n3 ) ?

55
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I It helps us express the best case running time for any input.

56
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I It helps us express the best case running time for any input.
I The running time of insertion sort is Ω(n) for any input.

56
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation

I It helps us express the best case running time for any input.
I The running time of insertion sort is Ω(n) for any input.
I The running time of binary search is for any input.

56
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

57
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

57
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation
1 2
I Is 2n − 3n ∈ Θ(n2 )?

58
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 1 2 2
2 n − 3n ∈ Θ(n )?
I To check the above we need to find positive constants c1 , c2
and n0 such that:
1
0 ≤ c1 n2 ≤ n2 − 3n ≤ c2 n2 , For n ≥ n0 (3)
2

58
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 1 2 2
2 n − 3n ∈ Θ(n )?
I To check the above we need to find positive constants c1 , c2
and n0 such that:
1
0 ≤ c1 n2 ≤ n2 − 3n ≤ c2 n2 , For n ≥ n0 (3)
2
1 3
0 ≤ c1 ≤ − ≤ c2
2 n

58
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 1 2 2
2 n − 3n ∈ Θ(n )?
I To check the above we need to find positive constants c1 , c2
and n0 such that:
1
0 ≤ c1 n2 ≤ n2 − 3n ≤ c2 n2 , For n ≥ n0 (3)
2
1 3
0 ≤ c1 ≤ − ≤ c2
2 n
I Eqn. 3 will be true for c1 = 1/14, c2 = 1/2 and n0 = 7.
(Other choices are also possible.)

58
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?
I Let p(n) be a degree d polynomial:
cd nd + cd−1 nd−1 + · · · + c2 n2 + c1 n + c0

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?
I Let p(n) be a degree d polynomial:
cd nd + cd−1 nd−1 + · · · + c2 n2 + c1 n + c0
I p(n) ∈ Θ(nd ).

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?
I Let p(n) be a degree d polynomial:
cd nd + cd−1 nd−1 + · · · + c2 n2 + c1 n + c0
I p(n) ∈ Θ(nd ).
Note: The coefficient of the highest degree term (cd ) must be
positive.

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?
I Let p(n) be a degree d polynomial:
cd nd + cd−1 nd−1 + · · · + c2 n2 + c1 n + c0
I p(n) ∈ Θ(nd ).
Note: The coefficient of the highest degree term (cd ) must be
positive.
I Θ-notation helps us to be more precise when expressing the
running time:

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Θ notation

I Is 3n + 2 ∈ Θ(n2 ) ?
I Is 2n3 ∈ Θ(n2 ) ?
I Let p(n) be a degree d polynomial:
cd nd + cd−1 nd−1 + · · · + c2 n2 + c1 n + c0
I p(n) ∈ Θ(nd ).
Note: The coefficient of the highest degree term (cd ) must be
positive.
I Θ-notation helps us to be more precise when expressing the
running time:
Worst case running time of merge-sort algorithm is Θ(n lg n).

59
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Asymptotic notation

I 2n + 2 = O(n2 )

60
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Asymptotic notation

I 2n + 2 = O(n2 )
I 2n2 + Θ(n) = Θ(n2 )

60
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Asymptotic notation

I 2n + 2 = O(n2 )
I 2n2 + Θ(n) = Θ(n2 )
I Is the following statement true?
If f (n) = O(g (n)) then f (n) = Θ(g (n)).

60
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Asymptotic notation

I 2n + 2 = O(n2 )
I 2n2 + Θ(n) = Θ(n2 )
I Is the following statement true?
If f (n) = O(g (n)) then f (n) = Θ(g (n)).

60
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.
I Tight bound:2n2 = O(n2 )

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.
I Tight bound:2n2 = O(n2 ) Loose bound:2n = O(n2 ).

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.
I Tight bound:2n2 = O(n2 ) Loose bound:2n = O(n2 ).

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.
I Tight bound:2n2 = O(n2 ) Loose bound:2n = O(n2 ).

I Is 2n2 + 1 = o(n2 ) ?

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation

I o(g (n)) “little-oh of g of n”


I We use this notation to denote an asymptotically loose
upperbound.
I Tight bound:2n2 = O(n2 ) Loose bound:2n = O(n2 ).

I Is 2n2 + 1 = o(n2 ) ?
I Is 2n2 + 1 = o(n3 ) ?

61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound
I Tight bound:2n2 = Ω(n2 )

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound
I Tight bound:2n2 = Ω(n2 ) Loose bound:2n2 = Ω(n).

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound
I Tight bound:2n2 = Ω(n2 ) Loose bound:2n2 = Ω(n).

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound
I Tight bound:2n2 = Ω(n2 ) Loose bound:2n2 = Ω(n).

I Is 2n2 + 1 = ω(n2 ) ?

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation

I ω(g (n)) “little-omega of g of n”


I We use this notation to denote an asymptotically loose
lowerbound
I Tight bound:2n2 = Ω(n2 ) Loose bound:2n2 = Ω(n).

I Is 2n2 + 1 = ω(n2 ) ?
I Is 2n2 + 1 = ω(n) ?

62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparing Functions

I Is the following True?


f (n) = o(g (n)) if and only if g (n) = ω(f (n))
I Asymptotic notations:

63
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparing Functions

I Is the following True?


f (n) = o(g (n)) if and only if g (n) = ω(f (n))
I Asymptotic notations:
1. O-notation
2. Ω-notation
3. Θ-notation
4. o-notation
5. ω-notation

63
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Standard notations and common functions

I alogc b = b logc a , where a > 0 and b > 0


Let k = logb a, then a = b k

64
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Standard notations and common functions

I alogc b = b logc a , where a > 0 and b > 0


Let k = logb a, then a = b k

I Skim through section 3.3 of the textbook.

64
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like