34
Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

Embed Size (px)

Citation preview

Page 1: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

1

Analysis of Algorithms:Methods and Examples

CSE 2320 – Algorithms and Data StructuresVassilis Athitsos

University of Texas at Arlington

Page 2: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

2

Using Limits• if is a constant, then g(N) = O(f(N)).

– "Constant" includes zero, but does NOT include infinity.

• if then g(N) = O(f(N)).

• if is a constant, then g(N) = Ω(f(N)).– Again, "constant" includes zero, but not infinity.

• if is a non-zero constant, then g(N) = Θ(f(N)).– In this definition, both zero and infinity are excluded.

Page 3: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

3

Using Limits: An Example

• Show that .

Page 4: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

4

Using Limits: An Example

• Show that .

• Let • Let .

Page 5: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

5

Using Limits: An Example

• Show that .

• Let • Let .

• In the previous slide, we showed that • Therefore, .

Page 6: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

6

Big-Oh Transitivity

• If and , then .

• How can we prove that?

Page 7: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

7

Big-Oh Transitivity

• If and , then .

• How can we prove that? Using the definition of the big-Oh notation.

• g(N) < c0 f(N) for all N > N0.

• f(N) < c1 h(N) for all N > N1.• Set:

– c2 = c0 * c1

– N2 = max(N0, N1)

• Then, g(N) < c2 h(N) for all N > N2.

Page 8: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

8

Big-Oh Hierarchy

• If , then .– Higher-order polynomials always get larger than lower-

order polynomials, eventually.

• For any , if , – Exponential functions always get larger than polynomial

functions, eventually.

• You can use these facts in your assignments.• You can apply transitivity to derive other facts, e.g.,

that .

Page 9: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

9

Using Substitutions

• If , then:

.

• How do we use that?• For example, prove that .

Page 10: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

10

Using Substitutions

• If , then:

.

• How do we use that?• For example, prove that .• Use . We get:

Page 11: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

11

Summations

• Summations are formulas of the sort:

• Computing the values of summations can be handy when trying to solve recurrences.

• Oftentimes, establishing upper bounds is sufficient, since we use big-Oh notation.

• If , then:

• Sometimes, summing to infinity give a more simple formula.

Page 12: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

12

Geometric Series

• A geometric series is a sequence Ck of numbers, such that Ck = D * Ck-1 , where D is a constant.

• How can we express C1 in terms of C0?– C1 = D * C0

• How can we express C2 in terms of C0?– C2 = D * C1 = D2 * C0

• How can we express Ck in terms of C0?– Ck = Dk * C0

• So, to define a geometric series, we just need two parameters: D and C0.

Page 13: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

13

Summation of Geometric Series• This is supposed to be a review of material you have seen in

Math courses:• Suppose that :• Finite summations:

• Infinite summations:

• Important to note: Therefore, . Why?

Page 14: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

14

Summation of Geometric Series• This is supposed to be a review of material you have seen in

Math courses:• Suppose that :• Finite summations:

• Infinite summations:

• Important to note: Therefore, . Why? – Because is independent of n.

Page 15: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

15

Summation of Geometric Series• Suppose that : The formula for finite summations is the same,

and can be rewritten as:

• This can be a handy formula in solving recurrences:• For example:

Page 16: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

16

Approximation by Integrals

• Suppose that f(x) is a monotonically increasing function:– This means that .

• Then, we can approximate summation using integral .

• Why? Because ≤ • Why? is the average value of in the interval . • For every in the interval , .Since is increasing, if then

.

Page 17: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

17

Solving Recurrences: Example 1

• Suppose that we have an algorithm that at each step:– takes O(N2) time to go over N items.– eliminates one item and then calls itself with the

remaining data.

• How do we write this recurrence?

Page 18: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

18

Solving Recurrences: Example 1

• Suppose that we have an algorithm that at each step:– takes O(N2) time to go over N items.– eliminates one item and then calls itself with the

remaining data.

• How do we write this recurrence?•

= . How do we approximate that?

Page 19: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

19

Solving Recurrences: Example 1

• We approximate using an integral:

• Clearly, is a monotonically increasing function.• So,

Page 20: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

20

Solving Recurrences: Example 2

• Suppose that we have an algorithm that at each step:– takes O(log(N)) time to go over N items.– eliminates one item and then calls itself with the

remaining data.

• How do we write this recurrence?

Page 21: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

21

Solving Recurrences: Example 2

• Suppose that we have an algorithm that at each step:– takes O(log(N)) time to go over N items.– eliminates one item and then calls itself with the

remaining data.

• How do we write this recurrence?

. How do we compute that?

Page 22: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

22

Solving Recurrences: Example 2

• We process using the fact that:

Page 23: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

23

Solving Recurrences: Example 3

• Suppose that we have an algorithm that at each step:– takes O(1) time to go over N items.– calls itself 3 times on data of size N-1.– takes O(1) time to combine the results.

• How do we write this recurrence?

Page 24: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

24

Solving Recurrences: Example 3

• Suppose that we have an algorithm that at each step:– takes O(1) time to go over N items.– calls itself 3 times on data of size N-1.– takes O(1) time to combine the results.

• How do we write this recurrence?

Note: is just a constant finite summation

Page 25: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

25

Solving Recurrences: Example 3

• Suppose that we have an algorithm that at each step:– takes O(1) time to go over N items.– calls itself 3 times on data of size N-1.– takes O(1) time to combine the results.

• How do we write this recurrence?

Page 26: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

26

Solving Recurrences: Example 4

• Suppose that we have an algorithm that at each step:– calls itself N times on data of size N/2.– takes O(1) time to combine the results.

• How do we write this recurrence?

Page 27: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

27

Solving Recurrences: Example 4

• Suppose that we have an algorithm that at each step:– calls itself N times on data of size N/2.– takes O(1) time to combine the results.

• How do we write this recurrence? Let .

Page 28: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

28

Solving Recurrences: Example 4

• Suppose that we have an algorithm that at each step:– calls itself N times on data of size N/2.– takes O(1) time to combine the results.

• How do we write this recurrence? Let .

Page 29: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

29

Solving Recurrences: Example 4

• Suppose that we have an algorithm that at each step:– calls itself N times on data of size N/2.– takes O(1) time to combine the results.

• How do we write this recurrence? Let .

Page 30: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

30

Solving Recurrences: Example 4

Substituting N for

Page 31: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

31

Solving Recurrences: Example 4

• Let (which we have just computed).

(taking previous slide into account)

Page 32: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

32

Solving Recurrences: Example 4

• Based on the previous two slides, we can conclude that the solution of: is that:

Page 33: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

33

Big-Oh Notation: Example Problem

• Is ?• Answer:

Page 34: Analysis of Algorithms: Methods and Examples CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

34

Big-Oh Notation: Example Problem

• Is ?• Answer: no!• Why? fluctuates forever between -1 and 1.• As a result, fluctuates forever between negative and

positive values.• Therefore, for every possible and , we can always

find an such that: