Section 1
CA : [Name of the CA]
1
Section 1 agenda
2
What is pseudocode?
3
What is a pseudocode
What is a good pseudocode?
Example: Light Bulbs and Lost Socket
Siggi used to have n pairs of light bulbs and their matching sockets with distinct sizes such that each light bulb would only match exactly one of the n sockets and vice versa.
But Siggi lost all their sockets while they were moving from one dorm room to another. Lucky found a socket in the hallway and gave it to Siggi with the hopes that it’s one of the lost sockets.
Siggi is extremely organized, so they have already ordered their light bulbs based on their base sizes from small to big. Help Siggi find the matching light bulb with the found socket.
Sample Pseudocodes
find_matching_light_bulb (LightBulbs, Socket)
search the light bulbs and return the matching lightbulb with the socket
find_matching_light_bulb (LightBulbs, Socket)
search the light bulbs and return the matching lightbulb with the socket
find_matching_light_bulb (lightBulbs, socket)
for lightBulb in lightBulbs:
if lightBulb and socket match: return lightBulb
return no matching light bulb found
For the sake of this example, assume the following implementation of binary search was given in the lecture.
find_matching_light_bulb (LightBulbs, Socket)
run binary_search to find the matching light bulb.
find_matching_light_bulb (LightBulbs, Socket)
run binary_search to find the matching light bulb.
find_matching_light_bulb (lightBulbs, socket)
ind=modified_binary_search(lightBulbs,
socket)
if lightBulbs[ind] matches with socket:
return lightBulbs[ind]
else:
return no matching light bulb found
find_matching_light_bulb (lightBulbs, socket)
if the 1st light bulb matches the socket:
return the first light bulb
if the 2nd light bulb matches the socket:
return the 2nd light bulb
if the 3rd light bulb matches the socket:
return the 3rd light bulb
.
.
.
if the nth light bulb matches the socket:
return the nth light bulb
return no matching light bulb found
find_matching_light_bulb (lightBulbs, socket)
for lightBulb in lightBulbs:
if lightBulb and socket match: return lightBulb
return no matching light bulb found
find_matching_light_bulb (lightBulbs, socket)
if the 1st light bulb matches the socket:
return the first light bulb
if the 2nd light bulb matches the socket:
return the 2nd light bulb
if the 3rd light bulb matches the socket:
return the 3rd light bulb
.
.
.
if the nth light bulb matches the socket:
return the nth light bulb
return no matching light bulb found
Divide-and-Conquer
15
Divide and conquer
Break problem up into smaller (easier) sub-problems
Big problem
Smaller problem
Smaller problem
Yet smaller problem
Yet smaller problem
Yet smaller problem
Yet smaller problem
[Lecture 1: Slide 76]
Divide and conquer for multiplication
1
2
3
4
One n-digit multiply
Four (n/2)-digit multiplies
Break up an n-digit integer:
Suppose n is even
There are n2 1-digit problems
1 problem
of size n
4 problems
of size n/2
4t problems
of size n/2t
____ problems
of size 1
…
…
Note: this is just a cartoon – I’m not going to draw all 4t circles!
Do Better: Karatsuba integer multiplication
ad + bc = (a+b)(c+d) - ac - bd
What’s the running time?
1 problem
of size n
3 problems
of size n/2
3t problems
of size n/2t
____ problems
of size 1
…
We aren’t accounting for the work at the higher levels! But we’ll see later that this turns out to be okay.
Note: this is just a cartoon – I’m not going to draw all 3t circles!
…
MergeSort
Big problem
Smaller problem
Smaller problem
Yet smaller problem
Yet smaller problem
Yet smaller problem
Yet smaller problem
MergeSort Pseudocode
MERGESORT(A):
If A has length 1,
It is already sorted!
Sort the right half
Sort the left half
Merge the two halves
[Lecture 2: Slide 63]
Merge sort in pictures
6
4
3
8
1
5
2
7
6
4
3
8
1
5
2
7
6
4
3
8
1
5
2
7
6
4
3
8
1
5
2
7
This array of length 1 is sorted!
Merge sort in pictures
6
4
3
8
1
5
2
7
1
2
5
7
3
4
6
8
1
2
3
4
5
6
7
8
Merge!
Merge!
Merge!
Merge!
Merge!
Merge!
Merge!
4
3
8
1
5
2
7
6
A bunch of sorted lists of length 1 (in the order of the original sequence).
Sorted sequence!
InsertionSort
InsertionSort �example
4
6
3
8
5
6
4
3
8
5
6
4
3
8
5
4
3
6
8
5
4
3
6
8
5
4
3
6
8
5
4
3
6
8
5
4
3
5
6
8
Start by moving A[1] toward the beginning of the list until you find something smaller (or can’t go any further):
Then move A[2]:
Then move A[3]:
Then move A[4]:
Then we are done!
4
6
3
8
5
[Lecture 2: Slide 13]
Insertion Sort (the actual algorithm)
Remember our two questions:
def InsertionSort(A):
for i in range(1,len(A)):
current = A[i]
j = i-1
while j >= 0 and A[j] > current:
A[j+1] = A[j]
j -= 1
A[j+1] = current
6
4
3
8
5
4
3
6
8
5
Asymptotic analysis
Aka big-Oh notation
28
In this class we will use…
29
Main idea:
Focus on how the runtime scales with n (the input size).
Number of operations | Asymptotic Running Time |
| |
| |
| |
| |
We say this algorithm is “asymptotically faster” than the others.
(Only pay attention to the largest function of n that appears.)
Some examples…
30
Informal definition for O(…)
Here, “constant” means “some number that doesn’t depend on n.”
pronounced “big-oh of …” or sometimes “oh of …”
31
T(n) = 2n2 + 10
g(n) = n2
32
T(n) = 2n2 + 10
g(n) = n2
3g(n) = 3n2
33
T(n) = 2n2 + 10
g(n) = n2
3g(n) = 3n2
n0=4
34
Formal definition of O(…)
“There exists”
“For all”
“such that”
“If and only if”
35
T(n) = 2n2 + 10
g(n) = n2
36
T(n) = 2n2 + 10
g(n) = n2
3g(n) = 3n2
(c=3)
37
T(n) = 2n2 + 10
g(n) = n2
3g(n) = 3n2
n0=4
(c=3)
38
Formally:
T(n) = 2n2 + 10
g(n) = n2
3g(n) = 3n2
n0=4
39
Formally:
T(n) = 2n2 + 10
g(n) = n2
7g(n) = 7n2
n0=2
There is not a “correct” choice of c and n0
40
g(n) = n2
T(n) = n
41
Ω(…) means a lower bound
Switched these!!
42
g(n)/3 = n
T(n) = nlog(n)
g(n) = 3n
43
Θ(…) means both!
44
45
Take-away from examples
46
Back to Insertion Sort
Seems plausible
Go back to the pseudocode and convince yourself of this!
47
Merge Sort: running time
Size n
n/2
n/2
n/4
(Size 1)
…
n/4
n/4
n/4
n/2t
n/2t
n/2t
n/2t
n/2t
n/2t
…
sub-problem each takes O(n/2t)
Level 0
Level 1
Level t
Level log(n)
2t subproblems at level t.
48
Total runtime…
Asymptotically better than Insertion Sort!
49
Any questions?