UNIT 1 BASICS OF AN ALGORITHM

Similar documents
Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

DESIGN AND ANALYSIS OF ALGORITHMS

INTRODUCTION TO ALGORITHMS

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

L.J. Institute of Engineering & Technology Semester: VIII (2016)

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

Assignment 1 (concept): Solutions

D.K.M.COLLEGE FOR WOMEN (AUTONOMOUS), VELLORE-1.

CS:3330 (22c:31) Algorithms

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Algorithms and Data Structures

Lecture Notes for Chapter 2: Getting Started

Midterm solutions. n f 3 (n) = 3

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Algorithm Design Techniques part I

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

DATA STRUCTURES AND ALGORITHMS

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

The divide and conquer strategy has three basic parts. For a given problem of size n,

END-TERM EXAMINATION

O(n): printing a list of n items to the screen, looking at each item once.

Outline and Reading. Analysis of Algorithms 1

CSci 231 Final Review

6. Asymptotics: The Big-O and Other Notations

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

6.001 Notes: Section 4.1

Lecture 1. Introduction

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Subset sum problem and dynamic programming

Choice of C++ as Language

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

CS521 \ Notes for the Final Exam

(Refer Slide Time: 1:27)

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Algorithm Analysis and Design

5105 BHARATHIDASAN ENGINEERING COLLEGE

Elementary maths for GMT. Algorithm analysis Part I

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Pseudo code of algorithms are to be read by.

The Running Time of Programs

Introduction to Data Structure

Algorithms and Data Structures

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

Unit-5 Dynamic Programming 2016

Lecture 2: Getting Started

(Refer Slide Time: 01.26)

We will give examples for each of the following commonly used algorithm design techniques:

Lecture 3. Recurrences / Heapsort

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

UNIT 1 ANALYSIS OF ALGORITHMS

Sorting & Growth of Functions

FINAL EXAM SOLUTIONS


Recurrences and Divide-and-Conquer

Analysis of Algorithms Part I: Analyzing a pseudo-code

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Module 2: Classical Algorithm Design Techniques

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala

EECS 2011M: Fundamentals of Data Structures

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Classic Data Structures Introduction UNIT I

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Contents. 1 Introduction. 2 Searching and Traversal Techniques. Preface... (vii) Acknowledgements... (ix)

Greedy Algorithms CHAPTER 16

Advanced Algorithms and Data Structures

15.4 Longest common subsequence

Practice Problems for the Final

INSTITUTE OF AERONAUTICAL ENGINEERING

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

SECTION 5.1. Sequences

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Lecture 7. Transform-and-Conquer

DESIGN AND ANALYSIS OF ALGORITHMS

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

CPSC 320 Midterm 2 Thursday March 13th, 2014

Introduction to Computer Science

ECE608 - Chapter 16 answers

CMSC Introduction to Algorithms Spring 2012 Lecture 16

Data Structures and Algorithms

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

Analysis of Algorithm. Chapter 2

Transcription:

UNIT 1 BASICS OF AN ALGORITHM Basics of an Algorithm Structure Page Nos. 1.0 Introduction 5 1.1 Objectives 6 1.2. Analysis and Complexity of Algorithms 6 1.3 Basic Technique for Design of Efficient Algorithms 8 1.4 Pseudo-code for algorithms 10 1.4.1 Pseudo-code convention 1.5 Mathematical Induction and Mathematical formulae for Algorithms 12 1.6 Some more examples to understand Time and Space Complexity 16 1.7 Asymptotic Notations: 20 1.7.1 The Notation O (Big Oh) 1.7.2 The Notation Ω (Big Omega ) 1.7.3 The Notation (Theta) 1.8 Some useful theorems for 26 1.9 Recurrence 29 1.9.1 Substitution method 1.9.2 Iteration Method 1.9.3 Recursion Tree Method 1.9.4 Master Method 1.10 Summary 48 1.11 Solutions/Answers 50 1.12 Further readings 61 1.0 INTRODUCTION The word Algorithm comes from the Persian author Abdullah Jafar Muhammad ibn Musa Al-khowarizmi in ninth century, who has given the definition of algorithm as follows: An Algorithm is a set of rules for carrying out calculation either by hand or on a machine. An Algorithm is a well defined computational procedure that takes input and produces output. An Algorithm is a finite sequence of instructions or steps (i.e. inputs) to achieve some particular output. Any Algorithm must satisfy the following criteria (or Properties) 1. Input: It generally requires finite no. of inputs. 2. Output: It must produce at least one output. 3. Uniqueness: Each instruction should be clear and unambiguous 4. Finiteness: It must terminate offer a finite no. of steps. Analysis Issues of algorithm is 1. WHAT DATA STRUCTURES TO USE! (Lists, queues, stacks, heaps, trees, etc.) 2. IS IT CORRECT! (All or only most of the time?) 3. HOW EFFICIENT IS IT! (Asymptotically fixed or does it depend on the inputs?) 4. IS THERE AN EFFICIENT ALGORITHM!! (i.e. P = NP or not) 5

