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

4timecomplexityandrecursion

The lecture covers time complexity and recursion, emphasizing the importance of understanding algorithm efficiency and memory management in programming. Key topics include the definition of time complexity using Big-O notation, examples of worst-case scenarios, and the concept of recursion with practical applications. Additionally, the lecture discusses the advantages and disadvantages of recursive functions, highlighting their impact on code readability and performance.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

4timecomplexityandrecursion

The lecture covers time complexity and recursion, emphasizing the importance of understanding algorithm efficiency and memory management in programming. Key topics include the definition of time complexity using Big-O notation, examples of worst-case scenarios, and the concept of recursion with practical applications. Additionally, the lecture discusses the advantages and disadvantages of recursive functions, highlighting their impact on code readability and performance.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Data Structures

<Lecture 4: Time Complexity and Recursion>

Sungmin Cha
New York University

09.16.2024
Outline
• Notice

• Review the previous lecture

• Time complexity of algorithm

• Recursion

2
Outline
• Notice

• Review the previous lecture

• Time complexity of algorithm

• Recursion

3
Notice
• There will be 1-2 programming problems in each lab
– Deadline: 12PM next Monday
 Late submission: 12PM next Tuesday (penalty: 50% of your score)
– I will upload the solution code and hidden test cases
every Wednesday (before the lecture)
– Please upload your questions to Ed discussion
 If you send an email to me, the response would be late

• On Wednesday, HW1 will be posted!


– I will introduce details of it in the next lecture

4
Outline
• Notice

• Review the previous lecture

• Time complexity of algorithm

• Recursion

5
Review the previous lecture
• Several examples of memory allocation of variables
– Primitive and reference variable
– Stack and Heap memory

• JAVA uses pass-by-value


– The function parameter values are copied to another variable
and then the copied object is passed to the function.
– For primitive variables, the real value is stored in the Stack
 As a result, it sends the copied real value to the function
– In the case of reference variables, the actual location of the
object allocated in the Heap is stored in the Stack
 Hence, it sends the copied location of the object to the function
6
Review the previous lecture
• Example 3: swap function
– What happens in memory for swap function? Stack

How to swap num1


and num2?

main
num1 = 3.1415
num2 = 2.1718
3.1415 2.1718
7
Review the previous lecture
• Example 3: swap function
– Let’s make a simple function that swaps a value in two floating-
point variables.
– Goal
1. Take two inputs of the actual number and save them into a variable
2. Print a value saved in the variable
3. Apply swap function
4. Print a value saved in the variable
– Hints
 Using other types of variable
 Using a primitive variable but applying a trick based on the
characteristic of local variable in Stack memory

8
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

main
num1 = 3.1415
num2 = 2.1718

9
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

swap
d1 = 2.1718
d2 = ?

main
num1 = 3.1415
num2 = 2.1718

10
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

swap
d1 = 2.1718
d2 = ?

main
num1 = 3.1415
num2 = 3.1415

11
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

swap
d1 = 2.1718
d2 = 3.1415

main
num1 = 3.1415
num2 = 3.1415

12
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

swap
d1 = 2.1718
d2 = 3.1415

return
main
num1 = 3.1415
num2 = 3.1415

13
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

main
num1 = 2.1718
num2 = 3.1415

14
Review the previous lecture
• Example 3: swap function
– The simple solution using a trick Stack

main
num1 = 2.1718
num2 = 3.1415

2.1718 3.1415
15
Review the previous lecture
• Example 3: swap function
– Using array
 array == reference variable

16
Outline
• Review the previous lecture

• Time complexity

• Recursion

17
Time Complexity
• What is the condition of a good algorithm?
– In most cases, a good algorithm is one that utilizes resources
effectively
– Resources: time, memory, network bandwidth, etc.
 In Data Structures and Algorithm classes, time is considered as the most
important resource.

• How can we express algorithm execution time?


– Actual running time (e.g., sec)?
 It depends on hardware, OS, the size of inputs, etc.
– The algorithm execution time is expressed in terms of how long
it takes relative to the size of the input (n).
 Asymptotic Running Time
18
Time Complexity
• Asymptotic Running Time
– In other words, it is time complexity
– The primary method for expressing time complexity is Big-O
 𝚶(): asymptotic upper bound
 Ω(): asymptotic lower bound You will learn them
 Θ(): asymptotic tight bound during the Algorithm class

Figure from this link 19


Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 1:

– For the input size of n, what is the time complexity?

20
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 2:

– For the input size of n, what is the time complexity?

21
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 3:

– For the input size of n,


what is the time complexity?

22
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 4:

– For the input size of n, what is the running time?

23
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 5:

– For the input size of n, what is the running time?

24
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 6:

– For the input size of n, what is the running time?

25
Time Complexity
• Big-O notation considers only the highest-order term
– Why?

n 𝒏𝟐 𝒏𝟐 − 𝟐𝒏 𝒏𝟐 − 𝟐𝒏 + 𝟏
10 100 80 81
20 400 360 361
50 2,500 2,400 2,401
100 10,000 9,800 9,801
1000 1,000,000 998,000 998,001

As n become larger, the time complexity is


ultimately dominated by the highest-order term

26
Outline
• Notice

• Review the previous lecture

• Time complexity of algorithm

• Recursion

27
Recursion
• What is recursion?
– Recursion occurs when the definition of a concept or process
depends on a simpler or previous version of itself
 From Wikipedia

– Examples in the real world


: nesting dolls, fractals

28
Recursion
• What is recursion?
– Example: Factorial (n!)

𝑛! = 𝑛 × 𝑛 − 1 × 𝑛 − 2 × 𝑛 − 3 × 𝑛 − 4 × … × 2 × 1

(𝑛 − 1)!

A factorial (n) includes the factorial (n-1)

 Let 𝑓 𝑛 is 𝑛!, 𝑓 𝑛 is defined as below

𝑛 × 𝑓 𝑛−1 … 𝑛≥1
𝑓 𝑛 =ቊ
1 … 𝑛=1
29
Recursion Function
• Recursion in programming language (JAVA)
– Recursive function refers to a function that calls itself within
its own function

 What is the expected output?

30
Recursive Function
• Recursion in programming language (JAVA)
– Recursive function refers to a function that calls itself within
its own function

 When a recursion function is called, how does the layout of the stack
memory change?

31
Recursive Function
• Understanding the flow of a recursive function
Main function() Stack

main
32
Recursive Function
• Understanding the flow of a recursive function
– When a recursive function is called, a copy of the recursive
function is created, and the copy is then executed
– When a recursive function returns, the return value is sent back
to the position where the recursive function was called in the
previous function
 Then, the subsequent statements are executed

33
Recursive Function
• The exit condition in recursion
– Setting the exit condition well is important

– Why?
 If there is no proper exit condition, the function calls functions infinitely
o stack overflow
34
Recursive Function
• Advantages and disadvantages of recursive function
– Advantages
1. Conciseness
: recursive solutions can lead to more concise and elegant code
2. Readability
: recursion often provides a natural and readable solution for certain
types of problems, enhancing code readability
3. Problem-solving
: recursive solutions can offer logical and intuitive approaches for
specific problem types (e.g., graph traversal, merge sort, etc.)

35
Recursive Function
• Advantages and disadvantages of recursive function
– Disadvantages
1. Performance
: recursion involves repeated function calls, potentially incurring function
call overhead and leading to performance degradation
2. Memory usage
: recursive calls use the stack call, and deep recursion may risk stack
overflow, resulting in memory issues
3. Debugging complexity
: Debugging recursive functions can be challenging.

36
Thank you!

E-mail: [email protected]

37

You might also like