Search Header Logo
BJC AP CSP Unit 5 Lab 1

BJC AP CSP Unit 5 Lab 1

Assessment

Presentation

Computers

10th - 12th Grade

Practice Problem

Hard

Created by

TSEE LEE

Used 34+ times

FREE Resource

16 Slides • 18 Questions

1

CSP Unit 5 Lesson 1

Algorithmic Efficiency

media

2

Open Ended

We'll watch the video together. Describe the strategy that the contestant used.

3

Multiple Choice

Below is an unsorted list of numbers, which is a list whose elements are not in order.

True or False? The list contain the number 121.


[171, 122, 211, 120, 71, 217, 12, 11, 211, 212, 711, 21, 210, 1, 17, 100, 121, 102, 127, 21, 721, 1, 712, 117]

1

True

2

False

4

Multiple Choice

True or False? The list contain the number 71.


[171, 122, 211, 120, 71, 217, 12, 11, 211, 212, 711, 21, 210, 1, 17, 100, 121, 102, 127, 21, 721, 1, 712, 117]

1

True

2

False

5

Multiple Choice

True or False? The list contain the number 172.


[171, 122, 211, 120, 71, 217, 12, 11, 211, 212, 711, 21, 210, 1, 17, 100, 121, 102, 127, 21, 721, 1, 712, 117]

1

True

2

False

6

Vocabulary: LINEAR SEARCH

Checking each item (element) in a list from left to right, one after another, to find a specific item.


It checks each item in sequential order and stops when a match is found or when the entire list has been checked (which happens when the item being searched for isn’t in the list).


Aka. Sequential search.

7

Multiple Choice

Suppose an unordered list has n elements. In the best case scenario, how many comparisons are required to find a specific value?

1

1

2

n

3

between 1 and n

4

Not enough info

8

Multiple Choice

Suppose an unordered list has n elements. In the worst case scenario, how many comparisons are required to find a specific value?

1

1

2

n

3

between 1 and n

4

Not enough info

9

Multiple Choice

Suppose an unordered list has 1,000 elements, and it takes up to 1 second to search through the entire list.


Then, more items are added to the list so that it now has 1,000,000 elements. How many seconds will it take to search this list?

1

1

2

1,000

3

1,000,000

4

Not enough info

10

Vocabulary: Linear/Sequential Search

  • An algorithm takes linear time if multiplying the input size by ten multiplies the time required by ten.

  • A linear search (or sequential search) algorithm checks each element of a list in order, a process which takes linear time.

media

11

Multiple Choice

You choose a number between 1 and 1,000. Roughly how many guesses will I take to guess your number, if you tell me to go higher or lower?

1

1

2

10

3

100

4

1000

12

Vocabulary:

Binary Search

  • 1) Set min & max

  • 2) Average min & max to find the index of the middle element

  • 3) Compare value at middle to target; if matched, return index

  • 4) If element < target, set min to middle+1; Else, set max to middle-1

  • 5) Repeat steps 2-4 until value is found or if min>max

media

13

Fill in the Blanks

Type answer...

14

Multiple Choice

Suppose an ordered list has n elements. In the best case scenario, using binary search, how many comparisons are required to find a specific value? (In CS, log is usually base 2.)

1

1

2

log n

3

n/2

4

n

15

Multiple Choice

Suppose an ordered list has n elements. In the worst case scenario, using binary search, how many comparisons are required to find a specific value? (In CS, log is usually base 2.)

1

1

2

log n

3

n/2

4

n

16

Will binary search to make a program more efficient?

  • Are you searching for one thing in a list or are you finding all the things in the list with some characteristic?

  • Is your data sorted or unsorted?

media

17

Multiple Choice

Suppose an ordered list has 1,000 elements, and it takes up to 1 second to binary search through the entire list.


Then, more items are added to the list so that it now has 1,000,000 elements. How many seconds will it take to search this list?

1

1

2

2

3

10

4

500

5

1,000

18

Multiple Choice

Suppose an ordered list has 1,000 elements, and it takes up to 1 second to binary search through the entire list.


