1 nonrecursive algorithm analysis * dr. ying lu [email protected] september 6, 2012 raik 283: data...

17
1 Nonrecursive Algorithm Analysis * Dr. Ying Lu [email protected] September 6, 2012 RAIK 283: Data Structures & RAIK 283: Data Structures & Algorithms Algorithms *slides refrred to http://www.aw-bc.com/info/ http://www.aw-bc.com/info/ levitin levitin

Upload: lydia-stafford

Post on 16-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

1

Nonrecursive Algorithm Analysis *

Dr. Ying [email protected]

September 6, 2012

RAIK 283: Data Structures & RAIK 283: Data Structures & AlgorithmsAlgorithms

*slides refrred to http://www.aw-bc.com/info/levitinhttp://www.aw-bc.com/info/levitin

Page 2: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

2

Time efficiency of nonrecursive Time efficiency of nonrecursive algorithmsalgorithms

Steps in mathematical analysis of nonrecursive Steps in mathematical analysis of nonrecursive algorithms:algorithms:

Page 3: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

3

Time efficiency of nonrecursive Time efficiency of nonrecursive algorithmsalgorithms

Steps in mathematical analysis of nonrecursive Steps in mathematical analysis of nonrecursive algorithms:algorithms: • Decide on parameter Decide on parameter nn indicating indicating input’s sizeinput’s size• Identify algorithm’s Identify algorithm’s basic operationbasic operation• Determine Determine worstworst, , averageaverage, & , & bestbest case for inputs of size case for inputs of size nn• Set up summation for Set up summation for C(n) C(n) reflecting algorithm’s loop reflecting algorithm’s loop

structurestructure• Simplify summation using standard formulas (see Simplify summation using standard formulas (see

Appendix A)Appendix A)

Page 4: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

4

SeriesSeries

Proof by Gauss when 9 years old (!):Proof by Gauss when 9 years old (!):

2

)1(

1

NNiS

N

i

)1(2

123...)2()1(

)1()2(...321

NNS

NNNS

NNNS

Page 5: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

5

General rules for sumsGeneral rules for sums

n

mi

c ?

Page 6: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

6

General rules for sumsGeneral rules for sums

i

ii

kki

ii

kn

kmii

n

miki

ii

ii

ii

iii

ii

n

mi

n

mi

xaxxa

aa

acca

baba

mnccc

)(

)1(1

Page 7: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

7

Examples:Examples:

Matrix multiplicationMatrix multiplication

• Section 2.3Section 2.3

Selection sortSelection sort• Section 3.1Section 3.1

Insertion sortInsertion sort• Section 4.1Section 4.1

Page 8: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

8

Matrix multiplicationMatrix multiplication

MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1])MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1]) Input: two n-by-n matrices A and BInput: two n-by-n matrices A and B Output: C = A * BOutput: C = A * B

--

-

- - - -

=

Page 9: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

Sorting problemSorting problem

Given a list of n orderable items, rearrange them Given a list of n orderable items, rearrange them in a non-decreasing orderin a non-decreasing order

9

Page 10: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

Sorting problemSorting problem

Given a list of n orderable items, rearrange Given a list of n orderable items, rearrange them in a non-decreasing orderthem in a non-decreasing order• Selection SortSelection Sort

– E.g. “3, 7, 8, 2”E.g. “3, 7, 8, 2”

10

Page 11: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

11

Selection sortSelection sort

-

--

-

-

Page 12: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

Sorting problemSorting problem

Given a list of n orderable items, rearrange Given a list of n orderable items, rearrange them in a non-decreasing orderthem in a non-decreasing order• Insertion SortInsertion Sort

– E.g. “5, 2, 9, 1”E.g. “5, 2, 9, 1”

12

Page 13: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

13

Insertion sortInsertion sort

-

--

-

-

-

Page 14: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

In-class exercisesIn-class exercises

14

P67 2.3.1 (c) & (d)P67 2.3.1 (c) & (d)

P68 2.3.11 (a) & (b)P68 2.3.11 (a) & (b)

Page 15: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

In-Class ExercisesIn-Class Exercises

Problem 12: Door in a wall You are facing a wall that Problem 12: Door in a wall You are facing a wall that stretches infinitely in both directions. There is a door in the stretches infinitely in both directions. There is a door in the wall, but you know neither how far away nor in which wall, but you know neither how far away nor in which direction. You can see the door only when you are right direction. You can see the door only when you are right next to it. Design an algorithm that enables you to reach the next to it. Design an algorithm that enables you to reach the door. How many steps will it require? door. How many steps will it require?

Please analyze the following two solutions of the problem. Please analyze the following two solutions of the problem.

15

Page 16: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

Solution 1Solution 1

Walk right and left going each time one step farther from the Walk right and left going each time one step farther from the initial position. A simple implementation of this idea is to do initial position. A simple implementation of this idea is to do the following until the door is reached: For i = 0, 1, ..., make i 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 steps to the right, return to the initial position, make i steps to the left, and return to the initial position again. the left, and return to the initial position again.

How many steps will this algorithm require to find the door?How many steps will this algorithm require to find the door? Does it require walking at most O(n) steps where n is the Does it require walking at most O(n) steps where n is the

(unknown to you) number of steps between your initial (unknown to you) number of steps between your initial position and the door.position and the door.

16

Page 17: 1 Nonrecursive Algorithm Analysis * Dr. Ying Lu ylu@cse.unl.edu September 6, 2012 RAIK 283: Data Structures & Algorithms *slides refrred to

Solution 2Solution 2

Walk intermittently right and left going each time Walk intermittently right and left going each time exponentially farther from the initial position. A simple exponentially farther from the initial position. A simple implementation of this idea is to do the following until the door implementation of this idea is to do the following until the door is reached: For i = 0, 1, ..., make 2is reached: For i = 0, 1, ..., make 2ii steps to the right, return to steps to the right, return to the initial position, make 2the initial position, make 2ii steps to the left, and return to the steps to the left, and return to the initial position again. Let 2initial position again. Let 2k−1 k−1 < n ≤ 2< n ≤ 2kk. .

How many steps will this algorithm require to find the door?How many steps will this algorithm require to find the door? Does it require walking at most O(n) steps where n is the Does it require walking at most O(n) steps where n is the

(unknown to you) number of steps between your initial (unknown to you) number of steps between your initial position and the door.position and the door.

17