Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Through C B.Padmaja Assoc. Professor Department of IT Vardhaman College of Engineering.

Similar presentations


Presentation on theme: "Data Structures Through C B.Padmaja Assoc. Professor Department of IT Vardhaman College of Engineering."— Presentation transcript:

1 Data Structures Through C B.Padmaja Assoc. Professor Department of IT Vardhaman College of Engineering

2 RECURSION AND LINEAR SEARCH UNIT - I 2

3 Contents Preliminaries of Algorithm Algorithm Analysis and Complexity Recursion Definition and Design Methodology Types of Recursion Implementation of Recursive Algorithms Searching Techniques – Linear, Binary, Fibonacci Analyzing Search Algorithms 3

4 Algorithm Definition An Algorithm may be defined as a finite sequence of instructions each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time. The word algorithm originates from the Arabic word Algorism which is linked to the name of the Arabic Mathematician AI Khwarizmi. AI Khwarizmi is considered to be the first algorithm designer for adding numbers. 4

5 Structure of an Algorithm An algorithm has the following structure: – Input Step – Assignment Step – Decision Step – Repetitive Step – Output Step 5

6 Properties of an Algorithm Finiteness – An algorithm must terminate after finite number of steps. Definiteness – The steps of the algorithm must be precisely defined. Generality – An algorithm must be generic enough to solve all problems of a particular class. Effectiveness – The operations of the algorithm must be basic enough to be put down on pencil and paper. Input-Output – The algorithm must have certain initial and precise inputs, and outputs that may be generated both at its intermediate and final steps. 6

7 Algorithm Analysis and Complexity The performances of algorithms can be measured on the scales of Time and Space. The Time Complexity of an algorithm or a program is a function of the running time of the algorithm or a program. The Space Complexity of an algorithm or a program is a function of the space needed by the algorithm or program to run to completion. 7

8 Algorithm Analysis and Complexity The Time Complexity of an algorithm can be computed either by an – Empirical or Posteriori Testing – Theoretical or Apriori Approach The Empirical or Posteriori Testing approach calls for implementing the complete algorithm and executes them on a computer for various instances of the problem. The Theoretical or Apriori Approach calls for mathematically determining the resources such as time and space needed by the algorithm, as a function of parameter related to the instances of the problem considered. 8

9 Algorithm Analysis and Complexity Apriori analysis computed the efficiency of the program as a function of the total frequency count of the statements comprising the program. Example: Let us estimate the frequency count of the statement x = x+2 occurring in the following three program segments A, B and C. 9

10 Total Frequency Count of Program Segment A Program Statements..………………… x = x+ 2 ….………………. Total Frequency Count Frequency Count 1 10 Time Complexity of Program Segment A is O(1).

11 Total Frequency Count of Program Segment B Program Statements..………………… for k = 1 to n do x = x+ 2; end ….………………. Total Frequency Count Frequency Count (n+1) n ………………………… 3n+1 11 Time Complexity of Program Segment B is O(n).

12 Total Frequency Count of Program Segment C Program Statements..………………… for j = 1 to n do for k = 1 to n do x = x+ 2; end ….………………. Total Frequency Count Frequency Count (n+1) (n+1)n n 2 n ………………………… 3n 2 +3n+1 12 Time Complexity of Program Segment C is O( n 2 ).

13 Asymptotic Notations Big oh(O): f(n) = O(g(n)) ( read as f of n is big oh of g of n), if there exists a positive integer n 0 and a positive number c such that |f(n)| c |g(n)| for all n n 0. Here g(n) is the upper bound of the function f(n). 13 f(n)g(n) 16n n nn3n3 f(n) = O(n 3 ) 34n – 40nf(n) = O(n) 501f(n) = O(1)

14 Asymptotic Notations Omega(Ω): f(n) = Ω(g(n)) ( read as f of n is omega of g of n), if there exists a positive integer n 0 and a positive number c such that |f(n)| c |g(n)| for all n n 0. Here g(n) is the lower bound of the function f(n). 14 f(n)g(n) 16n 3 + 8n 2 + 2n3n3 f(n) = Ω (n 3 ) 24n +9nf(n) = Ω (n)

15 Asymptotic Notations Theta(Θ): f(n) = Θ(g(n)) (read as f of n is theta of g of n), if there exists a positive integer n 0 and two positive constants c 1 and c 2 such that c 1 |g(n)| |f(n)| c 2 |g(n)| for all n n 0. The function g(n) is both an upper bound and a lower bound for the function f(n) for all values of n, n n 0 15 f(n)g(n) 16n n 2 – 90n2n2 f(n) = Θ(n 2 ) 7. 2 n + 30n2n2n f(n) = Θ (2 n )

16 Asymptotic Notations Little oh(o): f(n) = O(g(n)) ( read as f of n is little oh of g of n), if f(n) = O(g(n)) and f(n) Ω(g(n)). 16 f(n)g(n) 18n + 9n2n2 f(n) = o(n 2 ) since f(n) = O(n 2 ) and f(n) Ω(n 2 ) however f(n) O(n).

