Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
1
Chapter 22 Developing Efficient Algorithms Liang, Introduction to - - PowerPoint PPT Presentation
Chapter 22 Developing Efficient Algorithms Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1 rights reserved. Executing Time Suppose two algorithms perform the same task such as search (linear
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
2
§ First, there are many tasks running concurrently on a computer.
§ Second, the execution time is dependent on specific input.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
3
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
4
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
5
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
6
f(n) n 100 200 n n/2 100n 100 200 50 100 10000 20000 2 2 2 f(200) / f(100)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
7
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
8
1 ) 1 ( 3 2 1 1 ) 1 ( 3 2 1
+ − + − n n n n n n
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
9
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
10
Ignore multiplicative constants (e.g., “c”).
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
11
Ignore multiplicative constants (e.g., “c”).
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
12
Ignore non-dominating terms
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
13
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
14
for (i = 1; i <= n; i++) { for (j = 1; j <= 20; j++) { k = k + i + j; } }
for (j = 1; j <= 10; j++) { k = k + 4; }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
15
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
16
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
17
Recurrence Relation Result Example
) 1 ( ) 2 / ( ) ( O n T n T + = ) (log ) ( n O n T =
Binary search, Euclid’s GCD
) 1 ( ) 1 ( ) ( O n T n T + − = ) ( ) ( n O n T =
Linear search
) 1 ( ) 2 / ( 2 ) ( O n T n T + = ) ( ) ( n O n T = ) ( ) 2 / ( 2 ) ( n O n T n T + = ) log ( ) ( n n O n T =
Merge sort (Chapter 24)
) log ( ) 2 / ( 2 ) ( n n O n T n T + = ) log ( ) (
2 n
n O n T = ) ( ) 1 ( ) ( n O n T n T + − = ) ( ) (
2
n O n T =
Selection sort, insertion sort
) 1 ( ) 1 ( 2 ) ( O n T n T + − = ) 2 ( ) (
n
O n T =
Towers of Hanoi
) 1 ( ) 2 ( ) 1 ( ) ( O n T n T n T + − + − = ) 2 ( ) (
n
O n T =
Recursive Fibonacci algorithm
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
18
3 2 n
2
3
n
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
19
3 2 n
O(1) O(logn) O(n) O(nlogn) O(n2) O(2n)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
20