Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J.

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J."— Presentation transcript:

1 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear

2 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness

3 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-3 Definition of Algorithm An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

4 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-4 Definition of Algorithm (Cont.) Ordered: parallel algorithm? Not the first- step, second-step scenario Executable: make a list of all the positive integers? Unambiguous: need sufficient information, does not require creative skills Terminating: the execution must lead to an end (consider the case of “long-division algorithm”)

5 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-5 The Abstract Nature of Algorithms An algorithm v.s. its representation: a story v.s. a book The level of detail –F = (9/5)C + 32 –Convert the Celsius reading to its Fahrenheit equivalent” Programs: representation of algorithms Processes: the activities of executing algorithms

6 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-6 Algorithm Representation Traditional natural language (ambiguity) –Visiting grandchildren can be nerve-racking. The language of pictures –Few readers could successfully follow the directions Communication problems: not precisely defined or inadequate information

7 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-7 Figure 5.2 Folding a bird from a square piece of paper

8 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-8 Primitives Requires well-defined primitives (a well- defined set of building blocks) A collection of primitives constitutes a programming language. Each primitive consists of two part: –Syntax: symbolic representation of the primitive –Semantics: the meaning of the primitive Lower-level v.s. Higher-level primitives

9 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-9 Figure 5.3 Origami primitives

10 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-10 Pseudocode Primitives Assignment name  expression Conditional selection if condition then action Repeated execution while condition do activity Procedure procedure name (generic names)

11 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-11 Figure 5.4 The procedure Greetings in pseudocode

12 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-12 Polya’s Problem Solving Steps 1. Understand the problem. 2. Devise a plan for solving the problem. 3. Carry out the plan. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems.

13 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-13 Program Development Steps 1. Understand the problem. 2. Get an idea of how an algorithmic procedure might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the program for accuracy and its potential as a tool for solving other problems.

14 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-14 Ages of Children Problem Person A is charged with the task of determining the ages of B’s three children. –B tells A that the product of the children’s ages is 36. –A replies that another clue is required. –B tells A the sum of the children’s ages. –A replies that another clue is needed. –B tells A that the oldest child plays the piano. –A tells B the ages of the three children. How old are the three children?

15 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-15 Figure 5.5

16 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-16 Prediction of a race Before A, B, C, D ran a race they made the following predictions: –A predicted that B would win. –B predicted that D would be last. –C predicted that A would be the third. –D predicted that A’s prediction would be correct. –Only one of these predictions was true, and this was the prediction made by the winner. In what order did A, B, C, and D finish the race?

17 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-17 Getting a Foot in the Door Try working the problem backwards –E.g., unfold a completed bird Solve an easier related problem –Relax some of the problem constraints –Solve pieces of the problem first (bottom up methodology) –E.g, alphabetizing lists of names “Interchange the names David and Alice. Move the name Carol to the position between Alice and David. Move the name Bob to the position between Alice and Carol.”

18 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-18 Getting a Foot in the Door (Cont.) Stepwise refinement: Divide the problem into smaller problems (top-down methodology) –E.g. the binary search algorithm (On each step, look at the middle element of the remaining list to eliminate half of it, and quickly zero in on the desired element) <x<x>x>x<x<x<x<x

19 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-19 Iterative Structures Pretest loop: while (condition) do (loop body) Posttest loop: repeat (loop body) until(condition)

20 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-20 Figure 5.6 The sequential search algorithm in pseudocode

21 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-21 Figure 5.7 Components of repetitive control

22 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-22 Figure 5.8 The while loop structure

23 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-23 Figure 5.9 The repeat loop structure

24 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-24 Figure 5.10 Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

25 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-25 Figure 5.11 The insertion sort algorithm expressed in pseudocode

26 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-26 Recursion The execution of a procedure leads to another execution of the procedure. Multiple activations of the procedure are formed, all but one of which are waiting for other activations to complete.

27 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-27 Figure 5.12 Applying our strategy to search a list for the entry John

28 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-28 Figure 5.13 A first draft of the binary search technique

29 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-29 Figure 5.14 The binary search algorithm in pseudocode

30 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-30 Figure 5.15

31 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-31 Figure 5.16

32 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-32 Figure 5.17

33 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-33 Another recursion example: Tower of Hanoi Example Problem: Get all disks from peg 1 to peg 2. –Only move 1 disk at a time. –Never set a larger disk on a smaller one. Peg #1Peg #2Peg #3