Introduction to Algorithm 1.1 OBJECTIVES After studying this unit, you should be able to: Understand the definition and properties of an Algorithm Pseudo-code conventions for algorithm Differentiate the fundamental techniques to design an Algorithm Understand the Time and space complexity of an Algorithm Use on Asymptotic notations O (Big Oh ) Ω (Big Omega ) and Θ (Theta) Notations Define a Recurrence and various methods to solve a Recurrence such as Recursion tree or Master Method. 1.2 ANALYIS AND COMPLEXITY OF ALGORITHMS In this unit we will examine several issues related to basics of algorithm: starting from how to write a Pseudo-code for algorithm, mathematical induction, time and space complexity and Recurrence relations. Time and space complexity will be further discussed in detail in unit 2. Analysis of algorithm is a field in computer science whose overall goal is an understanding of the complexity of algorithms (in terms of time Complexity), also known as execution time & storage (or space) requirement taken by that algorithm. Suppose M is an algorithm, and suppose n is the size of the input data. The time and space used by the algorithm M are the two main measures for the efficiency of M. The time is measured by counting the number of key operations, for example, in case of sorting and searching algorithms, the number of comparisons is the number of key operations. That is because key operations are so defined that the time for the other operations is much less than or at most proportional to the time for the key operations. The space is measured by counting the maximum of memory needed by the algorithm. The complexity of an algorithm M is the function f(n), which give the running time and/or storage space requirement of the algorithm in terms of the size n of the input data. Frequently, the storage space required by an algorithm is simply a multiple of the data size n. In general the term complexity given anywhere simply refers to the running time of the algorithm. There are 3 cases, in general, to find the complexity function f(n): 1. Best case: The minimum value of for any possible input. 2. Worst case: The maximum value of for any possible input. 3. Average case: The value of which is in between maximum and minimum for any possible input. Generally the Average case implies the expected value of 6

The analysis of the average case assumes a certain probabilistic distribution for the input data; one such assumption might be that all possible permutations of an input data set are equally likely. The Average case also uses the concept of probability theory. Suppose the numbers occur with respective probabilities Then the expectation or average value of E is given by Basics of an Algorithm To understand the Best, Worst and Average cases of an algorithm, consider a linear array, where the array A contains n-elements. Students may you are having some problem in understanding. Suppose you want either to find the location LOC of a given element (say ) in the given array A or to send some message, such as LOC=0, to indicate that does not appear in A. Here the linear search algorithm solves this problem by comparing given, one-by-one, with each element in A. That is, we compare A[2], and so on, until we find LOC such that. with A[1], then Algorithm: (Linear search) /* Input: A linear list A with n elements and a searching element. Output: Finds the location LOC of in the array A (by returning an index ) or return LOC=0 to indicate is not present in A. */ 1. [Initialize]: Set K=1 and LOC=0. 2. Repeat step 3 and 4 3. 4. { 5. LOC=K 6. 7. } 8. 9. 10. 11. 12. Exit [end of algorithm] Analysis of linear search algorithm The complexity of the search algorithm is given by the number C of comparisons between x and array elements A[K]. Best case: Clearly the best case occurs when x is the first element in the array A. That is. In this case Worst case: Clearly the worst case occurs when x is the last element in the array A or is not present in given array A (to ensure this we have to search entire array A till last element). In this case, we have. 7

Introduction to Algorithm Average case: Here we assume that searched element appear array A, and it is equally likely to occur at any position in the array. Here the number of comparisons can be any of numbers, and each number occurs with the probability then It means the average number of comparisons needed to find the location of x is approximately equal to half the number of elements in array A. From above discussion, it may be noted that the complexity of an algorithm in the average case is much more complicated to analyze than that of worst case. Unless otherwise stated or implied, we always find and write the complexity of an algorithm in the worst case. There are three basic asymptotic notations which are used to express the running time of an algorithm in terms of function, whose domain is the set of natural numbers N={1,2,3,..}. These are: [This notation is used to express Upper bound (maximum steps) required to solve a problem] Ω [This notation is used to express Lower bound i.e. minimum (at least) steps required to solve a problem] Θ ( Theta ) Notations. [Used to express both Upper & Lower bound, also called tight bound] Asymptotic notation gives the rate of growth, i.e. performance, of the run time for sufficiently large input sizes and is not a measure of the particular run time for a specific input size (which should be done empirically). O-notation is used to express the Upper bound (worst case); Ω- notation is used to express the Lower bound (Best case) and Θ- Notations is used to express both upper and lower bound (i.e. Average case) on a function. We generally want to find either or both an asymptotic lower bound and upper bound for the growth of our function. The lower bound represents the best case growth of the algorithm while the upper bound represents the worst case growth of the algorithm. 1.3 BASIC TECHNIQUES FOR DESIGN OF EFFICIENT ALGORITHMS There are basically 5 fundamental techniques which are used to design an algorithm efficiently: 1. Divide-and-Conquer 2. Greedy method 3. Dynamic Programming 4. Backtracking 5. Branch-and-Bound 8

In this section we will briefly describe these techniques with appropriate examples. Basics of an Algorithm 1. Divide & conquer technique is a top-down approach to solve a problem. The algorithm which follows divide and conquer technique involves 3 steps: Divide the original problem into a set of sub problems. Conquer (or Solve) every sub-problem individually, recursive. Combine the solutions of these sub problems to get the solution of original problem. 2. Greedy technique is used to solve an optimization problem. An Optimization problem is one in which, we are given a set of input values, which are required to be either maximized or minimized (known as objective function) w. r. t. some constraints or conditions. Greedy algorithm always makes the choice (greedy criteria) that looks best at the moment, to optimize a given objective function. That is, it makes a locally optimal choice in the hope that this choice will lead to a overall globally optimal solution. The greedy algorithm does not always guaranteed the optimal solution but it generally produces solutions that are very close in value to the optimal. 3. Dynamic programming technique is similar to divide and conquer approach. Both solve a problem by breaking it down into a several sub problems that can be solved recursively. The difference between the two is that in dynamic programming approach, the results obtained from solving smaller sub problems are reused (by maintaining a table of results) in the calculation of larger sub problems. Thus dynamic programming is a Bottom-up approach that begins by solving the smaller sub-problems, saving these partial results, and then reusing them to solve larger sub-problems until the solution to the original problem is obtained. Reusing the results of sub-problems (by maintaining a table of results) is the major advantage of dynamic programming because it avoids the recomputations (computing results twice or more) of the same problem. Thus Dynamic programming approach takes much less time than naïve or straightforward methods, such as divide-and-conquer approach which solves problems in top-down method and having lots of re-computations. The dynamic programming approach always gives a guarantee to get a optimal solution. 4. The term backtrack was coined by American mathematician D.H. Lehmer in the 1950s. Backtracking can be applied only for problems which admit the concept of a partial candidate solution and relatively quick test of whether it can possibly be completed to a valid solution. Backtrack algorithms try each possibility until they find the right one. It is a depth-first-search of the set of possible solutions. During the search, if an alternative doesn t work, the search backtracks to the choice point, the place which presented different alternatives, and tries the next alternative. When the alternatives are exhausted, the search returns to the previous choice point and try the next alternative there. If there are no more choice points, the search fails. 5. Branch-and-Bound (B&B) is a rather general optimization technique that applies where the greedy method and dynamic programming fail. 9

