Data Structures Through C

Slides:



Advertisements
Similar presentations
한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
Advertisements

한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Analysis of Computer Algorithms
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
STATISTICS POINT ESTIMATION Professor Ke-Sheng Cheng Department of Bioenvironmental Systems Engineering National Taiwan University.
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Zabin Visram Room CS115 CS126 Searching
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Chapter 7: Arrays In this chapter, you will learn about
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Recursion.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
Copyright © Cengage Learning. All rights reserved.
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Chapter 10: Applications of Arrays and the class vector
Data Structures: A Pseudocode Approach with C
Abstract Data Types and Algorithms
Data Structures Using C++
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Hash Tables.
Association Rule Mining
College of Information Technology & Design
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Starting Out with Java: From Control Structures through Objects
PSSA Preparation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Topic 16 Sorting Using ADTs to Implement Sorting Algorithms.
Chapter 13 Recursion, Complexity, and Searching and Sorting
Asymptotics Section 3.2 of Rosen Spring 2010 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Copyright © Cengage Learning. All rights reserved.
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Program Performance & Asymptotic Notations CSE, POSTECH.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Chapter 19: Searching and Sorting Algorithms
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
CSC 211 Data Structures Lecture 13
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structure Introduction.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Algorithm Analysis (Big O)
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Algorithm Analysis 1.
Computation.
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Applied Discrete Mathematics Week 6: Computation
Discrete Mathematics 7th edition, 2009
Presentation transcript:

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

RECURSION AND LINEAR SEARCH UNIT - I RECURSION AND LINEAR SEARCH

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

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.

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

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.

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.

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.

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.

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

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 Time Complexity of Program Segment B is O(n).

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 n2 n ………………………… 3n2 +3n+1 Time Complexity of Program Segment C is O(n2).

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 n0 and a positive number c such that |f(n)| ≤ c |g(n)| for all n ≥ n0. Here g(n) is the upper bound of the function f(n). f(n) g(n) 16n3 + 45n2 + 12n n3 f(n) = O(n3 ) 34n – 40 n f(n) = O(n) 50 1 f(n) = O(1)

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

Asymptotic Notations Theta(Θ): f(n) = Θ(g(n)) (read as f of n is theta of g of n), if there exists a positive integer n0 and two positive constants c1 and c2 such that c1 |g(n)| ≤ |f(n)| ≤ c2 |g(n)| for all n ≥ n0. The function g(n) is both an upper bound and a lower bound for the function f(n) for all values of n, n ≥ n0 f(n) g(n) 16n3 + 30n2 – 90 n2 f(n) = Θ(n2 ) 7. 2n + 30n 2n f(n) = Θ (2n)

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)). f(n) g(n) 18n + 9 n2 f(n) = o(n2) since f(n) = O(n2 ) and f(n) ≠ Ω(n2 ) however f(n) ≠ O(n).

Time Complexity Complexity Notation Description Constant O(1) Constant number of operations, not depending on the input data size. Logarithmic O(logn) Number of operations proportional of log(n) where n is the size of the input data. Linear O(n) Number of operations proportional to the input data size. Quadratic O(n2 ) Number of operations proportional to the square of the size of the input data. Cubic O(n3 ) Number of operations proportional to the cube of the size of the input data. Exponential O(2n) Exponential number of operations, fast growing. O(kn ) O(n!)

Time Complexities of various Algorithms

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

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

Towers of Hanoi Tower(N, Beg, Aux, End) If N=1, then: Write: Beg -> End Return [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

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

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).

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

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); }

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)

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).

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; }

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.

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.

Fibonacci Search Algorithm Let k be defined as an element in F, the array of Fibonacci numbers. n = Fm is the array size. If the array size is not a Fibonacci number, let Fm be the smallest number in F that is greater than n. The array of Fibonacci numbers is defined where Fk+2 = Fk+1 + Fk, when k ≥ 0, F1 = 1, and F0 = 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.

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