34 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-34 Algorithm Efficiency Measured as number of instructions executed Best, worst, and average case analysis For example: –Sequential search algorithm for 30,000 records: if 10 milliseconds for retrieving and checking each record for its ID#, then total average 150 seconds –Binary search algorithm for 30,000 records: at most 15 entries are retrieved, so 0.15 seconds are required

35 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-35 Figure 5.18 Applying the insertion sort in a worst-case situation In the best case, n – 1 comparisons for n entries In the worse case, (1/2)(n^2 – n) comparisons In the average case, (1/4)(n^2 – n) comparisons

36 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-36 Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm

37 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-37 Figure 5.20 Graph of the worst-case analysis of the binary search algorithm

38 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-38 The Growth of Functions For functions over numbers, we often need to know a rough measure of how fast a function grows. If f(x) is faster growing than g(x), then f(x) always eventually becomes larger than g(x) in the limit (for large enough values of x). Useful in engineering for showing that one design scales better or worse than another.

39 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-39 Concept of Order of Growth We say f A (n)=30n+8 is order n, or O(n). It is, at most, roughly proportional to n. f B (n)=n 2 +1 is order n 2, or O(n 2 ). It is roughly proportional to n 2. Any O(n 2 ) function is faster-growing than any O(n) function. For large numbers of user records, the O(n 2 ) function will always take more time.

40 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-40 Definition: O(g), at most order g Let g be any function R  R. Define “at most order g”, written O(g), to be: {f :R  R |  c,k:  x>k: |f(x)|  c|g(x)|}. –“Beyond some point k, function f is at most a constant c times g (i.e., proportional to g).” “f is at most order g”, or “f is O(g)”, or “f=O(g)” all just mean that f  O(g). Sometimes the phrase “at most” is omitted.

41 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-41 “ Big-O ” Proof Examples Show that 30n+8 is O(n). –Show  c,k:  n>k: 30n+8  cn. Let c=31, k=8. Assume n >k=8. Then cn = 31n = 30n + n > 30n+8, so 30n+8 < cn. Show that n 2 +1 is O(n 2 ). –Show  c,k:  n >k: n 2 +1  cn 2. Let c=2, k=1. Assume n>1. Then cn 2 = 2n 2 = n 2 +n 2 > n 2 +1, or n 2 +1< cn 2.

42 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-42 Definition:  (g), exactly order g If f  O(g) and g  O(f) then we say “g and f are of the same order” or “f is (exactly) order g” and write f  (g). Another equivalent definition:  (g)  {f :R  R |  c 1 c 2 k  x >k: |c 1 g(x)|  |f(x)|  |c 2 g(x)| } “Everywhere beyond some point k, f(x) lies in between two multiples of g(x).”

43 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-43  (g) = {f | g  O(f)} “The functions that are at least order g.” Definition:  (g), at least order g

44 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-44 Chain Separating Problem A traveler has a gold chain of seven links. He must stay at an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?

45 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-45 Figure 5.21 Separating the chain using only three cuts

46 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-46 Figure 5.22 Solving the problem with only one cut

47 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-47 Figure 5.22 Solving the problem with only one cut (Cont.) First morning: Give the single link. Second: Retrieve the single link and give the two-link piece. Third: Give the single link. Four: Retrieve the three links and give the four-link piece. Five: Give the single link. Six: Retrieve the single link and give the two-link piece. Seven: Give the single link.

48 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-48 Software Verification Proof of correctness –Assertions Check if the assertions so established at the end of the program corresponds to the desired output specifications –E.g., loop invariants for the while structure: an assertion at a point in a loop that is true every time that point in the loop is reached. Check if the loop invariant and termination condition imply the desired output Testing: use cases

49 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-49 Figure 5.23 The assertions associated with a typical while structure

50 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-50 Consider the insertion sort algorithm again The loop invariant: Each time the test for termination is performed, the entries in the list from position 1 through position N-1 are sorted. The termination condition: The value of N is greater than the length of the list.

51 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-51 Another Example Assuming the precondition that the value associated with N is a positive integer, establish a loop invariant that leads to the conclusion that if the following routine terminates, then Sum is assigned the value 0+1+…+N. Sum <- 0; K <- 0; While (K < N) do ( K <- K+1; Sum <- Sum + K)

52 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-52 Another Example Loop invariant: “Sum=1+2+…+K and K less than or equal to N” is true each time the test for termination is conducted. Termination condition: “K greater than or equal to N” Sum <- 0; K <- 0; While (K < N) do ( K <- K+1; Sum <- Sum + K)


Download ppt "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J."

Similar presentations


Ads by Google