cmput101 introduction to computing(c) yngvi bjornsson & jia you1 the efficiency of algorithms...

38
CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & J ia You 1 The Efficiency of The Efficiency of Algorithms Algorithms Chapter 3 Chapter 3 Topics: Topics: Attributes of Algorithms Attributes of Algorithms A Choice of Algorithms A Choice of Algorithms Measuring Efficiency Measuring Efficiency Analysis of Algorithms Analysis of Algorithms When Things Get Out of Hand When Things Get Out of Hand

Post on 22-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 1

The Efficiency of The Efficiency of AlgorithmsAlgorithms

Chapter 3Chapter 3Topics:Topics:

Attributes of AlgorithmsAttributes of AlgorithmsA Choice of AlgorithmsA Choice of AlgorithmsMeasuring EfficiencyMeasuring Efficiency

Analysis of AlgorithmsAnalysis of AlgorithmsWhen Things Get Out of HandWhen Things Get Out of Hand

Page 2: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 2

Attributes of AlgorithmsAttributes of Algorithms• CorrectnessCorrectness

– Give a correct solution to the problem!Give a correct solution to the problem!• EfficiencyEfficiency

– Time: How long does it take to solve the problem?Time: How long does it take to solve the problem?– Space: How much memory is needed?Space: How much memory is needed?– Benchmarking vs. AnalysisBenchmarking vs. Analysis

• Ease of understandingEase of understanding– Program maintenanceProgram maintenance

• EleganceElegance

Page 3: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 3

A Choice of AlgorithmsA Choice of Algorithms• Possible to come up with several different Possible to come up with several different

algorithms to solve the same problem.algorithms to solve the same problem.• Which one is the "best"?Which one is the "best"?

– Most efficientMost efficient• Time vs. Space?Time vs. Space?

– Easiest to maintain?Easiest to maintain?• How do we measure time efficiency?How do we measure time efficiency?

– Running time? Machine dependent!Running time? Machine dependent!– Number of steps?Number of steps?

Page 4: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 4

The Data Cleanup ProblemThe Data Cleanup Problem

• We look at We look at threethree algorithms for the same problem, algorithms for the same problem, and compare their and compare their time-time- and and space-efficiency.space-efficiency.

• Problem:Problem: Remove 0 entries from a list of numbers. Remove 0 entries from a list of numbers.

00 1212 3232 7171 3434 00 3636 9292 00 1313

1212 3232 7171 3434 3636 9292 1313

Page 5: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 5

1. The Shuffle-Left 1. The Shuffle-Left AlgorithmAlgorithm

• We scan the list from left to right, and whenever We scan the list from left to right, and whenever we encounter a 0 element we copy ("shuffle") we encounter a 0 element we copy ("shuffle") the rest of the list one position left. the rest of the list one position left.

00 1212 3232 7171 3434 00 3636 9292 00 1313

1212 3232 7171 3434 3636 9292 1313 1313 1313 1313

Page 6: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 6

00 1212 3232 7171 3434 00 3636 9292 00 1313

1212 3232 7171 3434 00 3636 9292 00 1313 1313

1212 3232 7171 3434 00 3636 9292 00 1313 1313

1212 3232 7171 3434 3636 9292 00 1313 1313 1313

1212 3232 7171 3434 3636 9292 00 1313 1313 1313

1212 3232 7171 3434 3636 9292 1313 1313 1313 1313

Page 7: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 7

Shuffle-Left AnimationShuffle-Left Animation

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1212 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1212 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1212 3232 3232 7171 3434 00 3636 9292 00 1313

LL RR

1212 32 32 3232 7171 3434 00 3636 9292 00 1313

LL RR

1212 32 32 7171 7171 3434 00 3636 9292 00 1313

LL RR

1212 32 32 7171 7171 3434 00 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 3434 00 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 3434 00 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 00 00 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 00 00 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 00 3636 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 00 3636 3636 9292 00 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 9292 00 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 9292 00 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 00 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 00 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 00 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 3636 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 9292 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 00 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 00 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

1212 32 32 7171 3434 3636 9292 1313 1313 1313 1313

LL RR

Legit:Legit: 1010 99 88 77

Page 8: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 8

2. The Copy-Over 2. The Copy-Over AlgorithmAlgorithm

