1 / 57

Class exam #1

Class exam #1 . Chapters 1 through 4 Monday, June 28 th. 4.5 Recursive Structures. Alternative to loop paradigm for repetitive structures Loop repeats set of instruction – set is completed and them repeated Recursion repeats set of instructions as a sub-task of itself.

gino
Download Presentation

Class exam #1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class exam #1 Chapters 1 through 4 Monday, June 28th

  2. 4.5 Recursive Structures Alternative to loop paradigm for repetitive structures Loop repeats set of instruction – set is completed and them repeated Recursion repeats set of instructions as a sub-task of itself Phone calls with call-waiting call2 call0 call1 --- --- --- --- --- recv.call2 --- recv.call1 --- --- --- --- --- --- end call1 --- end call0 end call2

  3. More recursion Binary search - divide and conquer Searching a dictionary – how ? by narrowing the search find the “middle” entry what if no middle? -- use first entry in 2nd half of list

  4. Fig.4.12a: Applying our strategy to search a list for the entry John

  5. Fig.4.12b: Applying our strategy to search a list for the entry Henry 2nd sublist 3rd sublist Irene John Kelly Irene empty sublist

  6. Fig.4.13: A first draft of the binary search technique

  7. Fig.4.14: The binary search algorithm in pseudo-code Note use of abstract tool Procedure contains reference to itself

  8. Fig. 4.15 Looking for Bill Now have two copies of the program each with own data. Original copy temporarily suspended.

  9. Fig. 4.16a Looking for David

  10. Fig.4.16b Still looking for David

  11. Fig. 4.16c Where is David? Returns “not found” to previous activation & this version is discarded. Answer “percolates” back up through all previous activations to the first activation which gives results to user.

  12. Binary search, sequential sort “binary search”  use divide-by-two approach What must be done to the data before we apply the binary search ? What about for the sequential sort ? What is max. number of entries that must be interrogated with a list of length of N=100? 2X = N Solve for X. (more later)

  13. Recursive control return results activation0 activation1 activation2 --- --- --- --- request activ.2 --- request activ.1 --- --- --- --- base case found --- --- --- end activation0 end activation1 end activation2 return results results

  14. More Recursive control activation0 activation1activation2 --- --- --- --- request activ.2 --- request activ.1 --- --- --- --- base case found --- --- --- end activation0 end activation1 end activation2 • - Sub-task of previous stage • - Activation of a procedure • - Telescoping manner • Recursive control also involves  • initialize – modify – test for termination • - Base case (or degenerative case)  WHY MUST WE HAVE? • - Each activation closer to termination condition • - Terminate once target is found or find empty list

  15. What happens if no base case reached? Do we run out of CPU time? Do we run out of memory?

  16. 4.6 Efficiency and Correctness As you develop algorithms consider efficiency & correctness First Algorithm Efficiency

  17. Efficiency Algorithm Efficiency Machines are much faster – no problem, right? BUT Registrar - 30,000 students listed sequentially Compare search algorithms - sequential & binary

  18. More on Efficiency sequential search – with 30,000 what is average depth of search? 30,000 / 2 = 15,000 N / 2 10 milliseconds per comparison  2.5 minutes

  19. Efficiency binary search – with 30,000 what is average depth of search? 30,000 then 15,000 then 7500 then 3750 … can find with at most 15 comparisons 10 milliseconds per comparison  0.15 seconds

  20. Efficiency binary search  0.15 seconds sequential search  2.5 minutes both searching 30,000 entries at 10 milliseconds per retrieve and check

  21. Efficiency Algorithm analysis – important area of computer science Algorithm analysis – study of resources – time or storage space that algorithms require Algorithm analysis – applied to evaluate or compare different algorithms Want a basis by which to compare algorithms when operating on lists of arbitrary length.

  22. Efficiency Algorithm analysis – applied to evaluate or compare different algorithms Want a basis by which to compare algorithms when operating on lists of arbitrary length. • formula that indicates algorithm’s performance Analyses involve – best-case, worst-case, average-case

  23. Efficiency Analysis of sequential search – average-case Analysis of binary search – worst-case with a list of n entries – sequential searchinterrogates average of n/2 entries binary search – average of lg n entries at worst (lg n represents base two logarithm of n)

  24. Efficiency -- insertion sort analysis – select a list entry pivot, compare entry to predecessors until proper place for pivot is found, insert pivot in this place – Count number of comparisons when sorting list of length n best case – each pivot in proper place  (n – 1) comparisons worst case –list in reverse order  (n2 – n) / 2 average case – half pivots in proper place  (n2 – n) / 4

  25. See problem 2 pg. 189 -- list of 100,000 entries 2X = 100,000 Log 2 100,000 = X X = Log 2 100,000 = Log 100,000 / Log 2 X = 5 / 0.3 = 16.666… ≈ 17 217 = 131,072 a X = N Log a N = X Log a N = Log N / Log a What about a list of 200 entries? (make notes – not in slides)

  26. Efficiency -- insertion sort analysis worst case – list in reverse order (n2 – n) / 2 When searching worst case list with n entries have 1 + 2 + 3 + … + (n-1) or (n2 - n) / 2 comparisons worst-case with 10 entries (102 – 10) / 2  45 see Fig. 4.18

  27. Fig. 4.18: Applying the insertion sort in a worst-case situation

  28. Efficiency -- insertion sort analysis So if best case is the list already sorted and worst case is all entries in list in reverse order what is average case? worst case is (n2 - n) / 2 comparisons so expect average case to be half of worst case (n2 - n) / 4 comparisons Number of comparisons give approximation of time required to execute

  29. Efficiency Number of comparisons give approximation of time required to execute Next examine graphs in Figs. 4.19 & 4.20

  30. Fig. 4.19: Graph of the worst-case analysis of the insertion sort algorithm worst case is (n2 - n) / 2 as list length increases by uniform increments time increases by increasingly greater amounts

  31. Fig. 4.20: Graph of the worst-case analysis of the binary search algorithm as list length increases by uniform increments time increases by decreasing increments worst case is lg n

  32. Efficiency What is distinguishing factor between Figs. 4.19 and 4.20? (-- the general shape of the curve) Shape of graph determined by type of expression. - linear expressions  straight lines i.e., (n – 1) - quadratic expressions  parabolic curves i.e., (n2 – n) / 2 - logarithmic expressions  logarithmic shape i.e., lg n quadratic logarithmic

  33. Efficiency parabolic shape logarithmic shape lg n n2 Identify curves with simplest expression that produces the shape Shape reflects efficiency characteristics of algorithm the curve represents.

  34. Efficiency parabolic shape logarithmic shape lg n n2 Classify algorithms according to shapes of these graphs. (generally worst-case) use “big-theta notation”

  35. Efficiency “big-theta notation” – for classifying algorithms - algorithms with parabolic shaped graphs such as insertion sort are classified as Θ(n2) -- “big theta n squared” - algorithms with logarithmic shaped graphs such as binary search are classified as Θ(lg n) -- “big theta log n”

  36. Efficiency Knowing classification of algorithms allows - predicting performance - comparing with other algorithms that solve same problem Two algorithms both classified as Θ (n2) will exhibit similar time requirements as size of inputs increase. Time requirement of algorithm in Θ (lg n) time will not expand as rapidly as that of algorithm in Θ (n2) as input sizes increase.

  37. Efficiency Θ (n2) Θ (lg n) time time size of input size of input Which algorithm would you prefer?

  38. Software verification 4th phase of Polya’s analysis of problem solving – evaluating for accuracy A traveler with a gold chain of seven links must stay in 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?

  39. Fig. 4.21: Separating the chain using only three cuts We think this is the correct solution.

  40. Fig. 4.22: Solving the problem with only one cut this is the “correct” solution because ….

  41. Because … 1st morning – Give hotel the single link. 2nd morning – Retrieve single link and give hotel the two- link piece 3rd morning – Give hotel the single link. 4th morning – Retrieve three links held by hotel and give hotel four-link piece. 5th morning – Give hotel single link. 6th morning – Retrieve single link and give hotel double- link piece 7th morning – Give hotel single link.

  42. “correct” solution Software verification 1st answer we thought was correct is not correct – so what does this have to do with programming? Shows difference between program we thought was correct and a program that is correct.

  43. Software verification Verification of software – important Desire to protect ourselves from inaccurate conclusions – remember the gold rings? Search for efficient verification techniques ongoing active field of research. Attempting to apply formal logic techniques to prove correctness of programs Note phrase “prove correctness”

  44. Software verification Formal proof of program’s correctness based on specifications by which program designed. To prove that program correctly sort names we are allowed to assume that input consists of list of names. Step 1. Proof of correctness begins with assumption that certain conditions called preconditions are satisfied at beginning of execution.

  45. Software verification Step 2. Consider how consequences of preconditions propagate through program example: certain statement about value of Y is known to be true prior to executing X  Y then same can be said of X after the executions

  46. Software verification also examine if-then-else structure if (condition) then (instruction 1) else (instruction 2) If statement holds before executing the structure then immediately before executing instruction 1 we know that both statement and condition tested are true, but if instruction 2 is to be executed we know the statement and negation of condition are true.

  47. Software verification Step 3 Identify statements called assertions that can be established at various points in program Results is collection of assertions – each a consequence of program’s preconditions and sequence of instructions that lead to point in program where assertion is established. If assertions at end of program correspond to desired output specification can conclude that program is correct.

  48. Fig. 4.23: The assertions associated with a typical while structure As consequence of preconditions given at A we establish that assertion is true each time test for termination is done (at B) – this assertion is the loop invariant. If repetition ever terminates execution moves to point C, and we conclude that loop invariant and termination condition hold true. If these combined statements imply desired output proof of correctness can be completed by showing that initialization and modification of loop eventually lead to termination condition.

More Related