Introduction to Algorithm B&B design strategy is very similar to backtracking in that a state-spacetree is used to solve a problem. Branch and bound is a systematic method for solving optimization problems. However, it is much slower. Indeed, it often leads to exponential time complexities in the worst case. On the other hand, if applied carefully, it can lead to algorithms that run reasonably fast on average. The general idea of B&B is a BFS-like search for the optimal solution, but not all nodes get expanded (i.e., their children generated). Rather, a carefully selected criterion determines which node to expand and when, and another criterion tells the algorithm when an optimal solution has been found. Branch and Bound (B&B) is the most widely used tool for solving large scale NP-hard combinatorial optimization problems. The following table-1 summarizes these techniques with some common problems that follows these techniques with their running time. Each technique has different running time ( time complexity). Design strategy Problems that follows Divide & Conquer Greedy Method Binary search Multiplication of two n-bits numbers Quick Sort Heap Sort Merge Sort Knapsack (fractional) Problem Minimum cost Spanning tree Kruskal s algorithm Prim s algorithm Dynamic Programming Single source shortest path problem Dijkstra s algorithm All pair shortest path-floyed algorithm Chain matrix multiplication Longest common subsequence (LCS) 0/1 Knapsack Problem Traveling salesmen problem (TSP) Backtracking N-queen s problem Sum-of subset Branch & Bound Assignment problem Traveling salesmen problem (TSP) Table 1: Important Techniques to solve problems 1.4 PSEUDO-CODE FOR ALGORITHM Pseudo-code (derived from pseudo-code) is a compact and informal high level description of a computer programming algorithm that uses the structural conventions of some programming language. Unlike actual computer language such as C,C++ or JAVA, Pseudo-code typically omits details that are not essential for understanding the algorithm, such as functions (or subroutines), variable declaration, semicolons, special words and so on. Any version of pseudo-code is acceptable as long as its instructions are unambiguous and is resembles in form. Pseudo-code is independent of 10

any programming language. Pseudo-code cannot be compiled nor executed, and not following any syntax rules. Basics of an Algorithm Flow charts can be thought of as a graphical alternative to pseudo-code. A flowchart is a schematic representation of an algorithm, or the step-by-step solution of a problem, using some geometric figures (called flowchart symbols) connected by flow-lines for the purpose of designing or documenting a program. The purpose of using pseudo-code is that it may be easier to read than conventional programming languages that enables (or helps) the programmers to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language. In fact, one can write a pseudocode for any given problem without even knowing what programming language one will use for the final implementation. Example: The following pseudo-code finds the maximum of three numbers. The first line of a function consists of the name of the function followed parentheses, in parentheses we pass the parameters of the function. The parameters may be data, variables, arrays, and so on, that are available to the function. In the above algorithm, The parameters are the three input values, and the output parameter,, that is assigned the maximum of the three input values 1.4.1 PSEUDO-CODE CONVENTIONS The following conventions must be used for pseudo-code. 1. Give a valid name for the pseudo-code procedure. (See sample code for insertion sort at the end). 2. Use the line numbers for each line of code. 3. Use proper Indentation for every statement in a block structure. 4. For a flow control statements use if-else. Always end an if statement with an end-if. Both if, else and end-if should be aligned vertically in same line. 11

Introduction to Algorithm 5. Use operator for assignments. Ex: 6. Array elements can be represented by specifying the array name followed by the index in square brackets. For example, indicates the ith element of the array A. 7. For looping or iteration use for or while statements. Always end a for loop with end-for and a while with end-while. 8. The conditional expression of for or while can be written as shown in rule (4). You can separate two or more conditions with and. 9. If required, we can also put comments in between the symbol /* and */. A simple pseudo-code for insertion sort using the above conventions: /* insert A*j+ into sorted sequence A*1 j-1] */ 1.5 MATHEMATICAL INDUCTION AND MATEMATICAL FORMULE FOR ALGORITHMS Mathematical Induction In this section we will describe what mathematical induction is and also introduce some formulae which are extensively used in mathematical induction. Mathematical induction is a method of mathematical proof typically used to establish that a given statement is true for all natural number (positive integers). It is done by proving that the first statement in the infinite sequence of statements is true, and then proving that if any one statement in the infinite sequence of statements is true, then so is the next one. For Example, let us prove that 12

Basics of an Algorithm Here the sequence of statements is: Statement1: Statement2: Statement3:.. Statement n:. Statements 1-3 are obtained by everywhere replacing n by 1 in the original equation, then n by 2, and then n by 3. Thus a Mathematical induction is a method of mathematical proof of a given formula (or statement), by proving a sequence of statements Proof by using Mathematical Induction of a given statement (or formula), defined on the positive integer N, consists of two steps: 1. (Base Step): Prove that S(1) is true 2. (Inductive Step): Assume that S(n) is true, and prove that is true for all. Example1: Prove the proposition that The sum of the first n positive integers is ; that is by induction. Proof: (Base Step): We must show that the given equation is true for (Induction Step): i.e. Hence we proved S(1) is true. this is true. Let us assume that the given equation S(n) is true for n; that is ; Now we have to prove that it is true for (n+1). 13