• We scan the list from left to right, and whenever We scan the list from left to right, and whenever we encounter a nonzero element we copy it over we encounter a nonzero element we copy it over to a to a new listnew list..

00 1212 3232 7171 3434 00 3636 9292 00 1313

1212 3232 7171 3434 3636 9292 1313

Page 9: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 9

The Copy-Over AnimationThe Copy-Over Animation

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 3232

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636 9292

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636 9292

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636 9292

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636 9292 1313

NN

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL

1212 32 32 7171 3434 3636 9292 1313

NN

Page 10: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 10

3. The Converging-3. The Converging-Pointers AlgorithmPointers Algorithm

• We scan the list from We scan the list from both left (L) both left (L) andand right (R) right (R). . Whenever Whenever LL encounters a encounters a 00 element, the element, the element at location element at location RR is copied to location is copied to location LL, , then then RR reduced. reduced.

00 1212 3232 7171 3434 00 3636 9292 00 1313

1313 1212 3232 7171 3434 9292 3636 9292 00 1313

Page 11: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 11

Converging Pointers Converging Pointers AnimationAnimation

00 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 00 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 9292 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 9292 3636 9292 00 1313

LL RR

1313 12 12 3232 7171 3434 9292 3636 9292 00 1313

L RL R

Legit:Legit: 1010 99 88 77

Page 12: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 12

Data-Cleanup Algorithm Data-Cleanup Algorithm ComparisonComparison

• Which one is the most space efficient?Which one is the most space efficient?– Shuffle-leftShuffle-left no additional spaceno additional space– Copy-overCopy-over needs a new listneeds a new list– Converging-pointersConverging-pointers no additional spaceno additional space

• Which one is the most time efficient?Which one is the most time efficient?– Shuffle-leftShuffle-left many comparisonsmany comparisons– Copy-overCopy-over goes through list only oncegoes through list only once– Converging-pointersConverging-pointers goes through list only oncegoes through list only once

• How do we measure time efficiency?How do we measure time efficiency?

Page 13: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 13

ExerciseExercise

• Can you come up with a more efficient algorithm Can you come up with a more efficient algorithm for the data-cleanup problem, that does:for the data-cleanup problem, that does:– not require any additional spacenot require any additional space– less copying than shuffle-leftless copying than shuffle-left– maintain the order of the none-zero elements maintain the order of the none-zero elements

• Hint:Hint:– Can the copy-over algorithm be modified to copy the Can the copy-over algorithm be modified to copy the

element into the same list?element into the same list?

Page 14: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 14

Measuring EfficiencyMeasuring Efficiency• Need a metric to measure time efficiency of algorithms:Need a metric to measure time efficiency of algorithms:

– How long does it take to solve the problem?How long does it take to solve the problem?• Depends on machine speedDepends on machine speed

– How many steps does the algorithm execute?How many steps does the algorithm execute?• Better metric, but a lot of work to count all stepsBetter metric, but a lot of work to count all steps

– How many "fundamental steps" does the algorithm How many "fundamental steps" does the algorithm execute?execute?

• Depends on size and type of input, interested in knowing:Depends on size and type of input, interested in knowing:– BestBest-case, -case, WorstWorst-case, -case, AverageAverage-case behavior-case behavior

• Need to analyze the algorithm!Need to analyze the algorithm!

Page 15: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 15

Sequential Search Sequential Search

1. Get values for 1. Get values for NameName, , NN11,…, N,…, Nnn, T, T11,…, T,…, Tnn

2. Set the value 2. Set the value ii to 1and set the value of to 1and set the value of FoundFound to NO to NO3. Repeat steps 4 through 7 until 3. Repeat steps 4 through 7 until Found = Found = YESYES oror i > n i > n

4.4. IfIf Name = N Name = Nii thenthen5. 5. Print the value ofPrint the value of T Tii

6. Set the value of 6. Set the value of FoundFound to YES to YES ElseElse7. Add 1 to the value of 7. Add 1 to the value of ii8. If Found = NO then print "Sorry, name not in directory"8. If Found = NO then print "Sorry, name not in directory"

9. Stop9. Stop

1. Get values for 1. Get values for NameName, , NN11,…, N,…, Nnn, T, T11,…, T,…, Tnn

2. Set the value 2. Set the value ii to 1and set the value of to 1and set the value of FoundFound to NO to NO3. Repeat steps 4 through 7 until 3. Repeat steps 4 through 7 until Found = Found = YESYES oror i > n i > n

