Download presentation

Presentation is loading. Please wait.

Published byLydia Stafford Modified about 1 year ago

1
1 Nonrecursive Algorithm Analysis * Dr. Ying Lu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred tohttp://www.aw-bc.com/info/levitin

2
2 Time efficiency of nonrecursive algorithms b Steps in mathematical analysis of nonrecursive algorithms:

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

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

5
5 General rules for sums

6
6

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

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

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

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

11
11 Selection sort

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

13
13 Insertion sort

14
In-class exercises 14 b P (c) & (d) b P (a) & (b)

15
In-Class Exercises b 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? b Please analyze the following two solutions of the problem. 15

16
Solution 1 b 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. b How many steps will this algorithm require to find the door? b 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. 16

17
Solution 2 b 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 2 i steps to the right, return to the initial position, make 2 i steps to the left, and return to the initial position again. Let 2 k−1 < n ≤ 2 k. b How many steps will this algorithm require to find the door? b 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

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google