10:00 - 1PM Introduction
Defining an algorithm
Data Structures
Goal of algorithms
Time/space complexity
1:00 - 2:00 Lunch
2:00 - 5:00 Searching
Sorting
Binary Search
Algorithms in Real Life
Conclusion
"A repeatable process for determining the solution to a problem."
Turn on the TV
Rate all channels
ORRate top channels
A way to store data
(so that it can be used as desired)
A good way to approach an algorithm is to think about the data structures that you know.
What are their properties, and how can they be used to your advantage?
myObject = { key : value,
key : value,
key : value }
Linked List
Tree
Graph
Queue
Write down steps for several versions of an algorithm to solve an everyday problem
Ideas:
Designing algorithms is about making decisions. Decisions have tradeoffs.
You may want to optimize to complete the task:
How do you know if your algorithm is good?
How do you compare algorithms?
The less complex the better!
Saves time, but requires you to have a large car
Lower time complexity
Higher space complexity
Takes a long time, but doesn't require a car
Higher time complexity
Lower space complexity
How long does an algorithm take?
laundry.drop()
How long does the algorithm take?
How long does the algorithm take?
Dumping out my laundry takes 2 seconds
#items | #seconds |
---|---|
4 | 2 |
8 | 2 |
16 | 2 |
If N = items of clothing,
time it takes: (N*0 + 2)
→ O(2)
→ O(1)
Constant time
piles = {'shirts': [], 'socks': []}
for clothing_item in laundry:
piles[clothing_item.type].append(clothing_item)
for pile in piles:
for clothing_item in pile:
clothing_item.hang_up()
How long does the algorithm take?
Putting an item in a pile takes 10 seconds.
Hanging an item up takes 30 seconds.
#items | #seconds |
---|---|
4 | (4*10 + 4*30) = 160 |
8 | (8*10 + 8*30) = 320 |
16 | (16*10 + 16*30) = 640 |
If N = items of clothing,
time it takes: (N*10 + N*30)
→ O(40N)
→ O(N)
Linear time
Addition: We sort our clothes as we hang them up in the closet.
piles = {'shirts': [], 'socks': []}
closet = {'sock_drawer': [], 'shirt_section': []}
for clothing_item in laundry:
piles[clothing_item.type].append(clothing_item)
for pile in piles:
for clothing_item in pile:
section = closet[clothing_item.closet_section]
for hung_clothing in section:
if clothing_item.color > hung_clothing.color:
clothing_item.hang_up()
Now we must loop through closet section items
each time we hang up an item.
Each look at an item takes 2 seconds.
items | section | items | seconds |
---|---|---|---|
4 | 4 | (4*10 + 4*30 + 4*4*2) | 192 |
8 | 8 | (8*10 + 8*30 + 8*8*2) | 448 |
1024 | 1024 | (1024*10 + 1024*30 + 1024*1024*2) | 2138112 |
If N = # items of clothing and # items in section,
time it takes: (N*10 + N*30 + N*N*2)
→ O(40N + 2N2)
→ O(2N2)
→ O(N2)
Quadratic time
Order of growth | Name | Description | Example |
---|---|---|---|
1 | constant | statement | add two numbers |
logN | logarithmic | divide in half | binary search |
N | linear | loop | find the maximum |
NlogN | linearithmic | divide + conquer | merge sort |
N2 | quadratic | double loop | check all pairs |
N3 | cubic | triple loop | check all triples |
2N | exponential | exhaustive search | check all subsets |
skipping to the front of a line
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
waiting in a line
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
skipping half of a line
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
sorting your books in alphabetical order
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
looking up an element in an array
var things = ["raindrops", "roses", "whiskers", "kittens"]
things[2]
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
iterating over an array
var things = ["raindrops", "roses", "whiskers", "kittens"]
for (var i = 0; i < things.length; i++) {
things[i]
}
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
making every pair combination of items in array
var things = ["raindrops", "roses", "whiskers", "kittens"]
for (var i = 0; i < things.length; i++) {
for (var j = i + 1; j < things.length; j++) {
things[i] + things[j]
}
}
// raindrops + roses
// raindrops + whiskers
// raindrops + kittens
// roses + whiskers
// roses + kittens
// whiskers + kittens
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
The size of the cart grows as the number of items on the list grows.
If N is the number of items on the list, then the cart array needs to be size N.
The space complexity is O(N)
If N is the number of items on the list, then the cart array needs to be size 1.
The space complexity is O(1) or constant.
What's the time requirement of your algorithm?
What's the space requirement of your algorithm?
Write two algorithms for making crepes, one that is time efficient and one that is space efficient.
Pseudocode first then whiteboard code if you have time.
Find an item with a particular value in a sorted sequence.
Find 'J' in sequence:
[4, 5, J, 10, Q, K, A]
J is the 3rd element (index 2).
Find an item by repeatedly halving the search space.
To find the index of element e with a certain value:
What factors determine time?
N = number of items in sequence.
Since algorithm splits array in half every time, at most log2N steps are performed.
Performance varies depending on sequence characteristics (distribution)
Why so important?
You do it all the time in real life
They're not in perfect order, but in a way that makes it easier for you to search for things
Many sorting algorithms for many types of data
There are lots of different types of data computers need to sort, just like in the real world
What is the best case?
What is the worst case?
They are the same! No matter what, selection sort has a time complexity of O(N^2)
Only requires 1 extra temporary variable. O(1)
All differ in performance, and performance often depends on data characteristics.
Wikipedia: Comparison of algorithm performance, Sorting Algorithms Visualization
A great resource to help visualize the different sorting algorithms:
Visualgo.netWhen is it best?
When is it best?
When is it best?
When is it best?
I focused on which type of data is good or bad for each sorting example, but each sorting algorithm also has its own space trade offs as well.
Write a series of algorithms to suggest friends to facebook users. Remember to write an MVP algorithm first.
Don't feel like you have to copy what facebook does, just write an algorithm that gets the job done.
Not just one algorithm, but many.
If you buy something in category A, recommend EVERYTHING ELSE in category A.
They reflect the biases of the people who build them and of the society of where they were built
What did you learn today?
What surprised you?
You are google. You have lots of street view images and you want to correlate them with addresses.
How do you 'read' all of the house numers?
Remember:
Computers are terrible at reading distorted text in images.
That's why captchas (Completely Automated Public Turing test to tell Computers and Humans Apart) work.
Mechanical Turk: The use of human intelligence to perform tasks that computers are currently unable to do.
Sometimes humans are the solution