4.4. IfIf Name = N Name = Nii thenthen5. 5. Print the value ofPrint the value of T Tii

6. Set the value of 6. Set the value of FoundFound to YES to YES ElseElse7. Add 1 to the value of 7. Add 1 to the value of ii8. If Found = NO then print "Sorry, name not in directory"8. If Found = NO then print "Sorry, name not in directory"

9. Stop9. Stop

Page 16: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 16

Sequential Search - Sequential Search - AnalysisAnalysis

• How many steps does the algorithm execute?How many steps does the algorithm execute?– Steps 2, 5, 6, 8 and 9 are executed at most once.Steps 2, 5, 6, 8 and 9 are executed at most once.– Steps 3, 4, and 7 depends on input size.Steps 3, 4, and 7 depends on input size.

• Worst case:Worst case:– Step 3, 4, and 7 are executed at most n-times.Step 3, 4, and 7 are executed at most n-times.

• Best case:Best case:– Step 3 and 4 are executed only once.Step 3 and 4 are executed only once.

• Average case:Average case:– Step 3, 4 are executed approximately (n/2)-times.Step 3, 4 are executed approximately (n/2)-times.

• Can use name comparisons as a fundamental unit of work!Can use name comparisons as a fundamental unit of work!

Page 17: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 17

Order of MagnitudeOrder of Magnitude• We are: We are:

– Not interested in knowing the exact number of steps Not interested in knowing the exact number of steps the algorithm performs.the algorithm performs.

– Mainly interested in knowingMainly interested in knowing how the number of how the number of steps grows with increased input sizesteps grows with increased input size!!

• Why?Why?– Given large enough input, the algorithm with faster Given large enough input, the algorithm with faster

growth will execute more steps.growth will execute more steps.• Order of magnitude, Order of magnitude, O(...)O(...), measures how the , measures how the

number of steps grows with input size number of steps grows with input size nn. .

Page 18: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 18

Order of MagnitudeOrder of Magnitude• Not interested in the exact number of steps, for Not interested in the exact number of steps, for

example, algorithm where total steps are:example, algorithm where total steps are:– nn– 5n5n– 5n+3455n+345– 4500n+10004500n+1000

• are all of order O(n)are all of order O(n)– For all the above algorithms, the total number of For all the above algorithms, the total number of

steps grows approx. proportionally with input size steps grows approx. proportionally with input size (given large enough n).(given large enough n).

Page 19: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 19

Linear Algorithms - O(n)Linear Algorithms - O(n)• If the number of steps grows in proportion, or If the number of steps grows in proportion, or

linearly, with input size, its a linear algorithm, O(n).linearly, with input size, its a linear algorithm, O(n).– Sequential search is linear, denoted O(n)Sequential search is linear, denoted O(n)

• On a graph, will show as a straight lineOn a graph, will show as a straight line

nn

stepssteps

Page 20: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 20

Non-linear AlgorithmNon-linear Algorithm• Think of an algorithm for filling out the n-times Think of an algorithm for filling out the n-times

multiplication table.multiplication table.

• As n increases the work the algorithm does will As n increases the work the algorithm does will increase by n*n or nincrease by n*n or n22, the algorithm is O(n, the algorithm is O(n22))

11 ...... nn

11

......

nn

Page 21: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 21

Data Cleanup - AnalysisData Cleanup - Analysis

Shuffle-LeftShuffle-Left Copy-OverCopy-OverConvergingConvergingpointerspointers

TimeTime SpaceSpace TimeTime SpaceSpace TimeTime SpaceSpace

Best Best CaseCase O(n)O(n) nn O(n)O(n) nn O(n)O(n) nn

WorstWorstCaseCase

O(nO(n22)) nn O(n)O(n) 2n2n O(n)O(n) nn

AverageAverage CaseCase

O(nO(n22)) nn O(n)O(n) n n x x 2n2n O(n)O(n) nn

Page 22: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 22

SortingSorting• Sorting is a very common task, for example:Sorting is a very common task, for example:

– sorting a list of names into alphabetical ordersorting a list of names into alphabetical order– numbers into numerical ordernumbers into numerical order

• Important to find efficient algorithms for sortingImportant to find efficient algorithms for sorting– Selection sortSelection sort– Bubble sortBubble sort– Quick sortQuick sort– Heap sortHeap sort