How many seconds will it take to search a list of 1,000,000,000?

1

3

2

6

3

log (1000)

4

log (1,000,000)

5

1000

19

Algorithm takes...

  • Linear time: if the number of steps is proportional to the input size. 2x the input size, 2x the time.

  • Sublinear time: if the time grows more slowly than the size.

media

20

Algorithm takes...

  • Constant time: if it takes the same amount of time regardless of input size.

  • Quadratic time: if the number of steps is proportional to the square of the input size.

media

21

The most important difference in algorithm efficiency is between algorithms that take:

  • Polynomial time: if the number of steps is less than or equal to a power of the size of the input, such as constant (n0), sublinear, linear (n1), quadratic (n2), or cubic (n3).

  • Exponential time: if the number of steps is less than or equal to a function like 2n, 10n, etc., which is much slower than any polynomial.

22

Multiple Choice

Question image

The table shows the computer time it takes to complete various tasks on the data of different sized towns.

Based on the information in the table, which of the following tasks is likely to take the longest amount of time when scaled up for a city of population 1,000,000?

1

Backing up data

2

Sorting data

3

Searching through data

4

Entering data

23

Vocabulary: Heuristic Solutions

  • An exponential time algorithm still solves a problem correctly; it just takes a unreasonably long time.

  • Exponential time algorithms can sometimes be replaced by heuristics, which are polynomial-time algorithms that don't solve the problem exactly, but give a good enough approximation.

  • A decision problem is a problem with a true/false answer (for example, "is 5,825,496,221 a prime number?").

  • An optimization problem is one with the goal of finding the best solution among many (for example, "what's the best school schedule to place every student into as many of their requested classes as possible?").

24

Multiple Choice

Which type of algorithms may be replaced by heuristics?

1

Decision problems

2

Optimization problems

3

Both

4

Neither

25

Classic Heuristic: Traveling Salesman Problem

  • There must be a shortest route.

  • There is no known polynomial time algorithm for this problem.

  • One heuristic: Pick a path at random, then try to improve it by swapping two cities repeatedly until you can't make the path better with such small changes.

media

26

"Hill-Climbing"

  • You'll find the best nearby path (the top of a hill)

  • There might be a higher hill (a better path) further away

  • You'll be stuck at a local maximum

media

27

Multiple Choice

In which of the following problems is a heuristic solution appropriate?

1

Find the best combination of ingredients for spaghetti sauce.

2

Find the biggest item in a list.

3

Find the combination to a lock with n numbers.

4

Playing chess.

28

Not all decision problems (T/F questions) can be solved with an algorithm.

  • A decidable problem a decision problem for which it's possible to write an algorithm that will give a correct output for all inputs. EX: "Is the integer even?"

  • An undecidable problem is the opposite. It's not possible to write an algorithm that will give a correct output for all inputs—even though it might be possible for some of them. EX: "Will this computer program that takes an input always eventually report a result?"

29

Vocabulary:

Sequential & Parallel Computing

  • In sequential computing, operations are performed in order one at a time.

  • In parallel computing, the program is broken into smaller steps; some steps are performed at the same time. Modern computers have multiple processors (2, 4, or 8 cores) in a single computer, so you can do small-scale parallel processing on your desktop machine.

30

Multiple Choice

Question image

How long will this sequential program take to run?

1

4

2

6

3

8

4

18

31

Multiple Choice

Question image

Careful! How long will this parallel program take to run?

1

6

2

8

3

14

4

18

32

Vocabulary

  • Distributed computing is a form of parallel computing that uses multiple computers (perhaps even spread out around the world).

  • A processor is a piece of circuitry inside a computer that carries out the instructions from programs.

media

33

Vocabulary

  • Programmers refer to the speedup of parallel solution to describe how many times as fast the parallel solution is compared to the sequential solution:

  • E.g. Sequential time is 10 mins, parallel time is 2 mins. speedup = 10/2 = 5. Parallelization is 5x faster.

media

34

Homework

CSP Unit 5 Lesson 1

Algorithmic Efficiency

media

Show answer

Auto Play

Slide 1 / 34

SLIDE