170 likes | 307 Views
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:.
E N D
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:
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)
Series • Proof by Gauss when 9 years old (!):
Examples: • Matrix multiplication • Section 2.3 • Selection sort • Section 3.1 • Insertion sort • Section 4.1
- - - - = - - - 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
Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order
Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order • Selection Sort • E.g. “3, 7, 8, 2”
- - - - - Selection sort
Sorting problem • Given a list of n orderable items, rearrange them in a non-decreasing order • Insertion Sort • E.g. “5, 2, 9, 1”
- - - - - - Insertion sort
In-class exercises • P67 2.3.1 (c) & (d) • P68 2.3.11 (a) & (b)
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.
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.
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.