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 - Linear & Binary
Sorting
Algorithms in Real Life
Conclusion
"A repeatable process for determining
the solution to a problem."
1. Turn on the TV
OR2. Rate all channels
OR3. Rate top channels
Write down steps for several versions of
an algorithm to solve an everyday problem
Ideas:
(Just good to know; we won't be covering extensively!)
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?
How can these be used to your advantage?
myObject = {
key : value,
key : value,
key : value
}
Linked List
Tree
Graph
Queue
Designing algorithms is about making decisions.
All 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?
Dumping out laundry takes 2 seconds.
#items | #seconds |
---|---|
4 | 2 |
8 | 2 |
16 | 2 |
N = items of clothing
Time it takes: (N*0 + 2)
→ O(2)
→ O(1)
Constant time
const piles = { 'shirts': [], 'socks': [], 'pants': [] }
laundry.drop()
for (item in laundry) {
piles[item.type].push(item)
}
for (pile in piles) {
for (item in pile) {
item.hang_up()
}
}
How long does the algorithm take?
Dumping out laundry takes 2 seconds.
Putting an item in a pile takes 10 seconds.
Hanging an item up takes 30 seconds.
#items | #seconds |
---|---|
4 | 2 + (4*10 + 4*30) = 160 |
8 | 2 + (8*10 + 8*30) = 320 |
16 | 2 + (16*10 + 16*30) = 640 |
N = items of clothing
Time it takes: 2 + (N*10 + N*30)
→ O(2 + 40N)
→ O(40N)
→ O(N)
Linear time
pants_pile = ['jeans', 'overalls', 'leggings', ..., 'cutoffs'] // N items
shirts_pile = ['t-shirt', 'blouse', 'crop-top', ..., 'tank'] // N items
outfits = []
laundry.drop()
laundry.sort()
for (shirt in shirts_pile) {
for (pants in pants_pile) {
outfits.push([shirt, pants])
}
}
We must match each shirt with each pair of pants.
Dumping out laundry takes 2 seconds.
Putting an item in a pile takes 10 seconds.
Looking at each item takes 2 seconds.
pants | shirts | calculation | seconds |
---|---|---|---|
4 | 4 | 2 + (4*10) + (4*2)*4 | 74 |
8 | 8 | 2 + (8*10) + (8*2)*8 | 210 |
1024 | 1024 | 2 + (1024*10) + (1024*2)*1024 | 2107394 |
N = # items of clothing
Time it takes: 2 + (N*10) + (N*2)*N
→ O(2 + 10N + 2N2)
→ O(10N + 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
looking up an element in an array
var things = ["raindrops", "roses", "whiskers", "kittens"]
console.log(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++) {
console.log(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++) {
console.log(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 more items on the list,
the bigger the bag needed.
If N is the number of items on the list, then the bag array needs to be size N.
The space complexity is:
O(N) => linear space
No matter how many items are on the list, the bag only holds 1 item.
If N is the number of items on the list,
then the bag array needs to be size 1.
The space complexity is:
O(1) => constant space
Refer back to your algorithm from your exercise
What's the time requirement of your algorithm?
What's the space requirement of your algorithm?
Write two algorithms to make N servings of pancakes,
one that is time efficient and one that is space efficient.
Pseudocode each step first,
then code it if you have time.
A search algorithm is an algorithm which solves the problem of retrieving stored information.
Wikipedia - Search AlgorithmsHow do you solve the problem of
searching data numbering in the billions?
One of the simplest searches.
Find an item with a particular value in a sequence.
Find 'J' in the following sequence:
[4, 5, J, 10, Q, K, A]
J is the 3rd element (index 2).
What did we do to create our linear search algorithm?
Find an item by repeatedly halving the search space.
To find the index of element e with a certain value:
What did we do to create our binary search algorithm?
What factors determine time?
N = number of items in sequence.
Since binary search algorithm splits array in half
every time, at most log2N steps are performed.
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 that
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(N2).
What is the best case?
What is the worst case?
They are the same! No matter what,
it only requires 1 variable, for a space complexity of O(1).
All differ in performance, and performance often depends on data characteristics.
Great resources to help visualize
the different sorting algorithms:
Example: You built and now run a calendar app.
People add events one at a time, & you need to sort em.
Example: You work at a 100k person company and
want to create a list of birthdays so everyone can be celebrated each month. Year doesn't matter, just date.
Example: Two companies are merging and they each had different ways to calculate their hierarchies. They want to create a new seniority list based on hire dates.
When is it best?
Write a series of algorithms to suggest friends to Facebook users. 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.