DS-chapter2 - CHAPTER 2 ALGORITHM ANALYSIS L Definition L...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 2 ALGORITHM ANALYSIS L Definition L An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria: (1) Input There are zero or more quantities that are externally supplied. (2) Output At least one quantity is produced. (3) Definiteness Each instruction is clear and unambiguous.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Definition L (continued) (4) Finiteness If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after finite number of steps. (5) Effectiveness Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in(3); it also must be feasible.
Background image of page 2
Note: A program is written in some programming language, and does not have to be finite ( e.g. an operation system ). An algorithm can be described by human languages, flow charts, some programming languages, or pseudo-code.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example H Selection Sort : Sort a set of n 1 integers in increasing order. From those integers that are currently unsorted, find the smallest and place it next in the sorted list. Where and how are they stored? Where? for ( i = 0; i < n; i++) { Examine list[i] to list[n - 1] and suppose that the smallest integer is at list[min]; Interchange list[i] and list[min]; } Sort = Find the smallest integer + Interchange it with Algorithm in pseudo-code
Background image of page 4
2.1 Mathematical Background
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Definition L T ( N ) = O( f ( N ) ) if there are positive constants c and n 0 such that T ( N ) c f ( N ) for all N n 0 . H Definition L T ( N ) = ( g ( N ) ) if there are positive constants c and n 0 such that T ( N ) c g ( N ) for all N n 0 . H Definition L T ( N ) = Θ ( h ( N ) ) if and only if T ( N ) = O( h ( N ) ) and T ( N ) = ( h ( N ) ) . H Definition L T ( N ) = o( p ( N ) ) if T ( N ) = O( p ( N ) ) and T ( N ) Θ ( p ( N ) ) .
Background image of page 6
Note: 2 n + 3 = O( n ) = O( n k 1 ) = O( 2 n ) = ⋅ ⋅ ⋅ We shall always take the smallest f ( n ). 2 n + n 2 = ( 2 n ) = ( n 2 ) = ( n ) = ( 1 ) = ⋅ ⋅ ⋅ We shall always take the largest g ( n ). Note: Compare relative rates of growth among function. here are usually points where one function is smaller or larger than the other function.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
T ( N ) = O( f ( N ) ) the function T(N) grows at a rate no faster than f(N), f(N) is an upper bound on T(N). f(N) = ( T ( N ) ) T(N) is a lower bound on f(N). Example: N 2 = O( N 3 ) , N 3 = ( N 2 ) f ( N )= N 2 , g(N) = 2 N 2 f ( N ) = O( g ( N ) ), f(N) = ( g ( N ) )
Background image of page 8
rule1 L If T 1 ( N ) = O( f ( N ) ) and T 2 ( N ) = O( g ( N ) ) , then (a) T 1 ( N ) + T 2 ( N ) = max( O( f ( N )) , O( g ( N )) ) , (b) T 1 ( N ) * T 2 ( N ) = O( f ( N ) * g ( N ) ) .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/16/2011 for the course CS 135 taught by Professor Yuechen during the Fall '08 term at Zhejiang University.

Page1 / 34

DS-chapter2 - CHAPTER 2 ALGORITHM ANALYSIS L Definition L...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online