• We will analyze the complexity of We will analyze the complexity of selection sortselection sort..

Page 23: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 23

44 66 99 22 55

Selection SortSelection Sort• Divide the list into a Divide the list into a unsortedunsorted and a and a sortedsorted section, section, initially initially

the sorted section is emptythe sorted section is empty..• Locate the Locate the largest largest element in the unsorted section and element in the unsorted section and

replace that with thereplace that with the last last element of the unsorted section.element of the unsorted section.• Move the markerMove the marker between the unsorted and sorted section between the unsorted and sorted section

one position to the one position to the leftleft..• Repeat until Repeat until unsorted section of the list is emptyunsorted section of the list is empty..

Page 24: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 24

44 66 99 22 55

44 66 55 22 99

44 22 55 66 99

44 22 55 66 99

22 44 55 66 99

22 44 55 66 99

Page 25: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 25

Selection Sort - AnimationSelection Sort - Animation• Exchange the Exchange the largestlargest element of the unsorted section element of the unsorted section

with the with the last last element of the unsorted sectionelement of the unsorted section• Move marker separating the unsorted and sorted Move marker separating the unsorted and sorted

section one position to the section one position to the leftleft (forward in the list) (forward in the list)• Continue until unsorted section is Continue until unsorted section is emptyempty..

44 66 99 22 55

44 66 99 22 55

44 66 55 22 99

44 66 55 22 99

44 66 55 22 99

44 22 55 66 99

44 22 55 66 99

44 22 55 66 99

44 22 55 66 99

44 22 55 66 99

44 22 55 66 99

22 44 55 66 99

22 44 55 66 99

22 44 55 66 99

22 44 55 66 99

22 44 55 66 99

Page 26: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 26

Selection Sort - AnalysisSelection Sort - Analysis• What order of magnitude is this algorithm?What order of magnitude is this algorithm?

– Use number of comparisons as a fundamental unit of work.Use number of comparisons as a fundamental unit of work.• Total number of comparisons:Total number of comparisons:

(n-1)(n-1) ++ (n-2)(n-2) ++ ...... ++ 2 + 12 + 1== (n-1) / 2 (n-1) / 2 n n

½ n½ n22 - ½ n - ½ n==

• This is a This is a O(nO(n22)) algorithm. algorithm.• Worst, best, average case behavior the same (why?)Worst, best, average case behavior the same (why?)

Page 27: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 27

Binary SearchBinary Search• How do we look up words in a list that is already How do we look up words in a list that is already

sorted?sorted?– DictionaryDictionary– Phone bookPhone book

• Method:Method:– Open up the book roughly in the middle.Open up the book roughly in the middle.– Check in which half the word is.Check in which half the word is.– Split that half again in two.Split that half again in two.– Continue until we find the word. Continue until we find the word.

Page 28: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 28

Binary Search - ExampleBinary Search - Example

To find To find NancyNancy, we go through , we go through GarryGarry (mid point at 4) (mid point at 4) Pat Pat (mid point of 5-7) (mid point of 5-7) NancyNancy (mid point of a single item) (mid point of a single item)

Ann Bob Dave Garry Nancy Pat SueAnn Bob Dave Garry Nancy Pat Sue

Position: 1 2 3 4 5 6 7Position: 1 2 3 4 5 6 7

Page 29: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 29

Binary Search - Odd Binary Search - Odd number of elementsnumber of elements

Ann Bob Dave Garry Nancy Pat SueAnn Bob Dave Garry Nancy Pat Sue

Position: 1 2 3 4 5 6 7Position: 1 2 3 4 5 6 7

Whom that can be found Whom that can be found in one step: in one step: GarryGarry

in two steps: in two steps: Bob, PatBob, Pat in three steps: in three steps: all remaining personsall remaining persons

Page 30: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 30

Binary Search - Even Binary Search - Even number of elementsnumber of elements

Ann Bob Dave Garry Nancy Pat Ann Bob Dave Garry Nancy Pat

Position: 1 2 3 4 5 6 Position: 1 2 3 4 5 6

Let's choose the end of first half as midpoint.Let's choose the end of first half as midpoint.Whom that can be found Whom that can be found

in one step: in one step: DaveDave in two steps: in two steps: Ann, NancyAnn, Nancy in three steps: in three steps: all remaining personsall remaining persons

Page 31: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 31

Binary Search - AnalysisBinary Search - Analysis• Looking for a name is like walking branches in a treeLooking for a name is like walking branches in a tree

44

22 66

11 33 55 77

Ann Bob Dave Garry Nancy Pat SueAnn Bob Dave Garry Nancy Pat Sue

Position: 1 2 3 4 5 6 7Position: 1 2 3 4 5 6 7

Page 32: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 32

Binary Search - Analysis Binary Search - Analysis (cont.)(cont.)

• We cut the number of remaining names in half.We cut the number of remaining names in half.• The number of times a number The number of times a number nn can be cut if half can be cut if half

and not get below 1 is calledand not get below 1 is called– Logarithm of n to the base 2Logarithm of n to the base 2– Notation: logNotation: log22 n or lg n n or lg n

• Max. number of name comparisons = depth of tree.Max. number of name comparisons = depth of tree.– 3 in the pervious example.3 in the pervious example.– nn names then approx. names then approx. lg nlg n comparisons needed comparisons needed

• Binary search is O(lg n)Binary search is O(lg n)

Page 33: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 33

Logarithm vs. Linear Logarithm vs. Linear nn lg nlg n

88 33

1616 44

3232 55

6464 66

128128 77

......

3276832768 1515

......

10485761048576 2020

nn

stepssteps

lg nlg n

nn

Page 34: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 34

When Things Get Out of When Things Get Out of HandHand• Polynomial algorithms (exponent is a constant)Polynomial algorithms (exponent is a constant)

– For example: lg n, n, nFor example: lg n, n, n22, n, n33, ... , n, ... , n3000 3000 , ... , ... – More generally: nMore generally: naa

• Exponential algorithms (exponent function of n)Exponential algorithms (exponent function of n)– For example: 2For example: 2nn

– More generally: aMore generally: ann

• An exponential algorithm:An exponential algorithm:– Given large enough n will always performs more work than a Given large enough n will always performs more work than a

polynomially bounded one. polynomially bounded one. • Problem for which there exist only exponential Problem for which there exist only exponential

algorithms are called algorithms are called intractableintractable– Solvable, but not within practical time limitsSolvable, but not within practical time limits– Most often it is infeasible to solve but the smallest problems!Most often it is infeasible to solve but the smallest problems!

Page 35: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 35

Growth RateGrowth Rate

nn

stepssteps

lg nlg n

nnnn2222nn

Page 36: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 36

Example of growthExample of growth

N 10 50 100 1000

lg(n) .0003 sec .0006 sec .0007 sec .001 sec

n .001 sec .005 sec .01 sec 0.1 sec

n2 .01 sec .25 sec 1 sec 1.67 min

2n .1024 sec 3570 years

4*1016 centuries Too big

Page 37: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 37

SummarySummary• We are concerned with the efficiency of algorithmsWe are concerned with the efficiency of algorithms

– Time- and Space-efficiencyTime- and Space-efficiency– Need to analyze the algorithmsNeed to analyze the algorithms

• Order of magnitude measures the efficiencyOrder of magnitude measures the efficiency– E.g. O(lg n), O(n), O(nE.g. O(lg n), O(n), O(n22), O(n), O(n33) , O(2) , O(2nn), ...), ...– Measures how fast the work grows as we increase the Measures how fast the work grows as we increase the

input size n.input size n.– Desirable to have slow growth rate.Desirable to have slow growth rate.

Page 38: CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms

CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 38

SummarySummary• We looked at different algorithmsWe looked at different algorithms

– Data-Cleanup: Shuffle-left O(nData-Cleanup: Shuffle-left O(n22), Copy-over O(n), ), Copy-over O(n), Converging-pointers O(n)Converging-pointers O(n)

– Search: Sequential-search O(n), Binary-search 0(lg n)Search: Sequential-search O(n), Binary-search 0(lg n)– Sorting: Selection-sort O(nSorting: Selection-sort O(n22))

• Some algorithms are exponentialSome algorithms are exponential– Not polynomially boundedNot polynomially bounded– Problems for which there exists only exponential Problems for which there exists only exponential

algorithms are called intractablealgorithms are called intractable– Only feasible to solve small instances of such problemsOnly feasible to solve small instances of such problems