v1 Patch Notes - Oct. 9, 5:49 am
hey losers :3 !!! its 5:49 am i didnt check any of my work pls ping me with any errors so i can revise a v2, , ,,, i should just host a website for these… im hungry.. . could go for a warm bowl of soondubu rn. god i have to wake up at 6:30 im cooked time to faint in bed. miku out!! 🛌🌙
v2 Patch Notes - Oct 11, 3:25 am
filled out q1 w/ explanation of prev problems, added to question 2.b, included some formulas, uhhhh i made the soondubu i talked abt in v1 notes lol oh yeah we got an actual website now yippeeeeeeeeeeeeeeeee!!!!!
1) Code analysis like quiz1 or hw1 or any class code analysis
Note From Miku
uhh uhhhhnmmm ermmm she didnt really give problems here… so instead i will show you guys my thought process with the hw ones…
hw1 problem 5.e
what i usually do w these nested loops is find the number of iterations each loop/function has in form. so at the top we have a for loop iterating i
from 0 to n. obviously this makes i
’s iterations .
then, the inner loop iterates j
from 0 to i
. we just determined i to iterate times, so we can square that to say j iterates O(n) times.
then, we have k
looping from 0 to j. since j
to iterates times, this means k
also iterates times.
finally, within the innermost loop is a line, ++sum
. multiply all those numbers together and we get .
careful ‼️
watch out when doing these problems!!! this first example was much easier bc the iterators were only affected by the for loop and nothing else. additionally, the contents of the innermost for loop was O(1), keeping things simple. she will probably give more complex problems on exams and quizzes!! make sure you look thru the code line by line and deeply analyze how certain lines could affect other parts. below ill go over two examples of some things to pay attention for.
hw1 problem 10
questions like hw1 problem 10 modify the iterator values outside of the main for loop iteration. keep the behavior of these in mind and think about how they affect each loop. if we think about what j
does here, it adds 1 to j
every inner for loop iteration; this causes j
to increment by 2 instead of 1, which is still . then, at the end of the inner for loop, i
has increased by n/2 due to the line i++
. This means the outer for loop only runs exactly twice, which is an O(1) operation.
other thing to watch out for the innermost for loop may not contain an O(1) operation! heres one a lot of ppl struggled with:
hw1 problem 5.f
lets start with loop analysis on this one. i
iterates from 1 to n, so that’s . j
iterates from 1 to i
and is thus . the innermost loop iterates k
from 0 to j
, which is again . however, an if statement in the 2nd loop prevents this problem from being identical to problem e. the innermost loop is nested in an if statement which checks if j % i == 0
. this means although j
iterates times, its contents only run every i
times. using this information, we can divide j
’s iterations by i
’s iterations to determine the second loop’s content only runs times, not . we can multiply these values and calculate the runtime as .
2) Solve via recurrence tree, and confirm via substitution.
Formulas!!
Here are some formulas that are super useful for solving geometric series!! ^^
The main formula u should know isWhen , then as approaches we know that
Then, when aka there’s no exponent, then
As a bonus heres Masters Theorem. Makes lots of problems so easy :D
For ,if then the recurrence is .
if then the recurrence is .
if then the recurrence is .
T(n)=27T(n/3)+5n^3
Work:
Depth: Largest recursive constant is 1/3.
Substitution
Hypothesis:
Assuming for , for great enough we can use this assumption to determine that . Thus, we know that
From this, we want to prove that
We can divide by to rewrite the inequality as
Logarithm rules further simplify this inequality to
By cancelling out and treating the log base as 3 we can solve for and get
Thus, is proven by definition of big O as for and
T(n)=27T(n/3)+5n^2
Work:
Depth: Largest recursive constant is 1/3.
Substitution
disclaimer!!
The first hypothesis below does not successfully prove the recurrence through substitution. However, I’m still showing its process so you can understand why it fails, recognize the issue, and know how to deal with it.
Hypothesis:
Assuming for , for great enough we can use this assumption to determine that . Thus, we know that
From this, we want to prove that
We can cancel out from both sides and end with
ermmmm what the flip!!
This inequality can never hold true for any non-zero values of ! Despite this, a failed substitution proof does not indicate an incorrect hypothesis. Sometimes a new hypothesis must be formed where a lower degree term is subtracted from the original term. Below is an application of such, where is subtracted to deal with the .
Hypothesis:
Assuming for , for great enough we can use this assumption to determine that . Thus, we know that
From this, we want to prove that
We can cancel out from both sides and combine like terms to get
This time, we can solve for and get the inequality
As , we have proven through definition of big O as for and .
T(n)=27T(n/3)+5n^4
Work:
Depth: Largest recursive constant is 1/3.
Substitution
Hypothesis:
Assuming for , for great enough we can use this assumption to determine that . Thus, we know that
From this, we want to prove that
We can divide by to rewrite the inequality as
Then by solving for , we get
Thus, is proven by definition of big O as for and .
3) Create a linear algorithm that adds an array of numbers, and analyze it.
Note From Miku
uhhhh we already did this for hw but i think its to set up for problem 4. ill just copy my prev code lol
arraySum(arr):
total = 0
for (i=0 to arr.length-1):
total += arr[i]
return arrTotal
4) Create a divide & conquer algorithm that adds up an array of numbers and analyzes it using the recurrence tree method.
divAndConqSum(arr, start, end):
if start == end: # base case
return arr[start]
middle = floor((start + end) / 2) # MAKE SURE TO ROUND DOWN
a = divAndConqSum(arr, start, middle)
b = divAndConqSum(arr, middle+1, end)
return a + b
Recurrence equation:
Work:
Depth: Largest recursive constant is 1/2.
5) Pseudocode
Note From Miku
we already did all of these for quiz1/hw1. my guess is that it’ll be on the quiz this time?? dont quote me on that im yapping but ima copy my prev hw/quiz answers onto here
Write pseudocode that lists the subsets of an array of ints.
listSubsets(arr):
n = arr.length
subsets = []
for (i=0 to 2^n-1):
subset = []
for (j=0 to n-1):
if i & (1 << j):
subset.push(arr[j])
subsets.push(subset)
return subsets
Write pseudocode finds the smallest number of an array of ints.
findMinimum(arr):
n = arr.length
if !n:
return "Empty Array"
smallest = arr[0]
for (i=1 to n-1):
if arr[i] < smallest:
smallest = arr[i]
return smallest
Write pseudocode to list all possible substrings of a string S.
possibleSubstrings(S):
n = S.length
substrings = []
for (i=0 to n-1):
for (j=i to n-1):
substrings.push(S.substring(i, j))
return substrings
Given you have two sort lists write pseudocode that returns a new merged sorted
mergeSortedLists(list1, list2):
mergedList = []
# initializing iterators
i = j = 0
# we run thru the lists until one of them runs out of elements
while (i < list1.length && j < list2.length):
# compare the smallest elements of both lists, then add
# it to mergedList. increment that list's iterator
if list1[i] < list2[j]:
mergedList.push(list1[i])
i++
else:
mergedList.push(list2[j])
j++
# once one of the lists runs out of elements, add the rest
# of the elements from the other list
while (i < list1.length):
mergedList.push(list1[i])
i++
while (j < list2.length):
mergedList.push(list2[j])
j++
return mergedList