1 / 17

RAIK 283: Data Structures & Algorithms

RAIK 283: Data Structures & Algorithms. Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012. * slides refrred to http://www.aw-bc.com/info/levitin. Time efficiency of nonrecursive algorithms. Steps in mathematical analysis of nonrecursive algorithms:.

Télécharger la présentation

RAIK 283: Data Structures & Algorithms

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. RAIK 283: Data Structures & Algorithms Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 *slides refrred to http://www.aw-bc.com/info/levitin

  2. Time efficiency of nonrecursive algorithms • Steps in mathematical analysis of nonrecursive algorithms:

  3. Time efficiency of nonrecursive algorithms • Steps in mathematical analysis of nonrecursive algorithms: • Decide on parameter n indicating input’s size • Identify algorithm’s basic operation • Determine worst, average, & best case for inputs of size n • Set up summation for C(n) reflecting algorithm’s loop structure • Simplify summation using standard formulas (see Appendix A)

  4. Series • Proof by Gauss when 9 years old (!):

  5. General rules for sums

  6. General rules for sums

  7. Examples: • Matrix multiplication • Section 2.3 • Selection sort • Section 3.1 • Insertion sort • Section 4.1

  8. - - - - = - - - Matrix multiplication • MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1]) • Input: two n-by-n matrices A and B • Output: C = A * B

  9. Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order

  10. Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order • Selection Sort • E.g. “3, 7, 8, 2”

  11. - - - - - Selection sort

  12. Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order • Insertion Sort • E.g. “5, 2, 9, 1”

  13. - - - - - - Insertion sort

  14. In-class exercises • P67 2.3.1 (c) & (d) • P68 2.3.11 (a) & (b)

  15. In-Class Exercises • Problem 12: Door in a wall You are facing a wall that stretches infinitely in both directions. There is a door in the wall, but you know neither how far away nor in which direction. You can see the door only when you are right next to it. Design an algorithm that enables you to reach the door. How many steps will it require? • Please analyze the following two solutions of the problem.

  16. Solution 1 • Walk right and left going each time one step farther from the initial position. A simple implementation of this idea is to do the following until the door is reached: For i = 0, 1, ..., make i steps to the right, return to the initial position, make i steps to the left, and return to the initial position again. • How many steps will this algorithm require to find the door? • Does it require walking at most O(n) steps where n is the (unknown to you) number of steps between your initial position and the door.

  17. Solution 2 • Walk intermittently right and left going each time exponentially farther from the initial position. A simple implementation of this idea is to do the following until the door is reached: For i = 0, 1, ..., make 2i steps to the right, return to the initial position, make 2i steps to the left, and return to the initial position again. Let 2k−1 < n ≤ 2k. • How many steps will this algorithm require to find the door? • Does it require walking at most O(n) steps where n is the (unknown to you) number of steps between your initial position and the door.

More Related