Você está na página 1de 38

Data Structure

11

Unit - 1 Analysis of Algorithm


Click to edit Master subtitle style
Presented By Dr. Jigar Patel KIM-MCA, Kalol
2/2/13

Data + Structure
z

Data

Data is collection of numbers, alphabets and symbols combine to represent information. The value 545, A, JVP etc. are example of the data. Way of organization. Possible ways to define logically related 2/2/13 Dr. JIGAR PATEL 22 data item or atom called the Data

Structure

Data Structure

Example of Insertion Sort


void Insertion_Sort (int *a, int no) { int i=0,j=0,key=0; for(i=1; i<no; i++) { key=a[i]; j=i; while(j>0 && a[j-1]>key) {
Dr. JIGAR PATEL
2/2/13

33

Pseudo Code Convention


z

Insertion-Sort(A) for j 0 to length[A] do key A[j]

// Insert A[j] into the sort Sequences A[1j-1] i1 while i > 0 and A[i-1] > key do A[i+1] a[i] i i -1
Dr. JIGAR PATEL
2/2/13

44

Algorithm
z

The word Algorithm comes from the name of Persian author Abu Jafar Mohammad ibn Musba al Khowarizmi. It is method for solving problem in Computer Science and Applications

Dr. thus a An algorithm isJIGAR PATEL

2/2/13

55

Algorithm
Finiten ess

Effective ness

Algorithm

Definite ness

Output

Input

Dr. JIGAR PATEL

2/2/13

66

More on Algorithm
z

Definiteness

Every steps of algorithm should be clear and not have any ambiguity Every algorithm have a proper end. (Avoid Infinite condition) Easy to understand and easy for the implementation for any PL.
Dr. JIGAR PATEL
2/2/13

Finiteness

Effectiveness

77

Algorithmic Strategies
z

Divide and Conquer

Problem divide into subproblems to solve the entire problem The problems is divided into smaller instances and result of reoccurring instances are obtained to solve the problem Choose the best possible solution to obtain the final solution. 2/2/13 Dr. JIGAR PATEL

Dynamic Programming

Greedy Technique

88

Analysis of Algorithms
z

Efficiency measure

how long the program runs time complexity how much memory it uses space complexity

Why analyze at all?

Decide what algorithm to implement before actually doing it


Dr. JIGAR PATEL
2/2/13

99

Analysis of Algorithms
z

Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. we shall assume a generic oneprocessor, random-access machine (RAM) model of computation as our implementation technology and understand that our algorithms will be implemented as computer programs. 2/2/13
Dr. JIGAR PATEL

10

Insertion Sort Analysis


z

The running time of an algorithm on a particular input is the number of primitive operations or "steps executed. For each j = 2, 3, . . . , n, where n = length[A], we let tj be the number of times the while loop test in line 5 is executed for that value of j.
Dr. JIGAR PATEL
2/2/13

11

Analysis of Insertion Sort

Dr. JIGAR PATEL

2/2/13

12

Analysis of Insertion Sort

Thus tj = 1 for sorted sequence

T(n) = c1n + c2 (n - 1) + c4 (n - 1) + c5 (n - 1) + c8 (n - 1) = (c1 + c2 + c4 + c8)n - (c2 + c4 + c5 + c8).


z

This runningDr. JIGAR PATEL expressed 13 time can be as 2/2/13

Analysis of Insertion Sort


z

If Array is reverse then tj = j

And

We find that in the worst case, the running time of INSERTION-SORT is.
Dr. JIGAR PATEL
2/2/13

14

Analysis of Insertion Sort

This worst-case running time can be expressed as an2 + bn+ c for constants a, b, and c that again depend on the statement costs ci; it is thus a quadratic function of n
Dr. JIGAR PATEL
2/2/13

15

Designing algorithms
z

Many useful algorithms are recursive in structure: The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.
Dr. JIGAR PATEL
2/2/13

16

Merge Sort
z

The merge sort algorithm closely follows the divide-and-conquer paradigm. Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted 2/2/13 17 answer. Dr. JIGAR PATEL

Merge Sort
z

MERGE-SORT(A,p,r)

1 if p < r 2 then q [(p + r)/2] 3 4 5 MERGE-SORT(A,p,q) MERGE-SORT(A, q + 1, r) MERGE(A,p,q,r)

Dr. JIGAR PATEL

2/2/13

18

Merge Sort

Dr. JIGAR PATEL

2/2/13

19

Other Functions
z

Complexity as a function of input size n


T(n) = 4n + 5 T(n) = 0.5 n log n - 2n + 7 T(n) = 2n + n3 + 3n

What happens as n grows?


Dr. JIGAR PATEL
2/2/13

20

Why Asymptotic Analysis?


z

Most algorithms are fast for small n

Time difference too small to be noticeable External things dominate (OS, disk I/O, )

BUT n is often large in practice

Databases, internet, graphics etc. in which n is very large.


Dr. JIGAR PATEL
2/2/13

21

Exercise - Searching
2 3 5 16 37 50 73 75 126

boolean ArrayFind(int array[], int n, int key){

// Insert your code here

} Dr. JIGAR PATEL


2/2/13

22

Solution - I
boolean LinearArrayFind(int array[],int n, int key ) { for( int i = 0; i < n; i++ ) { if( array[i] == key ) // Found it! return true; }
Dr. JIGAR PATEL
2/2/13

23

Solution - II
boolean BinArrayFind( int array[], int low, int high, int key ) { // The subarray is empty if( low > high ) return false; int mid = (high + low) / 2; if( key == array[mid] ) { return true; } Dr. JIGAR PATEL

2/2/13

24

Asymptotic Notation
z

Asymptotic notations are convenient for describing the worst-case running time function T(n), which is defined only on integer input size. Let n be a non-negative integer representing the size of the input to an algorithm
Dr. JIGAR PATEL
2/2/13 25 25

Asymptotic Notation
z

Let f(n) and g(n) be two positive functions, representing the number of basic calculations (operations, instructions) that an algorithm takes (or the number of memory words an algorithm needs). - Big Theta O - Big O - Big Omega PATEL Dr. JIGAR
2/2/13 26 26

z z z

- Notation
For a given function g(n), we denote by (g(n)) the set of functions (g(n)) = {f(n) : there exist positive constants c1 , c2 , and n0 such that 0 c1 g(n) f(n) c2 g(n) for all n n0
Dr. JIGAR PATEL
2/2/13

27 27

Example
z

(g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, 0 c1g(n) f(n) c2g(n) }

1/2n2 - 3n = (n2) Determine the positive constant n0, c1, and c2.
Dr. JIGAR PATEL
2/2/13 28 28

Example Cont ...


z

c1 n2 1/2n2 - 3n c2 n2 n n0 c1 1/2 3/n c2 (Divide by n2) c1 = 1/14

c2 =

n0 = 7
2/2/13 29 29

1/2n2 - 3n = (n2)

Dr. JIGAR PATEL

O-Notation
z

For a given function g(n)

O(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 f(n) c g(n) for all n n0 }
z

Set of all functions whose rate of growth is the same as or lower than that of c. g(n)
Dr. JIGAR PATEL
2/2/13 30 30

O-Notation

g(n) is an asymptotic upper bound for f(n).

Dr. JIGAR PATEL

2/2/13

31 31

Big-O Notation (Examples)


z

f(n) = 5n+2 = O(n) // g(n) = n

f(n) 6n, for n 3 (C=6, n0=3) f(n) 0.5 n for n 0 (C=0.5, n0=0)

f(n)=n/2 3 = O(n)

n2-n = O(n2)

// g(n) = n2

n2-n n2 for n 0 (C=1, n0=0) n(n+1)/2 n2 for n 0 (C=1, n0=0)


Dr. JIGAR PATEL
2/2/13 32 32

n(n+1)/2 = O(n2)

- Notation
z For a given function g(n) z (g(n)) = {f(n) : there exist
positive constants c and n0 such that 0 c g(n) f(n) for all n n0}
Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n). Dr. JIGAR PATEL
2/2/13 33 33

- Notation

g(n) is an asymptotic lower bound for f(n).

Dr. JIGAR PATEL

2/2/13

34 34

Relations Between ,O,

Dr. JIGAR PATEL

2/2/13

35 35

Relations Between , O,
z

For any two function f(n) and g(n), we have f(n) = (g(n)) if and only if f(n) = O(g(n)) and f(n) = (g(n)) That is (g(n)) = O(g(n)) (g(n))

z z

Dr. JIGAR PATEL

2/2/13

36 36

The Growth of Functions


z

Listed from slowest to fastest growth:


1 log n n n log n n2 n3 2n n! T( n)

2 n

n 2

n log2 n

Problem Size Dr. JIGAR PATEL

log 2n

2/2/13

37 37

Assignment
z

What is Data Structure? What are the important elements of Data Structure? Define the term: Algorithm. What should be the properties of Algorithm. How can you analyze the Algorithm using Asymptotic Notation? Explain 2/2/13 38 Dr. with example. JIGAR PATEL

Você também pode gostar