Introduction to Algorithm Consider S( n 1) 1 2 P( n) ( n 1) n( n 1) 2 ( n ( n 1)( n 2) 2 3... n ( n 1) 1) ( n 1)[( n 1) 1] 2 Hence S(n+1) is also true whenever S(n) is true. This implies that equation S(n) is true for all Example2: Prove the following proposition using induction: 2 2 2 2 n( n 1)(2n P( n) :1 2 3... n 6 1). Proof: (Base Step): Consider n=1, then 2 1(1 1)(2 P(1) 1 6 Hence for n=1 it is true. 1) 1.2.3 6 1 (Induction Step): Assume P(n) is true for n i.e. P( n) 2 2 2 2 n( n 1)(2n 1) 1 2 3... n. 6 Now P( n 1) 2 1 2 2 2 3... 2 n ( n 1) n( n 1)(2n 1) 2 ( n 1) 6 n( n 1)(2n 1) 6( n 2 1) 6 ( n 1) n(2n 1) 6( n 1) 6 ( n 2 1) 2n n 6n 6 6 ( n 1)( n 2)(2n 3) 6 ( n 1)( n 2) 2( n 1) 1 6 Which is P(n+1). Hence P(n+1) is also true whenever P(n) is true P(n) is true for all n. 2 14

In the subsequent section we will look at some formulae which are usually used in mathematical proof. Basics of an Algorithm Sum formula for Arithmetic Series Given a arithmetic Series: where Sum formula for Geometric Series For is a geometric or exponential series and has a value I) (when II) (when III) Logarithmic Formulae The following logarithmic formulae are quite useful for solving recurrence relation. For all real ; 1. 2. 3. 4. 5. 6. 15

Introduction to Algorithm 7. (Note: Proof is left as an exercise) Remark: Since changing the base of a logarithm from one constant to another only changes the value of the logarithm by a constant factor (see property 4), we don t care about the base of a log when we are writing a time complexity using O, Ω, or θ- notations. We always write 2 as the base of a log. For Example: 1.6 SOME MORE EXAMPLES TO UNDERSTAND TIME & SPACE COMPLEXITY Given a program and we have to find out its time complexity (i.e. Best case, Worst case and Average case). In order to do that, we will measure the complexity for every step in the program and then calculate overall time complexity. Space Complexity The Space Complexity of an algorithm is the amount of memory it needs to run to completion. The time complexity of an algorithm is the amount of computer time it needs to run to completion. The time complexity of an algorithm is given by the no. of steps taken be the algorithm to compute the function it was written for. Time Complexity The time, taken by a program P, is the sum of the Compile time & the Run (execution) time. The Compile time does not depends on the instance characteristics (i.e. no. of inputs, no. of outputs, magnitude of inputs, magnitude of outputs etc.). Thus we are concerned with the running time of a program only. 1. Algorithm X (a,b,c) Here the problem instance is characterized by the specified values of a, b, and c. 2. Algorithm SUM (a, n) S:= 0 For i = 1 to n do S = S + a [i]; Return S; 16

Here the problem instance is characterized by value of n, i.e., number of elements to be summed. Basics of an Algorithm This run time is denoted by, we have: (n) = ADD(n) + C s SUB(n) + C m MUL(n) + C d DIV(n) +. where n instance characteristics. C a, C s, C m, C d denotes time needed for an Addition, Subtraction, Multiplication, Division and so on. ADD, SUB, MUL, DIV is a functions whose values are the numbers of performed when the code for P is used on an instance with characteristics n. Generally, the time complexity of an algorithm is given by the no. steps taken by the algorithm to complete the function it was written for. The number of steps is itself a function of the instance characteristics. How to calculate time complexity of any program The number of machine instructions which a program executes during its running time is called its time complexity. This number depends primarily on the size of the program s input. Time taken by a program is the sum of the compile time and the run time. In time complexity, we consider run time only. The time required by an algorithm is determined by the number of elementary operations. The following primitive operations that are independent from the programming language are used to calculate the running time: Assigning a value to a variable Calling a function Performing an arithmetic operation Comparing two variables Indexing into a array of following a pointer reference Returning from a function The following fragment shows how to count the number of primitive operations executed by an algorithm. { This function returns the sum from 17

Introduction to Algorithm To determine the running time of this program, we have to count the number of statements that are executed in this procedure. The code at line 1 executes 1 time, at line 2 the for loop executes Line 3 executes and line 4 executes 1 time. Hence In terms of O-notation this function is Example1 Frequency Total steps Add(a, b, c, m, n) For i = 1 to m do m m For j = 1 to n do m(n) m.n c[i, j] = a[i, j] + b[i, j] m n m n 2mn+m Time Complexity= (2mn + m)= Best, Worst and Average case (Step Count) Best Case: It is the minimum number of steps that can be executed for the given parameter. Worst Case: It is the maximum no. of steps that can be executed for the given parameter. Average case: It is the Average no. of steps that can be executed for the given parameter. To better understand all of the above 3 cases, consider an example of English dictionary, used to search a meaning of a particular word. Best Case: Suppose we open a dictionary and luckily we get the meaning of a word which we are looking for. This requires only one step (minimum possible) to get the meaning of a word. Worst case: Suppose you are searching a meaning of a word and that word is either not in a dictionary or that word takes maximum possible steps (i.e. now no left hand side or right hand side page is possible to see). Average Case: If you are searching a word for which neither a Minimum (best case) nor a maximum (worst case) steps are required is called average case. In this case, we definitely get the meaning of that word. To understand the concept of Best, Average and worst case and the where to use basic notations O, Ω, and Θ, consider again another example known as Binary search algorithm. A Binary search is a well known searching algorithm which follows the same concept of English dictionary. To understand a Binary search algorithm consider a sorted linear array, Suppose you want either to find(or search) the location LOC of a given element (say ) in the given array A (successful search) or to send some message, such as LOC=0, to indicate that does not appear in A(Unsuccessful search). A Binary search algorithm is an efficient technique for finding a position of specified value (say x) within a sorted array A. The best example of binary search is dictionary, which we are using in our daily 18

life to find the meaning of any word (as explained earlier). The Binary search algorithm proceeds as follow: Basics of an Algorithm 1) Begin with the interval covering the whole array; binary search repeatedly divides the search interval (i.e. Sorted array A) in half. 2) At each step, the algorithm compares the given input key (or search) value x with the key value of the middle element of the array A. 3) If it match, then a searching element x has been found so its index, or position, is returned. Otherwise, if the value of the search element x is less than the item in the middle of the interval; then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search element x is greater than the middle element s key, then on the sub-array to the right. 4) We repeatedly check until the searched element is found (i.e. ) or the interval is empty, which indicates x is not found. Best, Worst and Average case for Binary search algorithm Best Case: Clearly the best case occurs when you divide the array 1 st time and you get the searched element. This requires only one step (minimum possible step). In this case, the number of comparison,. Worst case: Suppose you are searching a given key and that is either not in a given array or to search that element takes maximum possible steps (i.e. now no left hand side or right hand side elements in array A is possible to see). Clearly the worst case occurs, when is searched at last. Let us assume for the moment that the size of the array is a power of 2, say. Each time when we examine the middle element, we cut the size of the sub-array is half. So before the 1 st iteration size of the array is. After the 1 st iteration size of the sub-array of our interest is :. After the 2 nd iteration size of the sub-array of our interest is :. After the.iteration size of the sub-array of our interest is : So we stop now for the next iteration. Thus we have at most iterations. Since with each iteration, we perform a constant amount of work: Computing a mid point and few comparisons. So overall, for an array of size n, we perform comparisions. Thus Average Case: If you are searching given key for which neither a Minimum (best case) nor a maximum (worst case) steps are required is called average case. In this case, we definitely get the given key in the array A. In this case also 19

