In the Asynchronous Lecture
In the Synchronous Lecture
If you have any questions while watching the pre-recorded material, be sure to write them down and to bring them up during the synchronous portion of the lecture.
The following tabs contain pre-recorded lecture materials for class this week. Please review these materials prior to the synchronous lecture.
Total time: 1 hour and 28 minutes.
# Same algorithm but the readability of the program differs
def alg1(n):
= 0
theSum for i in range(1,n+1):
= theSum + i
theSum return theSum
def alg2(tom):
= 0
fred for bill in range(1,tom+1):
= bill
barney = fred + barney
fred return fred
print(alg1(10))
print(alg2(10))
# Faster algorithm using a closed form solution to the problem
def alg3(n):
return (n*(n+1))/2
print(alg3(10))
f(n) | Name | Notation |
---|---|---|
\(1\) | Constant | \(O(1)\) |
\(log(n)\) | Logarithmic | \(O(log n)\) |
\(n\) | Linear | \(O(n)\) |
\(n log(n)\) | Log Linear | \(O(n log n)\) |
\(n^2\) | Quadratic | \(O(n^2)\) |
\(n^3\) | Cubic | \(O(n^3)\) |
\(2^n\) | Exponential | \(O(2^n)\) |
# %% Constant Time O(1) -----------------------------------------
def fun_constant(n):
print(n)
10)
fun_constant(
# %% Logarithmic Time O(log n) -----------------------------------------
def fun_log(n):
while n > 0:
= int(n/2)
n print(n,end=" ")
10)
fun_log(
# %% Linear Time O(n) -----------------------------------------
def fun_linear(n):
for i in range(n):
print(i,end=" ")
10)
fun_linear(
# %% Log Linear Time O(n log n) -----------------------------------------
def fun_log_lin(n):
for i in range(n):
= i
a while a > 0.1:
= a/2
a print(a)
10)
fun_log_lin(
# %% Quadratic Time O(n^2) -----------------------------------------
def fun_quadratic(n):
for i in range(n):
for j in range(n):
print( (i,j))
10)
fun_quadratic(
# %% Cubic Time O(n^3) -----------------------------------------
def fun_cubic(n):
for i in range(n):
for j in range(n):
for k in range(n):
print( (i,j,k))
10) fun_cubic(
# %% Sequential Search -----------------------------------------
# Input:
= ["Madeline","Yousuf","Justine","Ella","Ruyi"]
students = "Eric"
value
# Output: True/False
# Sequentially compare every item in the list
0] == value
students[1] == value
students[# ...
4] == value
students[
# A function that accomplishes sequential comparisosn
def sequential_search(data,value):
for d in data:
if d == value:
return True
return False
# Test
"Yousuf")
sequential_search(students,"Eric")
sequential_search(students,
# What is the best case run time? O(1)
# What is the worst case run time? O(N)
# %% Binary Search -----------------------------------------
= [3,7,55,2,90,10,25,33]
numbers # List must be sorted. Best Run time for a sorting alg. is O(n log n)
numbers.sort()
numbers
# The Gist of Binary Search
= 3
item = 0
first = 7
last
numbers[first]
numbers[last]
= (first + last)//2
midpoint == item
numbers[midpoint] > numbers[midpoint]
item < numbers[midpoint]
item = midpoint -1
last
= (first + last)//2
midpoint == item # Found it!
numbers[midpoint]
# Written up as an algorithm
def binarySearch(alist, item):
= 0
first = len(alist)-1
last = False
found while first<=last and not found:
= (first + last)//2
midpoint
if alist[midpoint] == item:
= True
found
else:
if item < alist[midpoint]:
= midpoint-1
last else:
= midpoint+1
first return found
# Implement the Algorithm
numbers90)
binarySearch(numbers,
# What is the best case run time? O(1)
# What is the worst case runtime? O(log n)
# %% Hashing -----------------------------------------
= [None]*11
hash_table
hash_table
= [33,51,23,97,13]
numbers
numbers
4]%11
numbers[
# F(item) -> pos in table
# %% Hash Function -----------------------------------------
def hash_function(item):
'''remainder hash function'''
return item%11
for i in numbers:
= hash_function(i)
ind = i
hash_table[ind]
hash_table
13)
hash_function(2]
hash_table[
# Look up in O(1)
# %% More complicated hash function -----------------------------------
= ["Madeline","Yousuf","Justine","Ella","Ruyi"]
students
= ['']*11
hash_table
ord("E") + ord("l") +ord("l") +ord("a")
381%11
list("Ella")
def hash_func2(name):
= 0
score for i in list(name):
+= ord(i)
score return score%11
"Ruyi")
hash_func2(
for student in students:
= hash_func2(student)
ind = student
hash_table[ind]
hash_table
"Madeline")
hash_func2("Ruyi")
hash_func2(
# %% Collision Resolution -----------------------------------------
# When the hash function stores values in the same location, we get a "collision"
= [None]*11
hash_table
# One way to deal with a collision is to store multiple values in the same
# location and then iterate through them when looking a value up.
for student in students:
= hash_func2(student)
ind if hash_table[ind] is None:
= [student]
hash_table[ind] else:
hash_table[ind].append(student)
# Run time for instances with a collision is O(m) where m is the number of items
# in the name location. That is, we have constant time for looking the value up
# and then it's a sequential search from there.
The following survey asks you quick questions regarding the usefulness of the asynchronous lecture materials. Feedback will be used to modify aspect of the asynchronous materials moving forward.
These exercises are designed to help you reinforce your grasp of the concepts covered in the asynchronous lecture material.
Give the Big-O performance of the following code fragment:
= n
i while i > 0:
= 2 + 2
k = i // 2 i
\(O(log N)\)
Give the Big-O performance of the following code fragment:
for i in range(n):
for j in range(n):
for k in range(n):
= 2 + 2 k
\(O(N^3)\)
Give the Big-O performance of the following code fragment:
for i in range(n):
= 2 + 2
k for j in range(n):
= 2 + 2
k for k in range(n):
= 2 + 2 k
\(O(N)\)
The following materials were generated for students enrolled in PPOL564. Please do not distribute without permission.
ed769@georgetown.edu | www.ericdunford.com