4timecomplexityandrecursion
4timecomplexityandrecursion
Sungmin Cha
New York University
09.16.2024
Outline
• Notice
• Recursion
2
Outline
• Notice
• 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
4
Outline
• Notice
• Recursion
5
Review the previous lecture
• Several examples of memory allocation of variables
– Primitive and reference variable
– Stack and Heap memory
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.
20
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 2:
21
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 3:
22
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 4:
23
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 5:
24
Time Complexity
• What is the worst-case time complexity (Big-O)?
– Example 6:
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
26
Outline
• Notice
• 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
28
Recursion
• What is recursion?
– Example: Factorial (n!)
𝑛! = 𝑛 × 𝑛 − 1 × 𝑛 − 2 × 𝑛 − 3 × 𝑛 − 4 × … × 2 × 1
(𝑛 − 1)!
𝑛 × 𝑓 𝑛−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
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