Introduction to Algorithm The following table summarizes the time complexity of Binary search in various cases: Cases Best: Worst: Average Suppose Element (say x) present in Array A 1 step required Element x, not present in A 1.7 ASYMPTOTIC NOTATIONS (O, Ω, and θ) Asymptotic notations have been used in earlier sections in brief. In this section we will elaborate these notations in detail. They will be further taken up in the next unit of this block. These notations are used to describe the Running time of an algorithm, in terms of functions, whose domains are the set of natural numbers, N = {1, 2, }. Such notations are convenient for describing the worst case running time function. T(n) (problem size input size). The complexity function can be also be used to compare two algorithms P and Q that perform the same task. The basic Asymptotic Notations are: 1. O(Big- Oh ) Notation. [Maximum number of steps to solve a problem, (upper bound)] 2. Ω (Big- Omega ) Notation [Minimum number of steps to solve a problem, (lower bound)] 3. (Theta) Notation [Average number of steps to solve a problem, (used to express both upper and lower bound of a given ) 1.7.1 THE NOTATION O (BIG Oh ) Big Oh Notation is used to express an asymptotic upper bound (maximum steps) for a given function. Let and are two positive functions, each from the set of natural numbers (domain) to the positive real numbers. We say that the function [read as f of n is big Oh of g of n ], if there exist two positive constants C and n 0 such that : n n 0 20

The intuition behind O- notation is shown in Figure 1. Basics of an Algorithm f(n) No Matter C.g(n F(n) Figure 1 n n 0 Figure 1 For all values of n to the right of n 0, the value of f(n) is always lies on or below To understand O-Notation let us consider the following examples: Example1.1: For the function defined by : show that Solution: (i) To show that ; we have to show that ; since we have found the required constant C and. Hence. Remark: The value of C and is not unique. For example, to satisfy the above equation (1), we can also take. So depending on the value of C, the value of is also changes. Thus any value of C and which satisfy the given inequality is a valid solution. (ii) To show that ; we have to show that ; Let C=3 21

Introduction to Algorithm Value of n 6 3 15 24 27 81...... ; Since we have found the required constant C and. Hence. (iii) To show For ; we get we have to show that (iv) To show, you have to show that. (1) There is no value of C and, which satisfy this equation (1). For example, if you take, then to contradict this inequality you can take any value greater than C, that is. Since we do not found the required constant C and to satisfy (1). Hence (v) Do yourself. Theorem: 1.7.2 THE NOTATION Ω (BIG Omega ) O- Notation provides an asymptotic upper bound for a function; Ω-Notation, provides an asymptotic lower-bound for a given function. We say that the function [read as f of n is big Omega of g of n ], if and only if there exist two positive constants C and n 0 such that : n n 0 22

Basics of an Algorithm The intuition behind Ω- notation is shown in Figure 2. f(n) No Matter f(n) = Ω g(m) n n 0 Figure 2 Note that for all values of f(n) always lies on or above g(n). To understand Ω-Notation let us consider the following examples: Example1.1: For the function defined by : show that Solution: (i) To show that ; we have to show that Since we have found the required constant C and. Hence. (ii) To show that ; we have to show that no value of C and is there which satisfy the following equation (1). We can prove this result by contradiction. 23

Introduction to Algorithm Let positive constant C and such that then there exist a But for any ; the inequality (1) is not satisfied Contradiction. Thus. (iii) To show that ; we have to show that Thus (iv) We can prove the result by contradiction. Let the inequality (1) or (2) is not satisfied Contradiction, hence proved. (v) Do yourself. 1.7.3 THE NOTATION (Theta) Θ-Notation provides simultaneous both asymptotic lower bound and asymptotic upper bound for a given function. Let and are two positive functions, each from the set of natural numbers (domain) to the positive real numbers. In some cases, we have We say that the function [read as f of n is Theta of g of n ], if and only if there exist three positive constants such that. 24

(Note that this inequality (1) represents two conditions to be satisfied simultaneously viz and Basics of an Algorithm The following figure -1 shows the intuition behind the Θ-Notation. c 2 g(n). f(n) c 1 g(n) f(n) = (g(n)) n n 0 Figure 3 Note that for all values of n to the right of the n 0 the value of f(n) lies at or above C 1 g(n) and at or below C 2.g(n). To understand Ω-Notation let us consider the following examples: Example1.1: For the function defined by : show that Solution: To show that ; we have to show that To satisfy this inequality (1) simultaneously, we have to find the value of, and, using the following inequality Inequality (2) is satisfied for Inequality (3) is satisfied for 25

Introduction to Algorithm Hence inequality (1) simultaneously satisfied for Hence (ii) We can prove this by contradiction that no value of Let Left side inequality: ; is satisfied for Right side inequality: for this inequality is not satisfied. Thus. (iii) Do yourself. 1.8 SOME USEFUL THEOREMS FOR The following theorems are quite useful when you are dealing Theorem1: If (or solving problems) with Proof: for Let us assume Then. Theorem2: If. Proof: Since 26

Basics of an Algorithm Theorem3: If Proof: From Theorem1 and Theorem2,.(1) From (1) and (2) we can say that. Example1: By applying theorem, find out the O-notation, Ω- notation and Θ- notation for the following functions. (i) (ii) Solution: (i) Here, So by Theorem1,2 and 3:, and. (ii) Theorem1,2 and 3:, So by, and. Check Your Progress 1 Q.1, Where O(n) stands for order n is: c) d) none of these Q.2: What is the running time to retrieve an element from an array of size n (in worst case): c) d) none of these Q.3: The time complexity for the following function is: c) d) Q.4: The time complexity for the following function is: 27

