CSP '22-23 Unit 6 - Algorithms
CSP '22-23 Unit 6 - Algorithms
Prompt:
Go around the room and write down your answers to these problems.
Problems
1. Find a person whose birthday is before yours
2. Find a person whose birthday is after yours
3. Find the person whose birthday is the closest before yours
4. Find the person whose birthday is the closest after yours
5. Find the person whose birthday is closest to yours
6. Find the person with an equal number of birthdays before and after theirs
7. Find the two people with the closest birthdays in the room
8. Find the shortest period of time in which three people have birthdays
9. Find the shortest period of time in which four people have birthdays
10. Find the longest period of time in which no one has a birthday
Unit 6 Lesson 1 - Activity
Prompt:
Share with someone how you went
about solving each of these problems.
Algorithm 2
false true
REPEAT 2 TIMES count < 4 false true
{ count < 2
MOVE_FORWARD() MOVE_FORWARD()
MOVE_FORWARD() End
TURN_RIGHT()
TURN_RIGHT() count ← count + 1
MOVE_FORWARD() MOVE_FORWARD()
End MOVE_FORWARD()
TURN_RIGHT() Algorithm 5
} turnCount ← 0
REPEAT 2 TIMES
Algorithm 3 {
REPEAT 2 TIMES
moves ← [“F”, “R”, “F”, “R”, “F”, “R”, “F”, “R”] {
FOR EACH move IN moves MOVE_FORWARD() false turnCount true
{ } < 3
IF (move = “F”) REPEAT 3 TIMES
{ {
MOVE_FORWARD() TURN_LEFT() MOVE_FORWARD()
} } TURN_RIGHT()
ELSE MOVE_FORWARD() count ← count + TURN_LEFT()
{ REPEAT 3 TIMES 1 turnCount ← turnCount + 1
TURN_RIGHT() {
} TURN_LEFT()
} }
}
Unit 6 Lesson 1 - Activity
Prompt:
Discuss with another group
which of these algorithms are “the
same” as one another?
Problem: a general description of a task that can (or cannot) be solved with an algorithm
Algorithm: a finite set of instructions that accomplish a task.
There are usually many algorithms to solve the same problem, and many ways to write or express
one algorithm including natural language, psuedocode, diagrams, and are implemented using
programming code. All algorithms can be created by combining steps in three different ways.
Prompt:
Prompt:
Algorithm Efficiency
You and your partner should have:
Sticky Notes
Your Journal
AP CSP
Journal
Unit 6 Lesson 2 - Activity
3 Volunteers:
Prompt:
How many steps did it take to find out if anyone
had the winning ticket? What is the greatest
possible number of steps it could take for this
instance?
Unit 6 Lesson 2 - Activity
Prompt:
inputs steps
Instance 3 3 10
9
8
7
6 6
steps
Instance 6
5
4
3
Instance 10 10 2
1
inputs
Unit 6 Lesson 2 - Activity
With a partner…
Do This:
● Use the ticket generator to generate
seven tickets. Write down the numbers
on separate sticky notes.
● Organize the sticky notes in numerical
order.
● Copy one of the numbers on to a 705
separate sticky note. This is the number
you are searching for.
● You can "jump" over sticky notes. In other words, you don't need to search the stickies in order.
● You can determine which sticky notes to search next based on the current sticky note you are
checking.
● The goal is to make the determination in the least steps possible, but don't forget your number
could be anywhere in the list - what is the worst possible case? What is the greatest number of
comparison steps it would take to find any number in your list using your current algorithm?
Unit 6 Lesson 2 - Activity
Do This: Now try this algorithm. See how it compares to your own.
1. Find the middle number in the list. Compare that number to the given
number. If the middle number is less than the given number, remove all of
the cards to the left (including the middle number). If the middle number is
more than the given number, remove all of the cards to the right (including
the middle number).
2. Find the middle number in this shorter list. Follow the instructions in Step
#1 for comparing.
3. Find the middle number in this new shorter list. Follow the instructions in
Step #1 for comparing.
We found the
number! 705 is in 705 705 705
the list. GREATER LESS
compare EQUAL TO!
compare compare
THAN THAN
Do This: Now try the Binary Search algorithm for different instances. We've done a few
for you! Copy the instance table to your journal and plot the points on the graph.
15
inputs steps 14
13
Instance 1 1 12
11
10
Instance 3 2
steps
9
8
Instance 5 3 7
6
5
Instance 7 3 4
3
2
Instance 9 4 1
Instance 15 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
inputs
Unit 6 Lesson 2 - Activity
There's another way of thinking about this. Can you see the pattern?
Instance 5 3
1 1 1
Instance 7 3
And 15?
Instance 9 4
Instance 15 4 1 1 1 1
Unit 6 Lesson 2 - Activity
R
9 happens with Binary Search. Notice
EA
8 how it grows at a much slower rate!
Binary Search is faster than Linear
N
7
search, BUT the data must be
6
5 LI sorted.
4
3
2
1 Binary Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Unit 6 Lesson 2 - Wrap Up
Prompt:
If I had one input, which algorithm would I use
to get my answer with the fewest amount of
steps?
R
the desired value is found or all elements 9
EA
in the list have been checked. 8
N
7
LI
6
Binary Search: a search algorithm that 5
starts at the middle of a sorted set of 4
numbers and removes half of the data; this 3
2
process repeats until the desired value is
found or all elements have been
1 Binary Search
eliminated. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Unit 6 - Lesson 3
Unreasonable Time
Unit 6 Lesson 3 - Warm Up
Prompt:
Do This
Generate a ticket
Silently move around the room.
See if you’re a part of a winning pair!
Unit 6 Lesson 3 - Activity
Do This
Generate a ticket
Move around the room (you can talk this
time
See if you’re part of a winning group!
Unit 6 Lesson 3 - Activity
Prompt
Which raffle felt like it was more difficult to
check? Why?
Unit 6 Lesson 3 - Activity
With your partner fill in the two tables on the activity guide.
Unit 6 Lesson 3 - Activity
9 (n2 - n)/2
8
3 3 7 You don’t need to know that
checks
6 formula, but you should know
5 that because of the
4 “n-squared” term the graph
4 6 3 curves up.
2
1 Any algorithm whose
efficiency includes an n2, n3,
5 10 n4 … is called polynomial.
1 2 3 4 5 6 7 8 9 10
tickets
8 28
Unit 6 Lesson 3 - Activity
9 (2n) - 1
8
3 7 7 You don’t need to know that
checks
6 formula, but you should know
5 that because of the “2 to the
4 n” term the graph curves up
4 15 3
2
very quickly.
tickets
8 255
Unit 6 Lesson 3 - Activity
4 15 1 1 1
5 31
8 255 1 1 1 1
Unit 6 Lesson 3 - Activity
← Sorted Raffle
← Normal Raffle
Discuss with a partner:
← Pair Raffle a. Which of these algorithms run
in a reasonable amount of
← Group Raffle time?
b. Which run in an unreasonable
amount of time?
Unit 6 Lesson 3 - Activity
tickets
Unit 6 Lesson 3 - Activity
Prompt:
Your school is considering running the
group raffle at an upcoming assembly
to give away a prize.
Prompt:
Traveling Salesman
You should have:
Your journal
Pen/pencil
AP CSP
Journal
Unit 6 Lesson 4 - Activity
⌂ ⌂
⌂ ⌂
Unit 6 Lesson 4 - Activity
⌂ ⌂ ⌂ ⌂ ⌂ ⌂
⌂ ⌂ ⌂ ⌂ ⌂ ⌂
Here are a few different paths you might take.
Prompt:
What do you need to know to
determine the best path?
Unit 6 Lesson 4 - Activity
⌂ ⌂ ⌂ ⌂
1
1
1
1
2
⌂ ⌂
1
2
2 1
⌂ ⌂ ⌂ ⌂ 1 1
2
⌂ ⌂
Total: 4 Total: 6 Total: 6
Distance!
Unit 6 Lesson 4 - Activity
For every new place to visit, the number of options for possible paths increases
factorialy.
Factorial fun: n!
Number of houses to Number of steps
visit to check for the
Here's how n! works: "best" path
Problems
Any task that may (or may not) be solved with an algorithm.
Sorting a list is a problem. Sorting the list (2, 3, 1, 7) is an instance of that problem.
Welcome to heuristics!
Do This:
● Navigate to Level 1 on Code Studio
● Try to find the "best" path to visit all
nodes.
● Write down a plan or heuristic for
choosing a good path.
○ Note: your heuristic may not
always find the best path, but it
should be close enough
AP CSP
Journal
Unit 6 Lesson 4 - Activity
Do This:
● Navigate to Level 2 on Code Studio
● Test your heuristic on three different
levels. Distance Distance
● Write down the distance for the path (Heuristic) (Brute Force)
your heuristic finds.
1225 1215
● Try to find the best version not using
the heuristic (brute force). Can you find
a better path? Is your heuristic on
average pretty good? Should you
update your heuristic?
AP CSP
Journal
Unit 6 Lesson 4 - Activity
Prompt:
Share Out:
9
Sample Heuristic: 10
8
11
At each node, travel to 7
12
the next closest node 6
5
2
3
Unit 6 Lesson 4 - Activity
Takeaways:
The Traveling Salesman Problem is an optimization
problem. We are attempting to find the best path.
Problems
Any task that may (or may not) be solved with an algorithm.
Sorting a list is a problem. Sorting the list (2, 3, 1, 7) is an instance of that problem.
Undecidable Problems
Takeaways:
There are some problems we’ve proven that no computer
will ever be able to solve. The Halting Problem is a very
famous example and in general we call these problems
undecidable.
Unit 6 Lesson 4 - Wrap Up
Prompt:
Prompt:
Brainstorm a task that you can
complete faster if you get other people
to help.
Sequential Parallel
Steps are performed in order, Some steps are performed at
one at a time. the same time.
Sequential
Parallel
Unit 6 Lesson 5 - Activity
Prompt
What portions of your algorithms for
Challenges 2 and 3 were parallel?
Speedup
Sequential time divided by parallel time
Prompt
As you watch this video write down
Prompt
As you watch this video write down
Prompt:
Unit Assessment