CSE 373 SP25
Section AX Week 2
Runtime Analysis
Warm-Up: What is the difference between case analysis and asymptotic analysis?
Announcements
MicroTeach: Runtime Analysis and ADTs
Formal Notation to compare Runtime?
Goal:
Steps:
Case Analysis vs Asymptotic Analysis Confusion
Can we combine the 2 methods? Yes! We can do a best & worst case asymptotic analysis (and you will need to in deques and future projects!).
The Three Bounds
�����If you feel shaky with functions, plot them out as you come across new bounds in this class using Desmos to understand variations in growth!�
What does Θ represent?
f must be both O(g) and Ω(g) to be Θ(g)!�
Think of this as “squeezing” the function from above and below.
Visual Reference
big-O
big-Omega (Ω)
Asymptotically “Less than or equal to k * f(n)”
Asymptotically “Greater than or equal to k * f(n)”
big-Theta (Θ)
k1* n ≤ Θ ≤ k2* n
ADTs Summary/Review
“Abstract Data Type”, indicates general expected functionality
Client v. Implementer:
It’s key for us to navigate these responsibilities and implications as creators of technology to best cater to user needs
Questions?
Worksheet Problems:
Asymptotic Analysis and Design Decisions
Asymptotic Notation
Challenge Q: What if we only knew that the order of growth of some function f(N) was in O(N)?
Asymptotic Analysis (Solution)
Challenge Q: What if we only knew that the order of growth of some function f(N) was in O(N)?
A: We cannot make assumptions for the other bounds beyond O(-). With some given upper bound, we can imagine other applicable bounds >= N, such as O(N^2).
Design Decisions: Restaurant
A restaurant wants to table customers in the order they arrived. When an inspector comes in to check the service by ordering, the restaurant also hopes to give them a table before other customers. Since there is a really long wait time for tables, waiters only look at the ADT representation when giving out tables.
Choose ADTs:
Choose data structures to implement your chosen ADTs:
ArrayList
LinkedList
etc…
You do not need to figure out the implementation of the Map if you use one.
Design Decisions: Restaurant
Sample Solution:
I would use the Deque ADT and implement this using a Linked List. This can use front and back pointers, allowing for new customers at the back and the inspector inserted to the front.
Cons: Finding specific customers/groups that leave the line
Pros: More efficient since no time is lost to shifting elements, less memory intensive
This can interact with some Map for tabling to place customers accordingly when they are removed from the front.
(key: int number, value: Object tableInfo)
Design Decisions: Disneyland
Disneyland has hired you to find a way to improve the processing efficiency of their long lines at attractions. There is no way to forecast how long the lines will be.
In addition, “the Disability Access Service (DAS) is intended for Guests who have difficulty tolerating extended waits in a conventional line environment due to disability. This service doesn’t provide immediate access to experiences, but rather allows Guests to request a return time for a specific experience that is comparable to the current standby wait.” (Source)
Design Decisions: Disneyland
The Map ADT for key, value pairs will be used for those utilizing the Disability Access Service and will help with lookup when a customer comes back to check how much time they have left to wait and what place in line they would be in, if they would like to re-join now.
Disneyland: Task
Please choose how we should implement the program of the line for the attraction. We want the functionality of the line to be able to:
Disneyland: Implementation
Please choose from the following:
Justify your reasoning. You do not need to figure out the implementation of the Map.
Choose ADTs:
Choose a data structures to implement your chosen ADTs:
ArrayList
LinkedList
etc…
Disneyland: Implementation
Sample Solution:
I would use the List ADT and implement this using a Linked List. This can include front and back pointers, directly corresponding to the add/remove functions.
For reinsertion at any given place, 2 pointers also work to choose the option with fewer nodes to traverse through.
*(asymptotically, n/2 => n)
Cons: Traversal through many nodes to add in at some xth spot, especially at middle
Pros: No issues with shifting of elements or resizing like an ArrayList
This can interact with the Map to determine line position for those utilizing DAS.
(key: int userId, value: Object customer info)
Sliding Window Practice
Why LeetCode Style Code Challenges?
Example Problem - Longest increasing subsequence
Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].
Input: nums = [1, 3, 5, 4, 7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1, 3, 5] with length 3.
Even though [1, 3, 5, 7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
Example Problem - Solution & Approach
public int lengthOfLCIS(int[] A) {
int result = 0;
int anchor = 0;
for (int i = 0; i < A.length; i++) {
if (i > 0 && A[i-1] >= A[i]) {
anchor = i;
result = Math.max(
result,
i - anchor + 1
);
}
}
return result;
}
Scan through the array once, extending the window as long as the sequence increases. If it stops increasing, slide the window to the current index.
Anchor: We track the start index of the current increasing subarray using a variable called anchor.
Update Anchor: If the current number is not greater than the previous one, the increasing sequence breaks so we move the anchor to the current index, starting a new subarray.
Subarray Length: The current increasing subarray has length i - anchor + 1, so we update the result with the maximum length found so far.
Instructions
Each group of 3 or 4 students will be assigned one of the following problems:
For instructions, search online for the problem by ID number, e.g. “LeetCode 219”.
You’ll have about 20 minutes to complete:
Let's get started!
Think individually for 2 min
Group discussion for 4 min (no PC)
Group discussion for 4 min (with PC)
Presentation prep for 4 min
Presenting to another group for 6 min
Presentation Guidelines
Questions to Ask Yourself:
Presentation Guidelines
Complete the reflection activity in Gradescope and add your groupmates!