Professional Documents
Culture Documents
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
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
My expectations from students
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
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Role of algorithms in computing
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Which Algorithm should be preferred?
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Efficiency of Algorithms
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of a good algorithm
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Importance of efficiency
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparison of running times
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time of Insertion sort
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
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Pseudocode for Insertion sort
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
17
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
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.
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analysis of algorithms
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
23
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
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs of all the steps
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Summation of time costs in the worst case
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
26
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
Running time estimation
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Running time estimation
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Order of growth: Simplifying abstractions
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
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge two subarrays that are sorted
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
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
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Merge sort
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
36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach
36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach
36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
The divide-and-conquer approach
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
38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm
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
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the merge sort algorithm
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
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
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
When n is not a power of 2
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
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
46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise
46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise
46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Exercise
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
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Search Algorithm
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)
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
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:
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:
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:
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:
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:
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:
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)
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
I Is 2n3 ∈ O(n2 ) ?
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
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)
53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
O notation (Big-oh)
53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation
54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation
54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation
54
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation
55
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ω-notation
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
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
I Is 2n2 + 1 = o(n2 ) ?
61
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
o-notation
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
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
I Is 2n2 + 1 = ω(n2 ) ?
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
ω-notation
I Is 2n2 + 1 = ω(n2 ) ?
I Is 2n2 + 1 = ω(n) ?
62
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparing Functions
63
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Comparing Functions
63
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Standard notations and common functions
64
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Standard notations and common functions
64
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms