62
1 2005/Sep/26

CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

1

2005/Sep/26

Page 2: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

2

Homework #2

• Chapter 1: Exercises 7, 9with modifications:for Exercise 7.a: 20 and 32 are changed as your ID number’s last two digits and 60.for Exercise 9: 47x25 are change as 47x(your ID number’s last two digits.

• Chapter 2:If (your ID number’s last two digits % 6) = 1: 1, 7, 13If (your ID number’s last two digits % 6) = 2: 2, 8, 14…If (your ID number’s last two digits % 6) = 0: 6, 12, 18

Page 3: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

3

Example 2: Big, Bigger, Biggest

Task

Find the largest value from a list of values

Algorithm outline

Keep track of the largest value seen so far (initialized to be the first in the list)

Compare each value to the largest seen so far, and keep the larger as the new largest

Page 4: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

4

Example 2: Big, Bigger, Biggest(continued)

Once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms

Library

A collection of useful algorithms

An important tool in algorithm design and development

Page 5: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

5

Example 2: Big, Bigger, Biggest(continued)

Find Largest algorithm

Uses iteration and indices like previous example

Updates location and largest so far when needed in the loop

Page 6: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

6

Figure 2.10Algorithm to Find the Largest Value in a List

Page 7: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

7

Example 3: Meeting Your Match

Task

Find if and where a pattern string occurs within a longer piece of text

Algorithm outline

Try each possible location of pattern string in turn

At each location, compare pattern characters against string characters

Page 8: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

8

Example 3: Meeting Your Match (continued)

Abstraction

Separating high-level view from low-level details

Key concept in computer science

Makes difficult problems intellectually manageable

Allows piece-by-piece development of algorithms

Page 9: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

9

Example 3: Meeting Your Match (continued)

Top-down design

When solving a complex problem:

Create high-level operations in first draft of an algorithm

After drafting the outline of the algorithm, return to the high-level operations and elaborate each one

Repeat until all operations are primitives

Page 10: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

10

Example 3: Meeting Your Match (continued)

Pattern-matching algorithm

Contains a loop within a loop

External loop iterates through possible locations of matches to pattern

Internal loop iterates through corresponding characters of pattern and string to evaluate match

Page 11: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

11

Figure 2.12Final Draft of the Pattern-Matching Algorithm

Page 12: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

12

Summary

Algorithm design is a first step in developing an algorithm

Must also:

Ensure the algorithm is correct

Ensure the algorithm is sufficiently efficient

Pseudocode is used to design and represent algorithms

Page 13: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

13

Summary

Pseudocode is readable, unambiguous, and analyzable

Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution

Abstraction is a key tool for good design

Page 14: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

14

Chapter 3: The Efficiencyof Algorithms

Objectives

In this chapter, you will learn about:

Attributes of algorithms

Measuring efficiency

Analysis of algorithms

When things get out of hand

Page 15: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

15

Introduction

Desirable characteristics in an algorithm

Correctness

Ease of understanding

Elegance

Efficiency

Page 16: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

16

Attributes of Algorithms

Correctness

Does the algorithm solve the problem it is designed for?

Does the algorithm solve the problem correctly?

Ease of understanding

How easy is it to understand or alter an algorithm?

Important for program maintenance

Page 17: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

17

Attributes of Algorithms (continued)

Elegance

How clever or sophisticated is an algorithm?

Sometimes elegance and ease of understanding work at cross-purposes

Efficiency

How much time and/or space does an algorithm require when executed?

Perhaps the most important desirable attribute

Page 18: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

18

Measuring Efficiency

Analysis of algorithms

Study of the efficiency of various algorithms

Efficiency measured as function relating size of input to time or space used

For one input size, best case, worst case, and average case behavior must be considered

The notation captures the order of magnitude of the efficiency function

Page 19: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

19

Sequential Search

Search for NAME among a list of n names

Start at the beginning and compare NAME to each entry until a match is found

Page 20: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

20

Sequential Search (continued)

Figure 3.1Sequential Search Algorithm

Page 21: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

21

Sequential Search (continued)

Comparison of the NAME being searched for against a name in the list Central unit of work Used for efficiency analysis

For lists with n entries: Best case

NAME is the first name in the list 1 comparison (1)

Page 22: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

22

Sequential Search (continued)

For lists with n entries: Worst case

NAME is the last name in the list NAME is not in the list n comparisons (n)

Average case Roughly n/2 comparisons (n)

Page 23: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

23

Sequential Search (continued)

Space efficiency

Uses essentially no more memory storage than original input requires

Very space-efficient

Page 24: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

24

Order of Magnitude: Order n

As n grows large, order of magnitude dominates running time, minimizing effect of coefficients and lower-order terms

All functions that have a linear shape are considered equivalent

Order of magnitude n

Written (n)

Functions vary as a constant times n

Page 25: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

25

Order of Magnitude: Order n

Figure 3.4 Work = cn for Various Values of c

Page 26: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

26

Selection Sort

Sorting Take a sequence of n values and rearrange them

into order

Selection sort algorithm Repeatedly searches for the largest value in a

section of the data Moves that value into its correct position in a

sorted section of the list

Uses the Find Largest algorithm

Page 27: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

27

Selection Sort (continued)

Figure 3.6Selection Sort Algorithm

Page 28: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

28

Selection Sort (continued)

Count comparisons of largest so far against other values

Find Largest, given m values, does m-1 comparisons

Selection sort calls Find Largest n times,

Each time with a smaller list of values

Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2

Page 29: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

29

Selection Sort (continued)

Time efficiency

Comparisons: n(n-1)/2

Exchanges: n (swapping largest into place)

Overall: (n2), best and worst cases

Space efficiency

Space for the input sequence, plus a constant number of local variables

Page 30: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

30

Order of Magnitude – Order n2

All functions with highest-order term cn2 have similar shape

An algorithm that does cn2 work for any constant c is order of magnitude n2, or (n2)

Page 31: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

31

Order of Magnitude – Order n2

(continued) Anything that is (n2) will eventually have larger

values than anything that is (n), no matter what the constants are

An algorithm that runs in time ( n) will outperform one that runs in (n2)

Page 32: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

32

Order of Magnitude – Order n2

(continued)

Figure 3.10 Work = cn2 forVarious Values of c

Page 33: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

33

Figure 3.11A Comparison of n and n2

Order of Magnitude – Order n2

(continued)

Page 34: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

34

Analysis of Algorithms

Multiple algorithms for one task may be compared for efficiency and other desirable attributes

Data cleanup problem

Search problem

Pattern matching

Page 35: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

35

Data Cleanup Algorithms

Given a collection of numbers, find and remove all zeros

Possible algorithms

Shuffle-left

Copy-over

Converging-pointers

Page 36: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

36

The Shuffle-Left Algorithm

Scan list from left to right

When a zero is found, shift all values to its right one slot to the left

Page 37: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

37

Figure 3.14The Shuffle-Left Algorithm for Data Cleanup

Page 38: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

38

The Shuffle-Left Algorithm(continued)

Time efficiency Count examinations of list values and shifts Best case

No shifts, n examinations (n)

Worst case Shift at each pass, n passes n2 shifts plus n examinations (n2)

Page 39: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

39

The Shuffle-Left Algorithm(continued)

Space efficiency

n slots for n values, plus a few local variables

(n)

Page 40: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

40

The Copy-Over Algorithm

Use a second list Copy over each nonzero element in turn

Time efficiency Count examinations and copies

Best case All zeros

n examinations and 0 copies

(n)

Page 41: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

41

The Copy-Over Algorithm(continued)

Figure 3.15The Copy-Over Algorithm for Data Cleanup

Page 42: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

42

The Copy-Over Algorithm(continued)

Time efficiency (continued) Worst case

No zeros

n examinations and n copies

(n)

Space efficiency 2n slots for n values, plus a few extraneous

variables

Page 43: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

43

The Copy-Over Algorithm(continued)

Time/space tradeoff

Algorithms that solve the same problem offer a tradeoff:

One algorithm uses more time and less memory

Its alternative uses less time and more memory

Page 44: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

44

The Converging-Pointers Algorithm

Swap zero values from left with values from right until pointers converge in the middle

Time efficiency

Count examinations and swaps

Best case

n examinations, no swaps

(n)

Page 45: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

45

The Converging-Pointers Algorithm

Figure 3.16The Converging-Pointers Algorithm for Data Cleanup

Page 46: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

46

The Converging-Pointers Algorithm(continued)

Time efficiency (continued)

Worst case

n examinations, n swaps

(n)

Space efficiency

n slots for the values, plus a few extra variables

Page 47: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

47

The Converging-Pointers Algorithm(continued)

Figure 3.17Analysis of Three Data Cleanup Algorithms

Page 48: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

48

Binary Search

Given ordered data,

Search for NAME by comparing to middle element

If not a match, restrict search to either lower or upper half only

Each pass eliminates half the data

Page 49: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

49

Binary Search (continued)

Figure 3.18Binary Search Algorithm (list must be sorted)

Page 50: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

50

Binary Search (continued)

Efficiency Best case

1 comparison

(1)

Worst case lg n comparisons

lg n: The number of times n may be divided by two before reaching 1

(lg n)

Page 51: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

51

Binary Search (continued)

Tradeoff

Sequential search

Slower, but works on unordered data

Binary search

Faster (much faster), but data must be sorted first

Page 52: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

52

Binary Search (continued)

Figure 3.21A Comparison of n and lg n

Page 53: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

53

Pattern Matching

Analysis involves two measures of input size

m: length of pattern string

n: length of text string

Unit of work

Comparison of a pattern character with a text character

Page 54: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

54

Pattern Matching (continued)

Efficiency Best case

Pattern does not match at all n - m + 1 comparisons (n)

Worst case Pattern almost matches at each point (m -1)(n - m + 1) comparisons (m x n)

Page 55: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

55

Pattern Matching (continued)

Figure 3.22Order-of-Magnitude Time Efficiency Summary

Page 56: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

56

When Things Get Out of Hand

Polynomially bound algorithms

Work done is no worse than a constant multiple of n2

Intractable algorithms

Run in worse than polynomial time

Examples

Hamiltonian circuit

Bin-packing

Page 57: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

57

When Things Get Out of Hand(continued)

Exponential algorithm

(2n)

More work than any polynomial in n

Approximation algorithms

Run in polynomial time but do not give optimal solutions

Page 58: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

58

Figure 3.25Comparisons of lg n, n, n2 , and 2n

When Things Get Out of Hand(continued)

Page 59: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

59

When Things Get Out of Hand(continued)

Figure 3.27A Comparison of Four Orders of Magnitude

Page 60: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

60

Summary of Level 1

Level 1 (Chapters 2 and 3) explored algorithms Chapter 2

Pseudocode Sequential, conditional, and iterative operations Algorithmic solutions to three practical problems

Chapter 3 Desirable properties for algorithms Time and space efficiencies of a number of

algorithms

Page 61: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

61

Summary

Desirable attributes in algorithms:

Correctness

Ease of understanding

Elegance

Efficiency

Efficiency – an algorithm’s careful use of resources – is extremely important

Page 62: CS w03 - 國立中興大學web.nchu.edu.tw/~ycchiang/CS/CS_w03.pdf · 2010. 9. 1. · Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as function

62

Summary

To compare the efficiency of two algorithms that do the same task

Consider the number of steps each algorithm requires

Efficiency focuses on order of magnitude