Introduction to Algorithm c) d) Q.5: Define an algorithm? What are the various properties of an algorithm? Q.6: What are the various fundamental techniques used to design an algorithm efficiently? Also write two problems for each that follows these techniques? Q.7: Differentiate between profiling and debugging? Q.8: Differentiate between asymptotic notations? Q.9: Define time complexity. Explain how time complexity of an algorithm is computed? Q.10: Let f(n) and g(n) be two asymptotically positive functions. Prove or disprove the following (using the basic definition of O, Ω and Θ): g) k) where 28

1.9 RECURRENCE Basics of an Algorithm There are two important ways to categorize (or major) the effectiveness and efficiency of algorithm: Time complexity and space complexity. The time complexity measures the amount of time used by the algorithm. The space complexity measures the amount of space used. We are generally interested to find the best case, average case and worst case complexities of a given algorithm. When a problem becomes large, we are interested to find asymptotic complexity and O (Big-Oh) notation is used to quantify this. Recurrence relations often arise in calculating the time and space complexity of algorithms. Any problem can be solved either by writing recursive algorithm or by writing non-recursive algorithm. A recursive algorithm is one which makes a recursive call to itself with smaller inputs. We often use a recurrence relation to describe the running time of a recursive algorithm. A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs or as a function of preceding (or lower) terms. Like all recursive functions, a recurrence also consists of two steps: 1. Basic step: Here we have one or more constant values which are used to terminate recurrence. It is also known as initial conditions or base conditions. 2. Recursive steps: This step is used to find new terms from the existing (preceding) terms. Thus in this step the recurrence compute next sequence from the k preceding values. This formula is called a recurrence relation (or recursive formula). This formula refers to itself, and the argument of the formula must be on smaller values (close to the base value). Hence a recurrence has one or more initial conditions and a recursive formula, known as recurrence relation. For example: A Fibonacci sequence can be defined by the recurrence relation 1. (Basic Step) The given recurrence says that if n=0 then and if n=1 then. These two conditions (or values) where recursion does not call itself is called a initial conditions (or Base conditions). 2. (Recursive step): This step is used to find new terms from the existing (preceding) terms, by using the formula 29