17 Time Complexity ComplexityNotationDescription ConstantO(1)Constant number of operations, not depending on the input data size. LogarithmicO(logn)Number of operations proportional of log(n) where n is the size of the input data. LinearO(n)Number of operations proportional to the input data size. QuadraticO(n 2 )Number of operations proportional to the square of the size of the input data. CubicO(n 3 )Number of operations proportional to the cube of the size of the input data. ExponentialO(2 n )Exponential number of operations, fast growing. O(k n ) O(n!) 17

18 Time Complexities of various Algorithms 18

19 Recursion Examples Factorial Function Factorial(Fact, N) 1. If N = 0, then set Fact :=1, and return. 2. Call Factorial(Fact, N-1) 3. Set Fact := N* Fact 4. Return 19

20 Fibonacci Sequence Fibonacci(Fib, N) 1.If N=0 or N=1, then: Set Fib:=N, and return. 2.Call Fibonacci(FibA, N-2) 3.Call Fibonacci(FibB, N-1) 4.Set Fib:=FibA + FibB 5.Return 20

21 Towers of Hanoi Tower(N, Beg, Aux, End) 1.If N=1, then: (a)Write: Beg -> End (b)Return 2.[Move N-1 disks from peg Beg to peg Aux] Call Tower(N-1, Beg, End, Aux) 3. Write: Beg -> End 4. [Move N-1 disks from peg Aux to peg End] Call Tower(N-1, Aux, Beg, End) 5. Return 21

22 Basic Searching Methods Search: A search algorithm is a method of locating a specific item of information in a larger collection of data. There are three primary algorithms used for searching the contents of an array: – Linear or Sequential Search – Binary Search – Fibonacci Search 22

23 Linear Search Begins search at first item in list, continues searching sequentially(item by item) through list, until desired item(key) is found, or until end of list is reached. Also called sequential or serial search. Obviously not an efficient method for searching ordered lists like phone directory(which is ordered alphabetically). 23

24 Linear Search contd.. Advantages – Algorithm is simple. – List need not be ordered in any particular way. Time Complexity of Linear Search is O(n). 24

25 Recursive Linear Search Algorithm int linearSearch(int a[], int n, int key) { if (n < 0 ) return -1; if(key == a[n-1]) return n - 1; linearSearch(a, n-1, key); } 25

26 Binary Search List must be in sorted order to begin with Compare key with middle entry of list – For lists with even number of entries, either of the two middle entries can be used. Three possibilities for result of comparison – Key matches middle entry --- terminate search with success – Key is greater than middle entry ---matching entry(if exists) must be in upper part of list (lower part of list can be discarded from search) – Key is less than middle entry ---matching entry (if exists) must be in lower part of list ( upper part of list can be discarded from search) 26

27 Binary Search contd… Keep applying above 2 steps to the progressively reduced lists, until match is found or until no further list reduction can be done. Time Complexity of Binary Search is O(logn). 27

28 Recursive Binary Search Algorithm int binary_search(int a[], int low, int high, int target) { if (high < low) return -1; int middle = (low + high)/2; if (target < a[middle]) return binary_search(a, low, middle-1, target); else if (target > a[middle]) return binary_search(a, middle+1, high, target); else if (target == a[middle]) return middle; } 28

29 Fibonacci Search Fibonacci Search Technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers. Fibonacci search examines locations whose addresses have lower dispersion, therefore it has an advantage over binary search in slightly reducing the average time needed to access a storage location. 29

30 Fibonacci Search contd… Fibonacci search has a complexity of O(log(n)). Fibonacci search was first devised by Kiefer(1953) as a minimax search for the maximum (minimum) of a unimodal function in an interval. 30

31 Fibonacci Search Algorithm Let k be defined as an element in F, the array of Fibonacci numbers. n = F m is the array size. If the array size is not a Fibonacci number, let F m be the smallest number in F that is greater than n. The array of Fibonacci numbers is defined where F k+2 = F k+1 + F k, when k 0, F 1 = 1, and F 0 = 0. To test whether an item is in the list of ordered numbers, follow these steps: – Set k = m. – If k = 0, stop. There is no match; the item is not in the array. 31

32 Fibonacci Search Algorithm Contd… – Compare the item against element in F k1. – If the item matches, stop. – If the item is less than entry F k1, discard the elements from positions F k1 + 1 to n. Set k = k 1 and return to step 2. – If the item is greater than entry F k1, discard the elements from positions 1 to F k1. Renumber the remaining elements from 1 to F k2, set k = k 2, and return to step 2. 32

33 33


Download ppt "Data Structures Through C B.Padmaja Assoc. Professor Department of IT Vardhaman College of Engineering."

Similar presentations


Ads by Google