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

Chapter 1. Data Structures and Complexity

This document discusses complexity analysis for recursive algorithms. It provides an example of analyzing the binary search algorithm recursively. The key steps are: 1) Count basic operations directly, 2) Define T(n) as the total operations and derive a recursive function, 3) Calculate the closed form of T(n) which provides the overall time complexity. Selection sort is also analyzed as an example, showing it has O(n^2) time complexity. Practice problems on deriving recursive time complexities are provided.

Uploaded by

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

Chapter 1. Data Structures and Complexity

This document discusses complexity analysis for recursive algorithms. It provides an example of analyzing the binary search algorithm recursively. The key steps are: 1) Count basic operations directly, 2) Define T(n) as the total operations and derive a recursive function, 3) Calculate the closed form of T(n) which provides the overall time complexity. Selection sort is also analyzed as an example, showing it has O(n^2) time complexity. Practice problems on deriving recursive time complexities are provided.

Uploaded by

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

CSCI2100E Complexity for

Recursive Algorithms

Complexity Analysis, Array & Structures 1


How to Count Basic Operations in Recursion?
Algorithm 1: BinarySearch(arr, searchnum, left, right)
1 if left = right 𝑂 (1)
2 if arr[left]= searchnum 𝑂 (1)
3 return left 𝑂 (1)
4 else 𝑂 (1)
5 return -1 𝑂 (1)
6 middle (left + right)/2 𝑂 (1)
7 if arr[middle] = searchnum 𝑂 (1)
8 return middle 𝑂 (1)
9
elseif arr[middle] < searchnum 𝑂 (1)
10
return BinarySearch(arr, searchnum, middle+1, right)
11
else 𝑂 (?)
12
return BinarySearch(arr, searchnum, left, middle -1)
13
𝑂 (?)

Complexity Analysis 2
Complexity Analysis with Recursions
Given the input size , let be the total number of basic
operations executed in BinarySearch
Algorithm 1: BinarySearch(arr, searchnum, left, right)
1 if left = right We can still count the number of
2 if arr[left]= searchnum basic operations for this part
3 return left
4 else The total number of basic
5 return -1 operations executed is a constant
6 middle (left + right)/2 intendent of the input size n, we
7 if arr[middle] = searchnum can use to denote this
8 return middle
9 elseif arr[middle] < searchnum
10 return BinarySearch(arr, searchnum, middle+1, right)
11 else
12 return BinarySearch(arr, searchnum, left, middle -1)
We either run line 10 or 12, but not both. What is the number of basic
operations that are executed by Line 10 or 12?
Complexity Analysis 3
Analysis for Recursive Binary Search (i)

can be also defined recursively


At the beginning, the input size is ,
After executing basic operations, we reduce the
input size by half.
Then, we run the recursive binary search with input size
• What is the number of basic operations executed in the
worst case by recursive binary search?
• We do not know, but we know it is according to our
definition

excluded

𝑛 𝑛/2
𝑔 ( 𝑛 ) =𝑎 +𝑔 ( )
𝑛
2

Complexity Analysis 4
Analysis for Recursive Binary Search (ii)
Given ,
What is by using and to represent?

What is by using and to represent if ?

of them

Complexity Analysis 5
Analysis for Recursive Binary Search (iii)

What is the time complexity of ?

Complexity Analysis 6
Sorting with Recursion
Input: a set S of n integers
Problem: store S in an array such that the elements
are arranged in ascending order

4 2 3 6 9 5 2 3 4 5 6 9

Complexity Analysis 7
Selection Sort
Step 1: Scan all the n elements in the array to find
the position of the largest element
,

4 2 3 6 9 5

Step 2: swap the position of the last one and

4 2 3 6 5 9

Step 3: We have a smaller problem: sorting the first


n-1 elements

4 2 3 6 5 9
sorted

Complexity Analysis 8
Selection Sort
Algorithm 2: selectionSort(arr, n)
1 if n 1 maxElement =9
2 return arr maxIndex = 4
3 maxElement  arr[0] maxElement =4 maxElement =6
4 maxIndex  0 maxIndex = 0 maxIndex = i
5 for i  1 to n -1
6 if maxElement >
7 arr[i] 4 2 3 6 9 5
8 maxElement =
9 arr[i]
i=1 i=2 i=3 i=4
10 maxIndex = I
11 arr[maxIndex] = arr[n-1]
arr[n-1] = maxElement
selectionSort(arr, n-1) 4 2 3 6 5 sorted
9

Complexity Analysis 9
Selection Sort: Complexity Analysis
Algorithm 2: selectionSort(arr, n) # of basic operations
1 if n 1 𝑂 (1)
2 return arr 𝑂 (1)
3 maxElement  arr[0] 𝑂 (1)
4 maxIndex  0 𝑂 (1)
5 for i  1 to n -1 𝑂 (𝑛)
6 if maxElement > 𝑂 (𝑛)
7 arr[i] 𝑂 (𝑛)
8 maxElement = 𝑂 (𝑛)
9 arr[i] 𝑂 (1)
10 maxIndex = i 𝑂 (1)
11 arr[maxIndex] = arr[n-1] 𝑂 (?)
arr[n-1] = maxElement
What is the total #n-1)
selectionSort(arr, of basic operations from Lines 1-10?
Depends linearly to n, we use to denote the cost. Both and are
constant, we do not care their specific values

Complexity Analysis 10
Analysis for Selection Sort (i)
Let be the total number of basic
operations in the worst case

Let , which is the base case when the array


has only one element

Complexity Analysis 11
Analysis for Selection Sort (ii)
Given and
What is by using and to represent?

What is by using and to represent?

What is the time complexity of


Complexity Analysis 12
Practice*
Analyze the time complexity of maxInArray algorithm
Algorithm 3: maxInArray(arr, n) # of basic operations
𝑂 (1)
1 if n 1 𝑂 (1)
2 return arr[0]
𝑂 (1)
3 else
4 tempMax = maxInArray(arr, n-1) 𝑂 (?)
5 return max(arr[n-1], tempMax) 𝑂 (1)
Let be the total number of basic operations we counted except
line 4. It is a constant independent of . Let be
Let be the total number of basic operations executed by
MaxInArray, what we can derive?

Complexity Analysis 13
Summary: Complexity Analysis with Recursion

Step 1: Count the number of basic operations we can


count directly.

Step 2: Define to be the total # of basic operations


in the worst case
Derive the recursive function

Step 3: calculate

Complexity Analysis 14

You might also like