Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Tell us about yourself.
"A repeatable process for determining the solution to a problem."
Some problems that we can create algorithms for (not necessarily related to computers)
Turn on the TV
Rate all channels
ORRate top channels
Find a partner and pick a problem. Come up with multiple versions of an algorithm and write them down.
Ideas:
Designing algorithms is about making decisions. Decisions have trade-offs.
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 |
How do we talk about these times more generally for computers?
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
Constant time complexity
O(1) - Big O of 1
where n is the number of pieces of laundry
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
Linear time complexity
O(n) or Big O of N
where n is the number of pieces of laundry
Comparing constant and linear time complexity
where n is the number of pieces of laundry
Making all possible outfit combinations.
For simplicity's sake, assume there are an equal amount of pants and shirts.
pants_pile = ['blue-jeans', 'black-jeans', 'dress-pants', ..., 'white-pants'] // (n items)
shirts_pile = ['white-shirt', 'blue-blouse', ..., 'stripe-shirt'] // (n items)
outfits = []
for shirt in shirts_pile:
for pants in pants_pile:
outfits.append([shirt, pants])
We must match each shirt with each pair of pants.
Looking at each item takes 2 seconds.
pants | shirts | calculation | seconds |
---|---|---|---|
4 | 4 | (4*2*4*2) | 64 |
8 | 8 | (8*2*8*2) | 256 |
1024 | 1024 | (1024*2*1024*2) | 4194304 |
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
We're going to determine the time complexity of some algorithms
Wait times: skipping to the front of a line
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
Wait times: waiting in a line
O(1) - Constant
O(n) - Linear
O(n2) - Quadratic
Wait times: 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
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?
We're going to determine the time complexity of some algorithms
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)
Linear space complexity
O(n) or Big O of N
where n is the number of items on the grocery list
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.
Constant space complexity
O(1) or Big O of one
where n is the number of items on the grocery list
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 for making crepes, one that is time efficient and one that is space efficient.
Pseudocode first then whiteboard code if you have 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 |
Linked List
Tree
Graph
Queue