03.Searching
03.Searching
Last
wee
kend
… Welcome!
https://hacknroll2023.devpost.com/project-gallery
is open
How to Search!
Algorithm Analysis
– Big-O Notation
– Model of computation
Searching
Peak Finding
– 1-dimension
– 2-dimensions
Admin
Archipelago
– With some browsers/devices don’t redirect properly from
the link.
– So… 404 error
– No workaround yet… try different browser/device?
Coursemology
– Safari V.14:
– Misplaced title bar
– Cannot access assignment 2.
– Use different browser or upgrade safari.
Admin
Tutorial Availability
Much availability: Overfull:
Wed. 3-5pm Tues. 12-2pm
Wed. 4-6pm Tues. 2-4pm
Recitation Availability
Availability: Overfull:
Thurs. 2-3pm Thurs. 1-2pm
Thurs. 3-4pm
Fri. 11-12pm
Some availability:
Thur. 12-1pm
Fri. 1-2pm
How to Search!
Algorithm Analysis
– Big-O Notation
– Model of computation
Searching
Peak Finding
– 1-dimension
– 2-dimensions
Algorithm Analysis
Warm up: which takes longer?
void pushAll(int k) { void pushAdd(int k) {
for (int i=0; for (int i=0; i<= k; i++)
i<= 100*k; {
i++) for (int j=0; j<= k; j++){
{ stack.push(i+j);
stack.push(i); }
} }
} }
On Zoom
Algorithm Analysis
Warm up: which takes longer?
void pushAll(int k) { void pushAdd(int k) {
for (int i=0; for (int i=0; i<= k; i++)
i<= 100*k; {
i++) for (int j=0; j<= k; j++){
{ stack.push(i+j);
stack.push(i); }
} }
} }
T (0) = 0 T (0) = 0
Time
T(n)
n
Big-O Notation
T(n) ≤ c f (n)
Big-O Notation
T(n)
n
Big-O Notation
T(n) = O(f(n))
c f(n)
T(n)
n=n0 n
Big-O Notation
c f(n)
T(n) = O(f(n))
T(n)
n=n0 n
Big-O Notation
T(n) big-O
Not
T(n) = n2 T(n) ≠ O(n) tight
T(n) ≤ c f (n)
Big-O Notation as Upper Bound
f(n)
T(n) = O(f(n))
Time
Namely, T(n) cannot be faster
than O(f (n))
T(n)
n
How about Lower bound?
How about Lower bound?
Time
f(n)
n=n0
n
Big-O Notation
T(n) ≥ c f(n)
Example
T(n) Asymptotic
Exercise:
True or false:
“f (n) = O(g(n)) if and only if g (n) = W(f (n))”
Prove that your claim is correct using the definitions
of O and W or by giving an example.
Big-O Notation
T(n) = Q(f(n))
c1 f(n)
T(n)
c2 f(n)
n
Example
T(n) big-O
Rules:
If T(n) is a polynomial of degree k then:
T(n) = O(nk)
Example:
10n5 + 50n3 + 10n + 17 = O(n5)
Big-O Notation
Rules:
If T(n) = O(f(n)) and S(n) = O(g(n)) then
Example:
10n2 = O(n2)
5nlog(n) = O(nlog(n))
10n2 + 5nlog(n) = O(n2 + nlog(n)) = O(n2)
Big-O Notation
Rules:
If T(n) = O(f (n)) and S(n) = O(g(n)) then:
Example:
10n2 = O(n2)
5n = O(n)
(10n2)(5n) = 50n3 = O(n*n2) = O(n3)
Why don’t you try a few?
is open
4n2log(n) + 8n + 16 = ?
1. O(log n)
2. O(n)
3. O(nlog n)
4. O(n2log n)
5. O(2n)
22n + 2n + 2 =
1. O(n)
2. O(n6)
3. O(2n)
4. O(22n)
5. O(nn)
log(n!) =
1. O(log n)
2. O(n)
3. O(n log n)
4. O(n2)
5. O(2n)
log(n!) =
1. O(log n)
2. O(n)
3. O(n log n)
4. O(n2)
5. O(2n)
Sequential Computer
– One thing at a time
int total=0;
1 assignment
for (int i=0; i<= k; i++){ k+1 comparisons
total = total + intArray[i]; k increments
}
k array access
return total; k addition
} k assignment
1 return
int fib(int n) {
if (n <= 1)
return n;
else
return fib(n-1) + fib(n-2);
}
What is the running time?
Algorithm Analysis
– Big-O Notation
– Model of computation
Searching
Peak Finding
– 1-dimension
– 2-dimensions
(Courtesy: MIT Puzzle Hunt, 2019)
Puzzle of the Week: Bubbly
• Two player game
• Players alternate popping bobbles
• The last player to pop a bubble wins.
Algorithm Analysis
– Big-O Notation
– Model of computation
Searching
Peak Finding
– 1-dimension
– 2-dimensions
Binary Search
Sorted array: A[0..n-1]
Reduce-and-Conquer:
– Start with n elements to search.
– Eliminate half of them.
– End with n/2 elements to search.
– Repeat.
How hard is binary search?
is open
Jon Bentley
Jon Bentley
How hard is binary search?
Search(A, key, n)
begin = 0 is open
end = n
while begin != end do:
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[begin]
Bug 1
Sorted array: A[0..n-1]
Search(A, key, n)
begin = 0
array out of bounds!
end = n
while begin != end do:
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end]
Bug 1
Sorted array: A[0..n-1]
Search(A, key, n)
array out of bounds!
begin = 0 (Can’t happen because of other bugs…)
end = n
while begin != end do:
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end]
Bug 1
Sorted array: A[0..n-1]
Search(A, key, n)
begin = 0
end = n-1
while begin != end do:
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end]
Bug 2 Example: search(7)
• begin = 0, end = 1
Sorted array:• A[0..n-1]
mid = (0+1)/2 = 0
• key >= A[mid] è begin = 0
5 10
Search(A, key, n)
begin = 0 May not terminate!
end = n-1
while begin != end do: round down
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end]
Bug 2 Example: search(2)
• begin = 0, end = 1
Sorted array:• A[0..n-1]
mid = (0+1)/2 = 0
• key < A[mid] è end = 0 – 1 = -1
5 10
Search(A, key, n)
begin = 0 end < begin
end = n-1
while begin != end do: subtract?
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end]
Bug 3
Sorted array: A[0..n-1]
Search(A, key, n)
begin = 0
end = n-1
while begin != end do:
if key < A[(begin+end)/2] then
end = (begin+end)/2 – 1
else begin = (begin+end)/2
return A[end] Useful return value?
Binary Search
Specification:
– Returns element if it is in the array.
– Returns “null” if it is not in the array.
Alternate Specification:
– Returns index if it is in the array.
– Returns -1 if it is not in the array.
Binary Search
Sorted array: A[0..n-1]
end = n-1
while begin < end do:
if key <= A[(begin+end)/2] then
end = (begin+end)/2
else begin = 1+(begin+end)/2
return (A[begin]==key) ? begin : -1
Binary Search
Sorted array: A[0..n-1]
Precondition:
– Fact that is true when the function begins.
– Usually important for it to work correctly.
Postcondition:
– Fact that is true when the function ends.
– Usually useful to show that the computation was
done correctly.
Binary Search
is open
Sorted array: A[0..n-1]
– Array is sorted
Binary Search
Functionality:
– If element is in the array, return index of element.
– If element is not in array, return -1.
Preconditions:
– Array is of size n
– Array is sorted
Should we do input
validation to make sure
array is sorted??
or on Zoom.
Binary Search
Functionality:
– If element is in the array, return index of element.
– If element is not in array, return -1.
Preconditions:
– Array is of size n
– Array is sorted
Should we do input
validation to make sure
array is sorted??
NO! Too slow!
Binary Search
Functionality:
– If element is in the array, return index of element.
– If element is not in array, return -1.
Preconditions:
– Array is of size n
– Array is sorted
Postcondition:
– If element is in the array: A[begin] = key
Invariants
Invariant:
– relationship between variables that is always true.
Invariants
Invariant:
– relationship between variables that is always true.
Loop Invariant:
– relationship between variables that is true at the
beginning (or end) of each iteration of a loop.
Binary Search
is open
Sorted array: A[0..n-1]
Interpretation:
– The key is in the range of the array
begin end
Binary Search
Loop invariant:
– A[begin] £ key £ A[end]
Interpretation:
– The key is in the range of the array
n/2
n/4
n/8
Binary Search
Sorted array: A[0..n-1]
n/2k = 1 è k = log(n)
Key Invariants:
Correctness:
– A[begin] £ key £ A[end]
Performance:
– (end-begin) <= n/2k in iteration k.
Binary Search
Sorted array: A[0..n-1]
T2
T3
Tutorials
(in order T4
of tutor
preference)
T5
T6
T7
T8
A problem…
Tutorial allocation
T1 Students want
certain tutorials.
T2
T3
Tutorials
(in order T4
of tutor
preference)
T5
T6
T7
T8
A problem…
Tutorial allocation
T1 Students want
certain tutorials.
T2
T3 We want each
Tutorials tutorial to have
(in order T4
of tutor < 18 students..
preference)
T5
T6
T7
T8
A problem…
Tutorial allocation
T1 Students want
certain tutorials.
T2
T3 We want each
Tutorials tutorial to have
(in order T4
of tutor < 18 students..
preference)
T5
T8
A problem…
Tutorial allocation
T1 Students want
certain tutorials.
T2
T3 We want each
Tutorials tutorial to have
(in order T4
of tutor < 18 students..
preference)
T5
T8
A problem…
Tutorial allocation
T1 Can we do
greedy allocation?
T2
First, fill T1.
T3 Then fill T2.
Tutorials Then fill T3.
(in order T4 …
of tutor
preference) Stop when all
T5 students are
allocated
T6
T7
T8 or on Zoom.
A problem…
Tutorial allocation
T1 Can we do
greedy allocation?
T2
NO
T3
Tutorials Assume max
(in order T4 tutorial size is 1.
of tutor
preference)
T5 Assign student 1 to
tutorial 1.
T6
Now we need all 8
T7 tutorials.
T8
A problem…
Tutorial allocation
T1 Can we do
greedy allocation?
T2
NO
T3
Tutorials Assume max
(in order T4 tutorial size is 1.
of tutor
preference)
T5 Assign student 1 to
tutorial 1.
T6
Now one student
T7 has no feasible
allocation!
T8
A problem…
Tutorial allocation
T1 Assume we can
solve allocation
T2 problem:
T6
T7
T8
A problem…
Tutorial allocation
T1 Observation:
T2 Number of
students in
T3 BIGGEST tutorial
Tutorials only decreases as
(in order T4 number of tutorials
of tutor
preference) increases.
T5
T6 Monotonic
function of
T7 number of
tutorials!
T8
A problem…
Tutorial allocation
T1
Monotonic
T2
function of
number of
T3
tutorials!
Tutorials
(in order T4
of tutor
preference)
T5
T6 Binary Search
T7
T8
A problem… T1
T2
Tutorial allocation T3
T4
Solution: T5
T6
Binary Search
T7
Define:
T8
T2
T3
T
4
crowded tutorial, T
6
if we offer x tutorials. T7
Search(n) T
8
begin = 0
end = n-1
while begin < end do:
mid = begin + (end-begin)/2;
if MaxStudents(mid) <= 18 then
end = mid
else begin = mid+1
return begin
Binary Search
Sorted array: A[0..n-1]
Algorithm Analysis
– Big-O Notation
– Model of computation
Searching
Peak Finding
– 1-dimension
– 2-dimensions