Introduction to Algorithm ; for This formula says that by adding two previous sequence (or term) we can get the next term. For example Let us consider some recursive algorithm and try to write their recurrence relation. Then later we will learn some method to solve these recurrence relations to analyze the running time of these algorithms. Example 1: Consider a Factorial function, defined as: Factorial function is defined as: /* Algorithm for computing n! Input: Output: Algorithm: FACT(n) 1: if 3: else 4: 5: Let that is denoted the number of multiplication required to execute the n!, denotes the # of times the line 4 is executed in FACT algorithm. We have the initial condition since when n=1, FACT simply return (i.e. Number of multiplication is 0). When, the line 4 is perform 1 multiplication plus FACT is recursively called with input (n-1). It means, by the definition of additional number of multiplications are required. Thus we can write a recurrence relation for the algorithm FACT as: (We can also write some constant valve instead of writing 0 and 1, that is Since when n=1 (base case), the FACT at line 1 perform one comparison and one return statement at line 2. Therefore 30

Where b is a some constant, and for line 4 (when it is The reason for writing constants b and c, instead of writing exact value (here 0 and 1) is that, we always interested to find asymptotic complexity (i.e. problem size n is large ) and O(big Oh) notation is used (for getting Worst case complexity) to quantify this. In Section 1.81-1.83 of this unit, we will learn some methods to solve these recurrence relations). Basics of an Algorithm Example2: Let denotes the number of times the statement is executed in the algorithm2. Algorithm2: Example(n) 1: if 3: 4: 5: 5: The base case is reached when The algorithm2 perform one comparison and one return statement. Therefore, When the statement executed n times at line 4. Then at line 5, is called with the input, which causes executed additional times. Thus we obtain the recurrence relation as: Example3: Let denotes the time the statement is executed in the algorithm2. Algorithm3: 1: if 3: 4: 5: 31

Introduction to Algorithm The base case is reached when The algorithm2 performs one comparison and one return statement. Therefore,, where is some constant. When the algorithm3 performs TWO recursive calls each with the parameter at line 4, and some constant number of basic operations. Thus we obtain the recurrence relation as: Example4: The following algorithm4 calculate the value of algorithm4. Let Algorithm4: 1: if 3: 4: 5: 6: 7: denotes the time complexity of the The base case is reached when The algorithm4 performs one comparison and one return statement. Therefore,, where is some constant. When the algorithm4 performs one recursive call with input parameter at line 6, and some constant number of basic operations. Thus we obtain the recurrence relation as: Example5: The worst case running time T(n) of Binary Search procedure (explained earlier) is given by the recurrence: METHODS FOR SOLVING RECURRENCE RELTIONS We will introduce three methods of solving the recurrence equation: 1. The Substitution Method (Guess the solution & verify by Induction) 32

2. Iteration Method (unrolling and summing) Basics of an Algorithm 3. The Recursion-tree method 4. Master method In substitution method, we guess a bound and then use mathematical induction to prove our guess correct. The iteration method converts the recurrence into a summation and then relies on techniques for bounding summations to solve the recurrence and the Master method provides bounds for the recurrence of the form 1.9.1 SUBSTITUTION METHOD A substitution method is one, in which we guess a bound and then use mathematical induction to prove our guess correct. It is basically two step process: Step1: Guess the form of the Solution. Step2: Prove your guess is correct by using Mathematical Induction. Example 1. Solve the following recurrence by using substitution method. Solution: step1: The given recurrence is quite similar with that of MERGE- SORT, you guess the solution is Or Step2: Now we use mathematical Induction. Here our guess does not hold for n=1 because Now for n=2 which is true. So is True for n=2 (ii) Induction step: Now assume it is true for n=n/2 Now we have to show that it is true for 33

Introduction to Algorithm We know that Thus Remark: Making a good guess, which can be a solution of a given recurrence, requires experiences. So, in general, we are often not using this method to get a solution of the given recurrence. 1.9.2 ITERATION METHOD (Unrolling and summing): In this method we unroll (or substituting) the given recurrence back to itself until not getting a regular pattern (or series). We generally follow the following steps to solve any recurrence: Expend the recurrence Express the expansion as a summation by plugging the recurrence back into itself until you see a pattern. Evaluate the summation by using the arithmetic or geometric summation formulae as given in section1.4 of this unit. Example1: Consider a recurrence relation of algorithm1 of section 1.8 Solution: Here Now substitute the value of M(n-1) in equation(2), [By substituting M(n-2) in equation (2) 34

=.. Basics of an Algorithm Example2: Consider a recurrence relation of algorithm2 of section 1.8 Solution: Solution: Here When we are solving recurrences we always omit the sealing or floor because it won t affect the result. Hence we can write the equation 2 as: Now substitute the value of in equation (3), (By substituting Thus we have a G.P. series: [Taking log both side] [ By using GP series sum formula 35

Introduction to Algorithm [ Using log property ] 1.9.3 RECURSION TREE METHOD A recursion tree is a convenient way to visualize what happens when a recurrence is iterated. It is a pictorial representation of a given recurrence relation, which shows how Recurrence is divided till Boundary conditions. Recursion tree method is especially used to solve a recurrence of the form: This recurrence (1) describe the running time of any divide-and-conquer algorithm. Method (steps) for solving a recurrence recursiontree: using Step1: We make a recursion tree for a given recurrence as follow: a) To make a recursion tree of a given recurrence (1), First put the value of at root node of a tree and make a of child node of this root value Now tree will be looks like as: number of child nodes Figure-a b) Now we have to find the value of by putting (n/b) in place of n in equation (1).That is (2) 36

Basics of an Algorithm From equation (2), now will be the value of node having branch (child nodes) each of size T(n/b). Now each in figure-a will be replaced as follows: number of child nodes. Figure-b c) In this way you can expend a tree one more level (i.e. up to (at least) 2 levels). Step2: (a) Now you have to find per level cost of a tree. Per level cost is the sum of the cost of each node at that level. For example per level cost at level1 is. This is also called Row-Sum. (b) Now the total (final) cost of the tree can be obtained by taking the sum of costs of all these levels. ). This is also called Column-Sum. Let us take one example to understand the concept to solve a recurrence using recursion tree method: Example1: Solve the recurrence using recursion tree method. Solution: Step1: First you make a recursion tree of a given recurrence. 1. To make a recursion tree, you have to write the value of at root node. And 37

Introduction to Algorithm 2. The number of child of a Root Node is equal to the value of a. (Here the value of So recursion tree be looks like as: Figure-a b) Now we have to find the value of in figure (a) by putting (n/2) in place of n in equation (1).That is (2) From equation (2), now will be the value of node having branch (child nodes) each of size T(n/2). Now each figure-a will be replaced as follows: in Figure-b 38

Basics of an Algorithm c) In this way, you can extend a tree up to Boundary condition (when problem size becomes 1). So the final tree will be looks like:. n n n n.... n 1 1 1. 1 1. n Figure-c: A Recursion tree for the recurrence Now we find the per level cost of a tree, Per-level cost is the sum of the costs within each level (called row sum). Here per level cost is For example: per level cost at depth 2 in figure-c can be obtained as:. Then total cost is the sum of the costs of all levels (called column sum), which gives the solution of a given Recurrence. The height of the tree is Total cost= ------- (3) To find the sum of this series you have to find the total number of terms in this series. To find a total number of terms, you have to find a height of a tree. Height of tree can be obtained as follow (see recursion tree of figure c): you start a problem of size n, then problem size reduces to, then and so on till boundary condition (problem size 1) is not reached. That is At last level problem size will be equal to 1 if. This represent the height of the tree, hence height. 39

Introduction to Algorithm Hence total cost in equation (3) is ). Example2: Solve the recurrence using recursion tree method. Solution: We always omit floor & ceiling function while solving recurrence. Thus given recurrence can be written as: Figure-a to figure-c shows a step-by-step derivation of a recursion tree for the given recurrence (1). Figure-a Figure-b 40

Basics of an Algorithm c) In this way, you can extend a tree up to Boundary condition (when problem size becomes 1). So the final tree will be looks like:. n n.. n... T(1) T(1) T(1) T(1) Figure-c: A Recursion tree for the recurrence Here the longest path from root to the leaf is:. --------------- (*) Here the smallest path from root to the leaf is:. --------------- (**) 41

Introduction to Algorithm From equation (*) and (**), thus we can write: Remark: If Example3: A recurrence relation for Tower of Hanoi (TOH) problem is. Solve this recurrence to find the solution of TOH problem. Solution: Figure-a to figure-c shows a step-by-step derivation of a recursion tree for the given recurrence Figure-a Figure-b c) In this way, you can extend a tree up to Boundary condition (when problem size becomes 1). That is 42

So the final tree will be looks like: Basics of an Algorithm 1.. 1 (..... 1 1 1 1. 1 1 1 1 Figure-c: A Recursion tree for the recurrence At last level problem size will be equal to 1 if. Hence Total Cost of the tree in figure (c) can be obtained by taking column sum upto the height of the tree. Hence the solution of TOH problem is Check Your Progress 2 Q1: write a recurrence relation for the following recursive functions:. a) { if //if n is even } 43

Introduction to Algorithm b) { if } Q.2: Solve the following recurrence using Iteration Method: a) b) Recurrence obtained in Q.1 a) part c) Recurrence obtained in Q.1 b) part Q.3: Solve the following recurrence Using Recursion tree method a. b. c. d. 1.9.4 MASTER METHOD Definition 1: A function exists a real number such that is asymptotically positive if and only if there The master method provides us a straightforward and cookbook method for solving recurrences of the form are constants and is an asymptotically positive function. This recurrence gives us the running time of an algorithm that divides a problem of size into subproblems of size. The subproblems are solved recursively, each in time The cost of dividing the problem and combining the results of the subproblems is described by the function This recurrence is technically correct only when is an integer, so the assumption will be made that is either or since such a replacement does not affect the asymptotic behavior of the recurrence. The value of is a positive integer since one can have only a whole number of subproblems. 44

Theorem1: Master Theorem Basics of an Algorithm The Master Method requires memorization of the following 3 cases; then the solution of many recurrences can be determined quite easily, often without using pencil & paper. Let be defined on the non negative integers by: treated as above ----- -(1) Then can be bounded asymptotically as follows: Case1: If for some, then Case2: If, then. Case3: If for some, and if for some constant and all sufficiently large then Remark: To apply Master method you always compares and If these functions are in the same Θ class, then you multiply by a logarithmic factor to get the run time of recurrence (1) [Case 2]. If is polynomially smaller than (by a factor of then is in the same Θ class as. If is polynomially larger than (by a factor of then is in the same Θ class as. In case 1 and 3, the functions must be polynomially larger or smaller then If is not polynomially larger or smaller then, then master method fail to solve a recurrence Thus master method fails in following two cases: a) If is asymptotically larger than, but not pollynomially larger (by a factor of than. For example in which. Here is asymptotically larger than, but it is not polynomially larger than In other words b) If is asymptotically smaller than, but not pollynomially smaller (by a factor of than : For example:. Here and 45

Introduction to Algorithm Examples of Master Theorem Example1: Consider the recurrence of, in which a = 9, b=3, and, where. By Master Theorem (case1), we get Example2: Consider the recurrence of Since in which. By Master Theorem (case 2), we get Example3: Consider the recurrence of in which Since be applied. Now check (Regularity Condition), Since where 0.21. Case3 of Master method may satisfied for since so now we can apply mater method (case 3).. Example4: Can the Master method be applied to solve recurrence Why or why not? Solution: Here a = 2 b = 2 Now since is asymptotically larger than, so it might seem that case 3 should apply. The problem is that it is not polynomially larger. The ratio = = logn ; is asymptotically less than for any positive constant. So, the recurrence falls into the gap between case 2 and case 3. Thus, the master theorem can not apply for this recurrence. Example 5: The recurrence T(n)=7T describes the running time of an algorithm A. A competing algorithm A has a running time of What is the largest integer value for a such that A is asymptotically faster than A? Solution: 46

The master method gives us a = 7, b = 2 f(n) = Basics of an Algorithm It is the first case because f(n) = = O( where which gives The other recurrence is a bit more difficult to analyze because when a is unknown it is not so easy to say which of the three cases applied in the master method. But f(n) is same in both algorithms which leads us to try the case1. Applying case1 for the recurrence gives For getting the value of a, so that is asymptotically faster than A: In other words, A asymptotically faster than A as long as (hence The other cases in the master method do not apply for a > 48. Hence A is asymptotically faster than A up to a=48. Check Your Progress 3 (Objective Questions) Q.1: Which of the following recurrence can t be solved by Master method? 1) T( n) 3T ( n / 2) nlog n 2 2) T( n) 4T ( n / 2) n log n a) Only 2 b) only 1 c ) both 1) and 2) d) both 1) and 2) can be solved Q.2: suppose is false? Which of the following a) b) c) d) Q.3 The time complexity of the following function is (assume 47