This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

Editors' Picks Books

Hand-picked favorites from

our editors

our editors

Editors' Picks Audiobooks

Hand-picked favorites from

our editors

our editors

Editors' Picks Comics

Hand-picked favorites from

our editors

our editors

Editors' Picks Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

4 Design and development of an algorithm 1.5 Some simple examples 1.6 Summary 1.7 Keywords 1.8 Answers to check your progress 1.9 Unit- end exercises and answers 1. 10 Suggested readings 1.0 OBJECTIVES At the end of this unit you will be able to • • • • Fundamentals of algorithms along with notation. Te various properties of an algorithm. How to write algorithm or pseudo code for any problem. Algorithms for varieties of problems.

1.1 Introduction to algorithm: An algorithm, named for the ninth century Persian mathematician al-Khowarizmi, is simply a set of rules used to perform some calculations, either by hand or more usually on a machine Even ancient Greek has used an algorithm which is popularly known as Euclid’s algorithm for calculating the greatest common divisor(gcd) of two numbers. An algorithm is a tool for solving a given problem.Before writing a program for solving the given problem, a good programmer first designs and writes the concerned algorithm, analyses it refines it as many times as required and arrives at the final ‘efficient ‘ form that works well for all valid input data and solves the problem in the shortest possible time, utilizing minimum memory space. Definition of algorithm: The algorithm is defined as a collection of unambiguous instructions occurring in some specific sequence and such an algorithm should produce output for given set of input in finite amount of time.

1

The basic requirement is that the statement of the problem is to be made very clear because certain concepts may be clear to some one and it may not be to some body else. For example, calculating the roots of a quadratic equation may be clear to people who know about mathematics, but it may be unclear to some one who is not. A good algorithm is like sharp knife – it does exactly what it is supposed to do with a minimum amount of applied effort. Using the wrong algorithm to solve a problem is like trying to cut a steak with a screwdriver. You obtain a result, but you would have spent more effort than necessary. Any algorithm should consist of the following: 1. Input : The range of inputs for which an algorithm works perfectly. 2. Output : The algorithm should always produce correct results and it should halt. 3. A finite sequence of the instructions that transform the give input to the desired output (Algorithm + Programming lanuage) Usually, algorithm will be written in simple English like statements along with simple mathematical expressions. The definition of Algorithm can be illustrated using the figure 1.1 Input ↓ Problem -- Algorithm -- Computer -- Output ( Fig 1.1 Notion of the Algorithm )

Any systematic method for calculating the result a can be considered as an algorithm. For example, the methods that we learn in school for adding, multiplying, dividing numbers can be considered as algorithms. By looking at the steps specified we can achieve for the result without even thinking. Even a cooking recipe can be considered as an algorithm if the steps: 1. Describe precisely how to make a certain dish. 2. Describe the exact quality to be used. 3. Detail instructions of what items to be added next at what time? How long to cook? 1.2 Properties of algorithms:

Each and every algorithm has to satisfy some properties. The various properties or the characteristics of an algorithms are: 1. Precise and unambiguous (Definiteness) :An algorithm must be simple, precise and unambiguous i.e. there should not be any ambiguity(dout) in the instructions or statements specified to solve a problem. Each and every instruction used in the algorithm must be clear and unambiguous.

2

2. Range of inputs : The range of inputs for which the algorithm produce the desired result should be specified. 3. Maintain order : The instructions in each and every step in an algorithms are I specified order i.e. they will be executed in sequence (i.e. one after the other). The instructions cannot be written in random order. 4. Finite and correct : They must solve the problem in certain finite number of steps and produce the appropriate result. The range of input for which the algorithm works perfectly should be specified. 5. Termination : ecac algorithm should terminate. 6. Several algorithms may exist for solving a given problem and execution speed of each algorithm may be different. (for example, to sort various algorithms bubble sort, insertion sort can be used). 7. An algorithm can be represented in several different ways. 8. Algorithm for a given problem can be based on very different ides (for example, to sort several methods exist such as bubble sort, insertion sort, radix sort etc.) and may have different execution speeds. 1.3 Algorithmic Notations The following notations are used usually while writing any algorithm. 1. Write the word algorithm and write what is the main objective of the algorithm. For example, Algorithm Area_of_circle 2. Then a brief description of what is achieved using the algorithm along with the inputs to the algorithm has to be provided. For example, Description : “The algorithm computes the area of circle using the input value radius” 3. Each i9nstruction should be in a separate steps and the step number has to be provided. What is accomplished in each step has to be described in brief and has to be enclosed within the square brackets (which we call as comment). For example, to find the area of circle, we can write: Step2: [ Find the area of circle] Area 3.142*radius*radius. 4. After all operations are over, the algorithm has to be terminated which indicates the logical end of the algorithm. For example , the last step in the algorithm will be: Step4: [Finished] exit. 1.4 Design and development of an algorithm The fundamental steps in solving any given problem which leads to the complete development of an algorithm, are as follows:

3

5. We should worry about the space3 and timing requirement too. Our job cannot stop once we write the algorithm and code it in say C or C++ or Java. In simple terms time complexity of a program is the amount of computer times it needs to run a program. 6. 7. analysis . We can list all the software specification requirements and try to ask several questions and get the answers. Before we attempt to solve a given problem. Development of a mathematical model Having understood the problem. 4. the next step is to look for a mathematical model. In fact the choose of the model has a long way to go down in the development process. 4 . in this section. Statement of the problem. This is a very important step in the overall solution process and it should be given considerable thought. Statement of the problem. 2. 5. 4. This would help us to understand the problem more clearly and remove any ambiguity. We must think of -which mathematical model is best suit for any given problem ? . which is best suit for the given problem. Development of a mathematical model Designing of the algorithm Implementation Analysis of the algorithm for its time and space complexity Program testing and debugging Documentation. There are again several ways to do this. a number of term s like complexity. efficiency. we can move on to writing down an algorithm. we must understand precisely the statement of the algorithm. Analysis of the algorithm for its time and space complexity We will use.Are there any model which has already been selected to solve a problem which resembles the current one? 3. Why? There are several reasons for this and so we shall start with the time complexity. 1. 3. 2. Implementation In this step appropriated data structures are to be selected and coded in a target language. The select of a target language is very important sub step to reduce complexities involved in coding. Designing of the algorithm As we are comfortable with the specification and the model of the problem at this stage. All of these terms refer to the performance of a program. etc.1.

whether the output of the program is correct or not. The documentation should exist for understanding the problem till it is tested and debugged. Program testing and debugging After implementing the algorithm in a specific language. 5 . So. Many debugging tool exist by which one can test the program for its correctness. Usually debugging is part of testing. Algorithm to find GCD of two numbers. Using different input values.e. Algorithm to find GCD of two numbers. To understand the design or code. 1. usage of proper variable name and data structures play a very important role during documentation. 7.( Euclid’s algorithm). 2.The space complexity of a program is the amount of memory needed to run a program.(Consecutive integer checking method). Any logical error can be identified by program testing. As far as possible program should be selfdocumented. proper comments should be given. After executing the program the desired output should be obtained. ALGORITHM : gcd(m. //Input : Two non-negative and non-zero values m and n //Output : GCD of m and n Step1 : if n=0 return m and stop Step2: Divide m by n and assign the remainder to r. During design and implementation phase the documentation is very useful. Step3: Assign the value of n to m and the value of r to n Step4: Go to step1. The documentation enables the individuals to understand the programs written by the other people. Testing is nothing about the verification of the program for its correctness i.5 Some simple examples 1. next is the time execute. one can check whether the desired output is obtained or not. It is very difficult to read and understand the others logic and code. Documentation Note that the documentation is not last step. 6.n) //Purpose :To find the GCD of two numbers //Description : This algorithm computes the GCD of two non-negative and non-zero values accepted as parameters.

Note: Same problem can be solved in many ways(example Algorithm 1. if (n=0) return m. Step2: [find the gcd using consecutive integer checking] While(1) if (m mod r = 0 and n mod r = 0) break. return non-negative number a the GCD] if (m=0) return n.(Repetative subtraction method) ALGORITHM : gcd(m.2 and 3).n) //Purpose :To find the GCD of two numbers //Description : This algorithm computes the GCD of two non-negative and non-zero values accepted as parameters. 6 .ALGORITHM : gcd(m.n). //Input : Two non-negative and non-zero values m and n //Output : GCD of m and n Step1 : [If one of the two numbers is zero. Step2 :[Repeat step 2 while m and n are different] While (m!=n) if(m>n) mm-n. Algorithm to find GCD of two numbers. else nn-m. end if end while Step 3: [finished : return GCD as the output] return m. end while Step 3: return r. //Input : Two non-negative and non-zero values m and n //Output : GCD of m and n Step1 : [find the minimum of m and n] rmin(m.n) //Purpose :To find the GCD of two numbers //Description : This algorithm computes the GCD of two non-negative and non-zero values accepted as parameters. 3.

(Pseudo code) ALGORITHM SIEVE_PRIME(n) //Purpose : To generate prime numbers between 2 and n //Description : This algorithm generates prime numbers using sieve method //Input : A positive integer n>=2 //Output : Prime numbers <=n Step 1: [ Generate the list of integers from 2 to n ] for p 2 to n do a[p]p end for Step 2: [Eliminate the multiples of p between 2 to n ] for p 2 to √n do if (a[p] != 0 ) i p*p while ( i <= n ) a[i] 0 i i + p end while end if end for Step 3: [Obtain the prime umbers by copying the non zero elements] m0 for p 2 to n do if (a[p] != 0 ) b[m] a[p]. m m + 1 end if end for Step 4: [Output the prime numbers between 2 to n ] for i0 to m-1 write b[i] end for Step 5 : [Finished ] Exit 5.4. //Input : n : a positive decimal integer. Algorithm to find the number of digits in a binary representation of a given decimal integer Algorithm : Binary(n) //Purpose : To count the number of digits in a binary representation of a given decimal integer. Algorithm to generate prime numbers using sieve Eratosthenes method. 7 .

An input to an algorithm specifies an instance of the problem the algorithm solves. 1. 1.8 ANSWERS TO CHECK YOUR PROGRESS 1. Algorithm can be specified in a natural language or a pseudo code. 4. For example.1 2. What are various properties of an algorithm? 3. three algorithms were given for computing the greatest common divisor of two integers: Euclid’s algorithm.7 KEYWORDS Algorithm : It is a sequence of unambiguous instructions to solve a problem in afinite amount of time. While ( n > 1) Count Count + 1 nn/2 end while return Count Check your progress 1.6 SUMMARY • • • • Algorithm : An algorithm is a sequence of non-ambiguous instructions for solving a problem in a finite amount of time. 1. Explain the steps involved in the design and development of an algorithm. A good algorithm is usually a result of repeated efforts and rework. and repetitive subtraction. What is an algorithm ? Explain the notion of the algorithm? 2. Space complexity : It is the amount of memory needed to run a program.//Output : Number of digits in a binary representation of a given positive decimal integer. Explain the procedure of generating prime numbers using the method “Sieve of eratosthenes’ and write the algorithm for the same. they can also be implemented as computer programs. the consecutive integer checking algorithm. 1.2 8 . Count 1. 1. Time complexity : It is the time required to execute a program. The same problem can often be solved by several algorithms.

2. Find gcd(31415.4 UNIT-EBD EXERCISES AND ANSWERS 1.. 1.9 SUGGESTED READINGS 1. 2.24) ] 1. Write an algorithm to find the gcd of two numbers by using repetitive subtraction method. Aho. Alfred V. Introduction to the Design & Analysis of Algorithms By Anany Levitin 2.14142) by applying Euclid’s algorithm.5(4th algorithm) 4.3. 1. Trace it for the input 255. What does Euclid’s algorithm do for a pair of numbers in which the first number is smaller than the second one? What is the largest number of times this can happen during the algorithm’s execution on such an input? 3. 4.5 (3rd algorithm) 1. Write an algorithm to find the number of digits in a binary representation of a given decimal integer. Find gcd(36. Analysis and design of algorithms By A M Padma Reddy. 3. Answers: SEE 1. "The design and analysis of computer algorithm". 9 .5 (1st algorithm) [ Hint : find gcd(12.5 (5th algorithm) 1. 3.171) by using repetitive subtraction 4. 1.5 (1st algorithm) 1.

Performance measurement Need for time complexity Worst-case .0 OBJECTIVES At the end of this unit you will be able to • • • • • • • Efficiency of an Algorithm.2 Properties of algorithm 1. one attempts only to get asymptotic bounds on the step count. Asymptotic analysis makes use of the O (Big Oh) notation. 10 Suggested readings 1.1 Introduction to algorithm 1. Two other notational constructs used by computer scientists in the analysis of algorithms are Θ (Big Theta) notation and Ω (Big Omega)notation. Asymptotic Notations. Best-case and Average-case efficiencies.MODULE-1.8 Answers to check your progress 1. 10 . Time complexity of an algorithm concerns determining an expression of the number of steps needed as a function of the problem size.6 Summary 1.3 Algorithmic Notations 1. one does not aim at obtaining an exact step count. Since the step count measure is somewhat coarse. Instead. Space complexity.9 Unit. Analysis of Recursive algorithms.1 INTRODUCTION Two important ways to characterize the effectiveness of an algorithm are its space complexity and time complexity.5 Some simple examples 1. Big-Oh (O) Big-Omega (Ω) Big-Theta (θ) Practical complexities.UNIT 2 ANALYSIS OF ALGORITHM EFFICIENCY Structure 1.0 Objectives 1.7 Keywords 1. Time complexity. Analysis of iterative algorithms. • • • 1.end exercises and answers 1.4 Design and development of an algorithm 1.

generally every problem will be associated with n. etc. To store these details. When we design a program we must see that memory requirement is kept to the minimum so that even computers with less memory can execute the program. a stack segment will be used.avg. • 1.2 How to calculate space complexity? Before we proceed to any specific example. Stack space To store return address.1 Analysis of space complexity The following components are important in calculating the space requirements: • Instruction space This is the space required to store the machine code generated by the compiler. Therefore.2. Finding the average of three numbers. then the data space occupied by the algorithm/program may be considered as zero. • Number of elements – in the sorting and searching problem. dynamic variables etc. Generally the object code will be placed in the code segment. intermediate variables.a. scanf(“%d %d%d”. n.1. When a problem is independent of n. Now-a-days the operating systems take care of the efficient usage of memory based upon the virtual memory concept or dynamic linking and loading. 1. Void main() { int a. 3. The primary memory of a computer is an important resource for the proper execution of a program. we must understand the importance of the size of the input.c).b. This is nothing but the data segment space. return values. static variables. Let us start with few simple problems which are iterative type. • Number of cities – coloring the map problem. 1. Without sufficient memory either the program works slowly or may not work totally.b. • Number of objects – knapsack problem. EXAMPLE: 1.2.2 Space complexity The space complexity of a program is the amount of memory that may be required to run a program. the exact memory requirement for a program is to be in advance. 2. that is.c. • Data space The space needed for constants. 11 . it may refer to • Number of cities –in travelling salesman problem.

That is how fast an algorithm runs. Note that the time taken by a program for compilation is not included in the calculation. Normally researchers give more attention to time efficiency rather than space efficiency. We say f(n) = Θ(g(n)) if there exist positive constants n0. one attempts only to get asymptotic bounds on the step count. c and avg are all integer variables. Two other notational constructs used by computer scientists in the analysis of algorithms are Θ (Big Theta) notation and Ω (Big Omega) notation. one does not aim at obtaining an exact step count.3 Time complexity It is the amount of time a program or algorithm takes for execution. } Program to illustrate the space complexity. Asymptotic analysis makes use of the O (Big Oh) notation. 1. b. c1 and c2 such that to the right of n0 the value off(n) always lies 12 . printf(“average is=%d”. Since the step count measure is somewhat coarse. because handling memory problems is easier than time.4 ASYMPTOTIC NOTATIONS Two important ways to characterize the effectiveness of an algorithm are its space complexity and time complexity. Time complexity of an algorithm concerns determining an expression of the number of steps needed as a function of the problem size.avg). Θ-Notation (Same order) This notation bounds a function to within constant factors. total space is = 8 + 2 = 10 bytes.avg=(a+b+c)/3. The following notations are commonly use notations in performance analysis and used to characterize the complexity of an algorithm. As a. The performance evaluation of an algorithm is obtained by totaling the number of occurrences of each operation when running the algorithm. 1. The performance of an algorithm is evaluated as a function of the input size n and is to be considered modulo a multiplicative constant. Instead. Hence. the space occupied by them is =4*sizeof(int) =4*2bytes =8bytes Space occupied by the constant 3 is = 1*2 bytes.

c2 = 1/2. and n0 such that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0} We say that is g(n) an asymptotically tight bound for f(n). we write as follows: Θ(g(n)) = {f(n) : there exist positive constants c1. for all n ≥ n0. In other words. Example: n2/2 − 2n = (n2). the value of f(n) lies at or above c1 g(n) and at or below c2 g(n). Graphically. Ο-Notation (Upper Bound) 13 . we could write f(n) ∈ Θ(g(n)) to indicate that f(n) is a member of Θ(g(n)). In the set terminology. because O(g(n)) is a set. this notation is "f(n) = Θ(g(n))" although the idea that f(n) is equal to something called Θ(g(n)) is misleading. We say that g(n) is an asymptotically tight bound for f(n). In the set notation. f(n) is said to be a member of the set Θ(g(n)) of functions. the function f(n) is equal to g(n) to within a constant factor. and n0 = 8.between c1 g(n) and c2 g(n) inclusive. Instead. Historically. In other words. c1. we write f(n) = Θ(g(n)) to express the same notation. for all values of n to the right of n0. with c1 = 1/4.

since Θ-notation is a stronger notation than Ο-notation. the set of functions Ο(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ c g(n) for all n ≥ n0} We say that the function g(n) is an asymptotic upper bound for the function f(n). we may also define f is of order g as follows: If f(n) and g(n) are functions defined on the positive integers. for all values of n to the right of n0.This notation gives an upper bound for a function to within a constant factor.e. Example: 2n2 = Ο(n3). We write f(n) = O(g(n)) if there are positive constants n0 and c such that to the right of n0. then f(n) is Ο(g(n)) if and only if there is a c > 0 and an n0 > 0 such that | f(n) | ≤ | g(n) | for all n ≥ n0 14 . the value of the function f(n) is on or below g(n). We use Ο-notation to give an upper bound on a function. with c = 1 and n0 = 2. the value of f(n) always lies on or below c g(n). we write as follows: For a given function g(n). Equivalently. In the set notation. We write f(n) = O(g(n)) to indicate that a function f(n) is a member of the set Ο(g(n)) i. f(n) ∈ Ο(g(n)) Note that f(n) = Θ(g(n)) implies f(n) = Ο(g(n)). to within a constant factor. Graphically.

Ω-Notation (Lower Bound) This notation gives a lower bound for a function to within a constant factor. the value of f(n) always lies on or above c g(n). 15 . the set of functions Ω(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 ≤ c g(n) ≤ f(n) for all n ≥ n0} We say that the function g(n) is an asymptotic lower bound for the function f(n). There are two interpretations of upper bound. We write f(n) = Ω(g(n)) if there are positive constants n0 and c such that to the right of n0. The intuition behind Ω-notation is shown above. In the set notation. Worst-case Complexity The running time for any given size input will be lower than the upper bound except possibly for some values of the input where the maximum is reached.4. Example: √n = (lg n).Historical Note: The notation was introduced in 1892 by the German mathematician Paul Bachman.1 Algorithm Analysis The complexity of an algorithm is a function g(n) that gives the upper bound of the number of operation (or running time) performed by an algorithm when the input size isn. 1. with c = 1 and n0 = 16. we write as follows: For a given function g(n).

a computational problem consists of problem instances and solutions to these problem instances. it will be optimal and of complexity Θ(n2). most of the time we content ourselves to a worst case behavior.Average-case Complexity The running time for any given size input will be the average number of operations over all problem instances for a given size. whatever the algorithm used. the complexity of g(n) is approximated by its family o(f(n)) where f(n) is one of the following functions.2 Optimality Once the complexity of an algorithm has been estimated. The theory formalizes this intuition. 1. Other complexity measures are also used.5 Practical Complexities Computational complexity theory is a branch of the theory of computation in theoretical computer science and mathematics that focuses on classifying computational problems according to their inherent difficulty. the number of gates in a circuit (used in circuit complexity) and the number of processors (used in parallel computing). Because. a computational problem is understood to be a task that is in principle amenable to being solved by a computer (which basically means that the problem can be stated by a set of mathematical instructions). A problem is regarded as inherently difficult if its solution requires significant resources. Informally. One of the roles of computational complexity theory is to determine the practical limits on what computers can and cannot do. 1. by introducing mathematical models of computation to study these problems and quantifying the amount of resources needed to solve them. Most of the time. An algorithm for a given problem is optimal if its complexity reaches the lower bound over all the algorithms solving this problem. The instances of this problem are natural numbers.4. In this context. na where a ≥ 2 (polynomial complexity). primality testing is the problem of determining whether a given number is prime or not. n (linear complexity). an (exponential complexity). any algorithm solving “the intersection of n segments” problem will execute at least n2operations in the worst case even if it does nothing but print the output. it is quite difficult to estimate the statistical behavior of the input. 16 . log n (logarithmic complexity). If one finds an O(n2) algorithm that solve this problem. This is abbreviated by saying that the problem has Ω(n2) complexity. such as time and storage. For example. the question arises whether this algorithm is optimal. For example. and the solution to an instance is yes or no based on whether the number is prime or not. such as the amount of communication (used in communication complexity).

Since the time taken on different inputs of the same size can be different. In particular. be solved algorithmically. b. it isn't just yes or no. it tries to classify problems that can or cannot be solved with appropriately restricted resources. A key distinction between analysis of algorithms and computational complexity theory is that the former is devoted to analyzing the amount of resources needed by a particular algorithm to solve a problem. the time taken can be expressed as a function of n. imposing restrictions on the available resources is what distinguishes computational complexity from computability theory: the latter theory asks what kind of problems can.Closely related fields in theoretical computer science are analysis of algorithms and computability theory. this is not really the case. Thus the time required to solve a problem (or the space required. or any measure of complexity) is calculated as function of the size of the instance. This is usually taken to be the size of the input in bits. then the algorithm is said to be a polynomial time algorithm.1 Function problems A function problem is a computational problem where a single output (of a total function) is expected for every input.2 Measuring the size of an instance To measure the difficulty of solving a computational problem. the multiplication of two integers can be expressed as the set of triples (a. In turn. More precisely. larger instances will require more time to solve. whereas the latter asks a more general question about all possible algorithms that could be used to solve the same problem. 1. since function problems can be recast as decision problems. the running time may. Deciding whether a given triple is member of this set corresponds to solving the problem of multiplying two numbers.5. how much more time does it take to solve a problem for a graph with 2n vertices compared to the time taken for a graph with n vertices? If the input size is n. the worst-case time complexity T(n) is defined to be the maximum time taken over all inputs of size n. However. c) such that the relation a × b = c holds. one may wish to see how much time the best algorithm requires to solve the problem. in principle. If T(n) is a polynomial in n. It is tempting to think that the notion of function problems is much richer than the notion of decision problems. depend on the instance. but the output is more complex than that of a decision problem.5. in general. However. For instance. 1. Complexity theory is interested in how algorithms scale with an increase in the input size. For example. Notable examples include the traveling salesman problemand the integer factorization problem. that is. in the problem of finding whether a graph is connected. Cobham's thesis says that a problem can be solved with a feasible amount 17 .

Find the time complexity of the following algorithms a. j++) Executed <= n*n times sum++. j<i.)Algorithm :simple for (i=0.j]+a[I.j] Return c Time complexity of this algorithm is given by M(n)= ∑ ∑ =n3 ∑ 1 i= j= k = 0 0 0 n− n− n− 1 1 1 4.j]0 For k 0 to n-1 do C[I. O(1) Running Time: O(n4) 2.6 Performance measurement of simple algorithms 1.k]+b[k..Algorithm for element uniqueness Algorithm : uniquelement(a[].n-1]) //Input : two nby n matrices //output: Matrix c=ab For i0 to n-1 do For j 0 to n-1 do C[I. i++) Executed n*n times for (j=0. Algorithm for matrix multiplication Algorithm matmul(a[0…n-1]. i<=n*n. j<i. 1.an array consisting of n elements For i 0 to n-2 do 18 .j] c[I.} Sol for (i=1. i++) for (j=i+1.b[0. j++) if(i<j){ sum++. i<=n*n.of resources if it admits a polynomial time algorithm.n) //input : n – number of elements and a.

Find the time complexity of matrix multiplication algorithm. 4. Check your progress 1. Worst case efficiency : T(n)€O(n2) Best case efficiency: if a[0]=a[1] then basic operation will be executed only once. Therefore t(n)€Ω(1) Note: To solve the non-recursive algorithms time efficiency make use of the formula result=upper bound-lower bound+1 in each summation. Explain the concept space complexity 2. Asymptotic notations – representation of time complexity in any of the notaions (big oh. ∑ ∑1 = n(n −1) / 2 <= n2 i =0 j =i +1 n −2 n −1 19 . 1.5 KEYWORDS Basic operation – an operation which is executed more number of times in the program (logic part). End if Return 1. SUMMARY: Space complexity: The space complexity of a program is the amount of memory that may be required to run a program.For i i+1 to n-1 do If(a[i]=a[j]) Return 0.big omega. What is meant by a time complexity? Why it is required? 3. write a note on asymptotic notations. big theta). Usually present in the inner most loop (part) of the algorithm/program. Time complexity : It is the time required to execute a program.

1. 1. "Graph Algorithms".4 4. Even. 1. Analysis and design of algorithms with C/C++ .Computer Science Press. 1. Nandagopalan 3.3rd edition by Prof. Inroduction to The design and analysis of algorithms by Anany Levitin 2.. .6 ANSWERS TO CHECK YOUR PROGRESS 1. Find the time complexity of for the algorithm transpose of a matrix 2. 1. Analysis and design of algorithms by Padma reddy 4.4 2. average case . 1.2 2.3 3.1.7 UNIT-END EXERCISES AND ANSWERS 1.8 SUGGESTED READINGS 1. Shimon. Write a note on best case. 20 .3 1. worst case in a program with example Answers: SEE 1.6 1.

There is no magic formula it is simply a matter of judgment.4 1. the time required by individual instructions. theses are some basic techniques that are often useful.3 1. intuition and experience. Find the shortest paths using Prim’s and Kruskal’s algorithm. Determine fist.6 1.2 1.2 Analyzing control structures Control Structures Analysis: Eventually analysis of algorithms proceeds from the inside out. such as knowing how to deal with control structures and recursive equations. Nevertheless.1 Solve container loading and knapsack problems.0 1.0 Objectives 3 ALGORITHMS ANALYSIS AND SOLVING Analyzing control structures Using a barometer Average case analysis Amortized analysis Solving recurrences Summary Key words Answers to check your progress Unit-end exercises and answers Suggested readings OBJECTIVES At the end of this unit you will be able to • • • 2.5 1.1 1.7 1.9 1.8 1.10 2. INTRODUCTION An essential tool to design an efficient and suitable algorithm is the "Analysis of Algorithms".MODULE-1.UNIT RECURRENCES Structure 1. Identifying the difference between graph tree and minimum spanning tree. 1. then combine 21 .

this time is in q(max(t1. Sequencing: Let P1 and P2 be two fragments of an algorithm. Let t be the time required to compute P(i) and the total time required is l=mt. Despite its simplicity. The analysis of for loop is more interesting when the time t(i) required for P(i) runs as a function of I and or also on size n. After all our for loops is shorthand for something like the following while loop. The sequencing rule says that the time required to compute “P1 and P2” is simply t1 + t2. P(i) could depend on the size. They may be single instructions or complicated sub-algorithms.these tines according to the control systems that combine the instructions in the program.2. Let t1 and t2 be the times taken by P1 and P2. Example : Algorithm for matrix multiplication 22 . “For” Loops: They are the easiest loops to analyze.1. (not an error). Usually this approach is adequate. t1 and t2 may depend on various parameters. By the maximum rule. our previous estimate that l is roughly equal to mt is justified. Let “c” be the upper bound on the time required by each of the operations: l <= c for i ¬1 + (m+1)c tests i £ m + mt execution of P(i) + mc execution of i i + 1 + mc sequencing operations l <= (t+3c)m+2c This time is clearly bounded below by mt.2. They way be single instruction. It could happen that one of the parameters that control t2 depends on the results of the computation performed P1. i1 while i <= m do P(i) i i + 1 In the worst situations it is reasonable to count the test i £ m at unit cost and the same thing with the instructions i ¬i + 1 and the sequencing operations “go to” implicit in the while loop. For i 1 to m do P(i) By a convention we’ll adopt: m=0 means P(i) is not executed at all. applying this rule is sometimes less obvious than it may appear.2. Of course. 1. the easiest case is when it doesn’t. ∑ t(i) (ignoring the time taken by the loop control). So: for i 1 to m do P(i) takes a time given by. 1. t2)). such as the instance size. If c is negligible compared to t. but there is a potential pitfall: We didn’t consider the time for the “loop control”.

Algorithm matmul(a[0…n-1],b[0..n-1]) //Input : two nby n matrices //output: Matrix c=ab For i0 to n-1 do For j 0 to n-1 do C[I,j]0 For k 0 to n-1 do C[I,j] c[I,j]+a[I,k]+b[k,j] Return c Time complexity of this algorithm is given by M(n)= ∑ ∑ €θ(n3) ∑ 1

i= j= k = 0 0 0 n− n− n− 1 1 1

1.2.3 Recursive Calls: The analysis of recursive algorithms is straight forward to a certain point. Simple inspection of the algorithm often gives rise to a recurrence equation that “mimics” the flow of control in the algorithm. General techniques on how to solve or how to transform the equation into simpler non-recursive equations will be seen later. 1.2.4 “While” and “Repeat” loops: Theses two types of loops are usually harder to analyze than “for” loops because there is no obvious a priori way to know how many times we shall have to go around the loop. The standard technique for analyzing these loops is to find a function of the variables involved where value decreases each time around. To determine how many times the loop is repeated, however, we need to understand better how the value of this function decreases. An alternative approach to the analysis of “while” loops consist of treating them like recursive algorithms. We illustrate both techniques with the same example, The analysis of “repeat” loops is carried out similarly.

1.3 Using a barometer 1.4 Supplementary examples a. Algorithm to find the sum os array elements Algorithm sum(a,n) 23

{ s=0.0; for I=1 to n do s= s+a[I]; return s; } The problem instances for this algorithm are characterized by n,the number of elements to be summed. The space needed d by ‘n’ is one word, since it is of type integer. • The space needed by ‘a’a is the space needed by variables of type array of floating point numbers. • This is atleast ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be summed. • So,we obtain S sum(n)>=(n+s) [ n for a[],one each for n,I a& s] • Time Complexity: The time T(p) taken by a program P is the sum of the compile time and the run time(execution time) The compile time does not depend on the instance characteristics. Also we may assume that a compiled program will be run several times without recompilation .This rum time is denoted by tp(instance characteristics). The number of steps any problem statemn t is assigned depends on the kind of statement. For example, comments 0 steps. Assignment statements 1 steps. [Which does not involve any calls to other algorithms] Interactive statement such as for, while & repeat-until Control part of the statement. 1. We introduce a variable, count into the program statement to increment count with initial value 0.Statement to increment count by the appropriate amount are introduced into the program. This is done so that each time a statement in the original program is executes count is incremented by the step count of that statement.

Algorithm: Algorithm sum(a,n) { s= 0.0; 24

count = count+1; for I=1 to n do { count =count+1; s=s+a[I]; count=count+1; } count=count+1; count=count+1; return s; } If the count is zero to start with, then it will be 2n+3 on termination. So each invocation of sum execute a total of 2n+3 steps. The second method to determine the step count of an algorithm is to build a table in which we list the total number of steps contributes by each statement. First determine the number of steps per execution (s/e) of the statement and the total number of times (ie., frequency) each statement is executed. By combining these two quantities, the total contribution of all statements, the step count for the entire algorithm is obtained.

Statement 1. Algorithm Sum(a,n) 2.{ 3. S=0.0; 4. for I=1 to n do 5. s=s+a[I]; 6. return s; 7. } Total

S/e 0 0 1 1 1 1 0

Frequency 1 n+1 n 1 -

Total 0 0 1 n+1 n 1 0 2n+3

1.5 AVERAGE –CASE ANALYSIS • • • Most of the time, average-case analysis are performed under the more or less realistic assumption that all instances of any given size are equally likely. For sorting problems, it is simple to assume also that all the elements to be sorted are distinct. Suppose we have ‘n’ distinct elements to sort by insertion and all n! permutation of these elements are equally likely.

25

other than size.3. This type analysis is called asymptotic analysis. Best case: This analysis constrains on the input. T[1….4] in 3 because T[1…. Resulting in the fasters possible run time Worst case:This analysis constrains on the input. n ≥ no. 1. The partial rank of T[I] is defined as the position it would occupy if the sub array were sorted. Omega: the function f(n)=Ω(g(n)) iff there exist positive constants c and no such that f(n) ≥ c*g(n) for all n. An alternative approach. n ≥ no. and their usage difference is primarily circumstantial and due to individual preference.7.i].1. At the heart of the method is the idea that while certain operations may be extremely costly in resources. Clearly the partial rank of T[I] does not depend on the order of the element in Sub array T[1…I-1].we could add the times required to sort each of the possible permutations . consider the sub array. It allows for the establishment of a worst-case bound for the performance of an algorithm irrespective of the inputs by looking at all of the operations. Theta: the function f(n)=ө(g(n)) iff there exist positive constants c1. other than size.6 Amortized anlysis In computer science. amortized analysis is a method of analyzing algorithms that considers the entire sequence of operations of the program. Asymptotic notation: Big ‘oh’: the function f(n)=O(g(n)) iff there exist positive constants c and no such that f(n)≤c*g(n) for all n.6]. easier in this case is to analyze directly the time required by the algorithm. For Example. the accounting method. All of these give the same answers. It is particularly useful because it guarantees worst-case performance while accounting for the entire set of operations in an algorithm.2 ≤ I ≤n. Complexity: Complexity refers to the rate at which the storage time grows as a function of the problem size Asymptotic analysis: Expressing the complexity in term of its relationship to know function.5. reasoning probabilistically as we proceed. the partial rank of T[4] in [3.6. they cannot occur at a high-enough frequency to weigh down the entire program because the number of less costly operations will far outnumber the costly ones in the long run.• • • • • • • Analysis To determine the time taken on a average by the algorithm .4] once sorted is [2.and then divide by n! the answer thus obtained.c2 and no such that c1 g(n) ≤ f(n) ≤ c2 g(n) for all n. There are generally three methods for performing amortized analysis: the aggregate method. Resulting in the fasters possible run time Average case: This type of analysis results in average running time over every type of input. "paying back" the program over a number of iterations.2. 26 . and the potential method. n ≥ no.5. For any I.

-It is a technique of defining an object/process by itself. Because of this. Clearly the time to perform these operations is less than the time needed to perform n regular insertions in addition to the number of array doublings that would have taken place in the original sequence of n operations.7 Recursion: Recursion may have the following definitions: -The nested repetition of identical algorithm is recursion. many short-running operations accumulate a "debt" of unfavorable state in small increments.7. and an array doubling which requires O(j) time (where j<n and is the size of the array at the time of the doubling). and in the worst case an insertion may require O(n). so n insertions can be completed in O(n) time. However. while rare long-running operations decrease it drastically. Recursion may have the following definitions: -The nested repetition of identical algorithm is recursion. The amortized time per operation is therefore O(3n) / n = O(n) / n = O(1). in a specific implementation of the dynamic array. array reallocation may be required. then calculates the average cost to be T(n) / n. Usually. As a simple example. we double the size of the array each time it fills up. 1. Therefore the cost of a sequence of n operations is strictly less than the below expression: The amortized time per operation is the worst-case time bound on a series of n operations divided by n. The amortizedtime per operation is therefore O(n) / n = O(1). -Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. -Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. The accounting method determines the individual cost of each operation. 1.Aggregate analysis determines the upper bound T(n) on the total cost of a sequence of n operations. but overcharges operations early to compensate for undercharges later.1 When to use recursion: 27 . a sequence of n insertions can always be done in O(n) time. The potential method is like the accounting method. combining its immediate execution time and its influence on the running time of future operations. There are only as many array doublings in the sequence as there are powers of 2 between 0 and n ( lg(n) ). There are 2 possible operations: a regular insertion which requires a constant c time to perform (assume c = 1). because the rest of the insertions are done in constant time. Another way to see this is to think of a sequence of n operations.

Output : f. The recursion algorithm for finding the factorial of a number is given below. There must be a decision criterion for stopping the process. the number whose factorial is to be found. local variables and return addresses.Recursion can be used for repetitive computations in which each action is stated in terms of previous result. In making the decision about whether to write an algorithm in recursive or non-recursive form. If the tree appears quite bushy. If the structure is simple then use non-recursive form. 28 . The general procedure for any recursive algorithm is as follows. with little duplication of tasks. the factorial of n Method : if(n=0) f=1 else f=factorial(n-1) * n if end algorithm ends. 1. Algorithm : factorial-recursion Input : n. Restore the most recently saved parameters. it is always advisable to consider a tree structure for the problem. 1. Each time a function calls itself it should get nearer to the solution. 2. 2. If the termination criterion is reached perform final computation and goto step 3 otherwise perform final computations and goto step 1 3. then recursion is suitable. local variable and return address and goto the latest return address. Save the parameters. There are two conditions that must be satisfied by any recursive procedure.

Even though mathematical functions can be easily implemented using recursion it is always at the cost of execution time and memory space. recursive mathematical function is implemented using iterative methods. It is also true that stack can be replaced by a recursive program with no stack.…etc. 2. For example. where the third number is the sum of preceding two numbers and so on.8.3.13. 1. 29 . It divides the problem into pieces or selects out one key step.1.3 Demerits of iterative methods : • • Mathematical functions such as factorial and Fibonacci series generation can be easily implemented using recursion than iteration. 4. a return to the proper location will result when the return to a calling statement is made. A recursive procedure can be called from within or outside itself and to ensure its proper functioning it has to save in some order the return addresses so that. f(n-2) is computed twice. The iterative function obviously uses time that is O(n) where as recursive function has an exponential time complexity.2. It is always true that recursion can be replaced by iteration and stacks. 3. It can be noticed from the fig 2. postponing the rest.2 Iteration v/s Recursion: Demerits of recursive algorithms: 1.1. A Fibonacci series is of the form 0.7. The recursive programs needs considerably more storage and will take more time. In iterative techniques looping of statement is very much necessary. f(n-3) is computed thrice.7.1. hence. Many programming languages do not support recursion.5.5 that. Iteration is more of a bottom up approach.5. It begins with what is known and from this constructs the solution step by step. the recursion tree for generating 6 numbers in a Fibonacci series generation is given in fig 2. Recursion is a top down approach to problem solving. f(n-4) is computed 5 times.

4 SOLVING RECURRENCES :-( Happen again (or) repeatedly) • • • • The indispensable last step when analyzing an algorithm is often to solve a recurrence equation. 1) (Fibonacci) Consider the recurrence. And finally prove by mathematical induction(perhaps constructive induction). This is a main topic of this section the technique of the characteristic equation. fn – f n-1 – f n-2 = 0. The characteristic polynomial is. 4. Calculate the first few values of the recurrence Look for regularity. -(-1) ± √((-1)2 + 4) x = -----------------------2 1 ±√ (1 + 4) = ---------------2 1 ± √5 = ---------2 if n=0 or n=1 30 . x2 – x – 1 = 0. most recurrence can be solved by intelligent guesswork. there exists a powerful technique that can be used to solve certain classes of recurrence almost automatically. n fn = f n-1 + f n-2 otherwise We rewrite the recurrence as. The roots are.7. 3. Guess a suitable general form. 1.1. However. Intelligent guess work: This approach generally proceeds in 4 stages. 2. With a little experience and intention. 1.

√5 ---.-------√5 2 1 – √5 --------n n + n 1 = ---- 31 .--------.= 1 2 2 1 – √5 – 1 – √5 --------------------=1 2 (1) (2) -C2 * 2√5 -------------. f0 = C1 + C2 = 0 f1 = C1r1 + C2r2 = 1 C 1 + C2 = 0 C1r1 + C2r2 = 1 From equation (1) C1 = -C2 Substitute C1 in equation(2) -C2r1 + C2r2 = 1 C2[r2 – r1] = 1 Substitute r1 and r2 values C2 C2 1 .= 1 2 – √5C2 = 1 C1 = 1/√5 Thus.--------√5 2 1 + √5 --------n C2 = -1/√5 -1 1 . fn = C1r1n + C2r2n when n=0.√5 1 .√5 r2 = --------2 The general solution is.√5 --------. when n=1. fn = 1 1 + √5 ---.1+√5 r1 = --------2 and 1 .

p(n) = 1. (x – 2)(x – 3) = 0 The roots are. Inhomogeneous recurrence : * The solution of a linear recurrence with constant co-efficient becomes more difficult when the recurrence is not homogeneous. C1 + C2 = t0 (2) when n=1. (2) * 2 2C1 + 2C2 = 2t0 2C1 + 3C2 = (3 + 2t0) -------------------------------C2 = -3 C2 = 3 Sub C2 = 3 in eqn (2) 32 . 2C1 + 3C2 = t1 (3) sub n=1 in eqn (A) t1 – 2t0 = 3 t1 = 3 + 2t0 substitute t1 in eqn(3). degree = 0.√5 2 2 3. r1 = 2. b is a constant p(n) is a polynomial in ‘n’ of degree ‘d’. b=3. tn – 2t n-1 = 3n (A) In this case. where. The characteristic polynomial is. tn = C1r1n + C2r2n tn = C12n + C23n (1) when n=0.(homogeneous) but on the right-hand side we have bnp(n). * Consider the following recurrence a0tn + a1 t n-1 + … + ak t n-k = bn p(n) * The left hand side is the same as before. that is when the linear combination is not equal to zero. r2 = 3 The general solution. Example(1) : Consider the recurrence.

but only for the case when ‘n’ is a power of 2 . n>1 Reconsider the recurrence we solved by intelligent guesswork in the previous section. Solve the following recurrence relation x(n)=x(n-1)+5 for n>1 . O[3. O(3n)] constants = O[3n] Example :2. 3n = Max[O[(t0 – 3) 2n]. * In the following example. Example: (1) Consider the recurrence. and ti for the term of a new recurrence obtained from the first by a change of variable..C 1 + C2 = t 0 C1 + 3 = t0 C1 = t0 – 3 Therefore tn = (t0-3)2n + 3. if n=1 33 .3n]] = Max[O(2n). if ‘n’ is a power of 2. Finally x(n)=x[n-(n-1)]+(n-1)*5 = x(1)+(n-1)*5 =0 +(n-1)*5 x(n)=5(n-1) 4. 1 T(n) = 3T(n/2) + n . …………………. we write T(n) for the term of a general recurrences. x(1)=0 Sol : The above recurrence relation can be written as shown below x(n)={ x(n-1)+5 if n>1 0 if n=1} consider the relation when n>1 x(n)=x(n-1)+5 -------a Replace n by n-1 in eqv a x(n)=x(n-2)+5+5 Replace n by n-2 in eqv a x(n)= x(n-2)+5+5+5 x(n)= x(n-2)+3*5 …………………. Change of variables: * It is sometimes possible to solve more complicated recurrences by making a change of variable.

define by ti = T(2i) * This transformation is useful because n/2 becomes (2i)/2 = 2 i-1 * In other words. 3.1 T(n) = 3T(n/2) + n * We replace ‘n’ by 2i. 34 . 4. r2 = 2. r1 = 3. r1 & r2: tn = 3nC1 + C2 2n tn = C1 3i + C2 2i We use the fact that. T(2i) = ti & thus T(n) = tlogn when n= 2i to obtain. our original recurrence in which T(n) is defined as a function of T(n/2) given way to one in which ti is defined as a function of t i-1. the characteristic equation. Write a recursive algorithm for generating Fibonacci series and construct the recurrence relation and solve. 3 log2n + C2. * This is achieved by introducing new recurrence ti. tn = C1 r1i + C2 r2i sub. ti = T(2i) = 3T(2 i-1) + 2i ti = 3t i-1 + 2i ti – 3t i-1 = 2i (A) In this case. Solve the following recurrence relation x(n)=x(n-1)+5 for n>1 . Write a note on average case anlysis. T(n) = C1. b = 2. Explain how to analyze different control structures of algorithms. T(n) = O(n log3) ‘n’ is a power of 2 Check your progress 1. nlog23 + C2. 2log2n T(n) = C1 . The general equation. 2. (x – 3)(x – 2) = 0 The roots are. x(1)=0. precisely the type of recurrence we have learned to solve. which is sufficient to conclude that.n [i = logn] When ‘n’ is a power of 2. p(n) = 1. degree = 0 So.

Computer Science Press. 1.7 UNIT-END EXERCISES AND ANSWERS 3. Big ‘oh’ .Guess a suitable general form and finally prove by mathematical induction(perhaps constructive induction).5 3. 1. Inroduction to The design and analysis of algorithms by Anany Levitin 2. solve the recurrence relation x(n)=x(n-1)*n if n>0 where x(0)=1 Answers: SEE 1.8 1. "Graph Algorithms". and Theta symbols of asymptotic notation. Nandagopalan 3. 1.Look for regularity. 1. 1.1 2. 1. 1.7 1. Analysis and design of algorithms by Padma reddy 4. Solving recurrences by using following steps : Calculate the first few values of the recurrence. ..SUMMARY: Control Structures Analysis: Eventually analysis of algorithms proceeds from the inside out.9 SUGGESTED READINGS 1. Determine fist.7 4.7 1. Recursion can be used for repetitive computations in which each action is stated in terms of previous result. ANSWERS TO CHECK YOUR PROGRESS 35 . the time required by individual instructions. 4. Analysis and design of algorithms with C/C++ .7 KEYWORDS 1. Even.3rd edition by Prof. then combine these tines according to the control systems that combine the instructions in the program. 1.6 2. Shimon. Write a note on amortized anlysis. Omega .

4 Keywords 1.0 OBJECTIVES At the end of this unit you will be able to • • • • 3. we have to search the entire file from the beginning till the end to check whether the element matching k is present in the file or not.UNIT 4 SEARCHING AND SORTING Structure 1. The linear search and binary search methods are relatively straight forward methods of searching. There are a number of complex searching algorithms to serve the purpose of searching. selection sort Performance measurement of searching and sorting techniques.1 Sequential search: (Linear search) In this method. then. SEARCHING ALGORITHMS Let us assume that we have a sequential file and we wish to retrieve an element matching with key ‘k’.MODULE-1. For both situations we must examine all n elements of the array so the order of magnitude 36 .5 Answers to check your progress 1.6 Unit.1 Searching algorithms • Linear search • Binary search 1. If the item is not found and the list is exhausted the search returns a zero value. 1.2 sorting • Selection sort • Insertion sort • Bubble sort 1. In the worst case the item is not found or the search item is the last (nth) element.1 Know how to search in different ways Identify which searching technique is better Make sorting in different ways example insertion sort.7 Suggested readings 3. If the desired element is found we stop the search and return the index of that element.3 Summary 1. we start to search from the beginning of the list and examine each element till the end of the list.1.end exercises and answers 1.0 Objectives 1.

then the list is chopped off at (mid+1)th location. vector of n elements K. The approximate mid entry is located and its key value is examined.2 Binary search: Binary search method is also relatively simple method. The execution time for this algorithm is proportional to n that is the algorithm executes in linear time. search element Output : low –index of k low=1. vector of n elements K. O(n).high=n While(low<=high-1) { mid=(low+high)/2 37 . The algorithm for binary search is as follows. For this method it is necessary to have the vector in an alphabetical or numerically increasing order. 1. Algorithm : binary search Input : A. search element Output : j –index of k i=1 While(i<=n) { if(A[i]=k) { write("search successful") write(k is at location i) exit(). The middle entry of the left-reduced list is examined in a similar manner.. The algorithm for sequential search is as follows. algorithm ends. i. } else i++ if end while end write (search unsuccessful). then the list is chopped off at the (mid-1)th location.1. A search for a particular item with X resembles the search for a word in the dictionary. Algorithm : sequential search Input : A. The middle entry of the right-reduced list is examined and the procedure is continued until desired key is found or the search interval is exhausted. Now the list gets reduced to half the original list. if the mid value is lesser than X. If the mid value is greater than X. This procedure is repeated until the item is found or the list has no more elements.e.or complexity of the sequential search is n. On the other hand.

including insertion sort. despite its evident "naïve approach "Selection sort has a quite important application because each item is actually moved at most once. Shell sort is a simple modification that improves performance significantly.2. if end. 1. Furthermore. and quicksort. Sorting by insertion is the simplest method.2 Sorting Several algorithms are presented. shell sort. Here's a step-by-step example to illustrate the selection sort algorithm using numbers: Original array: 6354927 1st pass -> 2 3 5 4 9 6 7 (2 and 6 were swapped) 2nd pass -> 2 3 4 5 9 6 7 (4 and 5 were swapped) 3rd pass -> 2 3 4 5 6 9 7 (6 and 9 were swapped) 4th pass -> 2 3 4 5 6 7 9 (7 and 9 were swapped) 5th pass -> 2 3 4 5 6 7 9 (no swap) 6th pass -> 2 3 4 5 6 7 9 (no swap) 38 .1 SELECTION_SORT Selection sort is among the simplest of sorting techniques and it work very well for small files. 1. and doesn’t require any additional storage. Section sort is a method of choice for sorting files with very large objects (records) and small keys. } else write (search unsuccessful). algorithm ends.if(k<a[mid]) high=mid else low=mid if end } while end if(k=A[low]) { write("search successful") write(k is at location low) exit().

but wastes some effort imparting some order to "unsorted" part of the array. The Selection sort spends most of its time trying to find the minimum element in the "unsorted" part of the array. Selection sort is quadratic in both the worst and the average case. Nonetheless. min j ← j.. only 4 swaps took place. min x ← A[i] for j ← i + 1 to n do If A[j] < min x then min j ← j min x ← A[j] A[min j] ← A [i] A[i] ← min x The worst case occurs if the array is already sorted in descending order. It clearly shows the similarity between Selection sort and Bubble sort.Note: There were 7 keys in the list and thus 6 passes were required. 39 . However. and requires no extra memory. Algorithm : Selection sort for i ← 1 to n-1 do min j ← i. min x ← A[j] of this test are executed. Bubble sort "selects" the maximum remaining elements at each stage.e. the time require by selection sort algorithm is not very sensitive to the original order of the array to be sorted: the test "if A[j] < min x" is executed exactly the same number of times in every case. The variation in time is only due to the number of times the "then" part (i.

. We also refer the numbers as keys. 2. this could be quadratic. and insert it into the correct position in the left hand [sorted array]. It takes as parameters an array A[1.For each i from 1 to n . This is called insertion sort. we compare it with each of the cards already in the hand. from right to left. there is one exchange and n .1. The given sequences are typically stored in arrays. . INSERTION_SORT (A) 40 . Insertion sort is an example of an incremental algorithm. An algorithm consider the elements one at a time. 1. Pseudo code We use a procedure INSERTION_SORT. . this quantity is O(n log n). with at most a constant number outside the array at any time. Note that at all times. the cards held in the left hand are sorted. where we build up a complicated structure on n items by first building it on n − 1 items and then making the necessary changes to fix things in adding the last item. We start with an empty left hand [sorted array] and the cards face down on the table [unsorted array]. so there is a total of n -1 exchanges and (n -1) + (n -2) + . and these cards were originally the top cards of the pile on the table. known as satellite data. To find the correct position for the card. Along with each key may be additional information. These observations hold no matter what the input data is. In the worst case.2.i comparisons.2 Insertion Sort If the first few objects are already sorted. but in the average case. inserting each in its suitable place among those already considered (keeping them sorted). + 2 + 1 =n(n -1)/2 comparisons. [Note that "satellite data" does not necessarily come from satellite!] Algorithm: Insertion Sort It works the way you might sort a hand of playing cards: 1. it builds the sorted sequence one number at a time. Then remove one card [key] at a time from the table [unsorted array]. n] and the length n of the array. 3. This is perhaps the simplest example of the incremental insertion technique. an unsorted object can be inserted in the sorted set in proper place. The array A is sorted in place: the numbers are rearranged within the array.

Read the figure row by row. 3). 4. we sum the products of the cost and times column [see CLRS page 26]. We say that a statement that takes ci steps to execute and executed n times contributes cin to the total running time of the algorithm. the running time of the algorithm is the sum of running times for each statement executed. we have T(n) = c1n + c2 (n − 1) + 0 (n − 1) + c4 (n − 1) + c5 ∑2 ≤ j ≤ n ( tj ) + c6 ∑2 ≤ j ≤ n (tj − 1) + c7 ∑2 ≤ j ≤ n (tj − 1) + c8 (n − 1) In the above equation we supposed that tj be the number of times the while-loop (in line 5) is executed for that value of j. FOR j ← 2 TO length[A] DO 2. To compute the running time. Elements to the left of A[j] that are greater than A[j] move one position to the right. i←i−1 8. That is. we must define "step" independent of machine. {Put A[j] into the sorted sequence A[1 . and A[j] moves into the evacuated position. We have 41 . j indexes the "current card" being inserted into the hand. 2. Note that the value of j runs from 2 to (n − 1). Each part shows what happens for a particular iteration with the value of j indicated. DO A[i +1] ← A[i] 7. key ← A[j] 3. So. A[i + 1] ← key Example: Following figure (from CLRS) shows the operation of INSERTION-SORT on the array A= (5. 1. T(n). i ← j − 1 5. . j − 1]} 4.1. 6. WHILE i > 0 and A[i] > key 6. Analysis Since the running time of an algorithm on a particular input is the number of steps executed.

. n.. we can say that since the while-loop exits because i reaches to 0. Equivalently. tj = 1 for j = 2. 3. always find the key A[i] upon the first time the WHILE loop is run. T(n) it is a linear function of n. 3. when i = j −1. Worst-Case The worst-case occurs if the array is sorted in reverse order i.. In other words.e. This happens if given array A is already sorted. there is one additional test after (j − 1) tests.. we have 42 . in decreasing order. T(n) = an + b = O(n) It is a linear function of n. n. For each j = 2.. The punch line here is that the while-loop in line 5 executed only once for each j. n and the best-case running time can be computed using equation (1) as follows: T(n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑2 ≤ j ≤ n (1) + c6 ∑2 ≤ j ≤ n (1 − 1) + c7 ∑2 ≤ j ≤ n (1 − 1) + c8 (n − 1) T(n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 (n − 1) + c8 (n − 1) T(n) = (c1 + c2 + c4 + c5 + c8 ) n + (c2 + c4 + c5 + c8) This running time can be expressed as an + b for constants a and b that depend on the statement costs ci. ..T(n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑2 ≤ j ≤ n ( tj )+ c6 ∑2 ≤ j ≤ n (tj − 1) + c7 ∑2 ≤ j ≤ n (tj − 1) + c8 (n − 1) Equation (1) Best-Case The best case occurs if the array is already sorted... we find that A[i] less than or equal to the key when i has its initial value of (j − 1). we always find that A[i] is greater than the key in the while-loop test.. . 3. tj = j for j = 2. So. . we must compare each element A[j] with each element in the entire sorted subarray A[1 ... Therefore.. In the reverse order.. j − 1] and so tj = j for j = 2. n and the worst-case running time can be computed using equation (1) as follows: T(n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑2 ≤ j ≤ n ( j ) + c6 ∑2 ≤ j ≤ n(j − 1) + c7 ∑2 ≤ j ≤ n(j − 1) + c8 (n − 1) And using the summations in CLRS on page 27. 3. Therefore. .. Therefore.

That is. when line 5 executed j times for each j. T(n) is a quadratic function of n. The reasons for this choice are as follows: • • • The worst-case running time gives a guaranteed upper bound on the running time for any input. Here the punch line is that the worst-case occurs.T(n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑2 ≤ j ≤ n [n(n +1)/2 + 1] + c6 ∑2 ≤ j ≤ n [n(n − 1)/2] + c7 ∑2 ≤ j ≤ n [n(n − 1)/2] + c8 (n − 1) T(n) = (c5/2 + c6/2 + c7/2) n2 + (c1 + c2 + c4 + c5/2 − c6/2 − c7/2 + c8) n − (c2 + c4 + c5 + c8) This running time can be expressed as (an2 + bn + c) for constants a. 43 . Example: Suppose that we randomly choose n numbers as the input to insertion sort. For example. and searches for absent items may be frequent. Why not analyze the average case? Because it's often about as bad as the worst case. Therefore. upper bound gives us a guarantee that the algorithm will never take any longer. b. Worst-case and average-case Analysis We usually concentrate on finding the worst-case running time: the longest running time for any input size n. the worst case often occurs when the item being searched for is not present. This can happens if array A starts out in reverse order T(n) = an2 + bn + c = O(n2) It is a quadratic function of n. and c that again depend on the statement costs ci. the worst case occurs often. The graph shows the n2 complexity of the insertion sort. For some algorithms. when searching.

In each step. and the best-case running time is θ(n). Here. since these elements are already in order (8 > 5). Let us take the array of numbers "5 1 4 2 8". and swaps them.On average. Insertion sort use no extra memory it sort in place. The pass through the list is repeated until no swaps are needed. This means that tj = j/2. Extra Memory This algorithm does not require extra memory. Now. despite the fact that a time in order of n is sufficient to solve large instances in which the items are already sorted. It implies that on average. 1. and sort the array from lowest number to greatest number using bubble sort algorithm.. j − 1] and it is greater than the other half. Stability Since multiple keys with the same value are placed in the sorted array in the same order that they appear in the input array. algorithm compares the first two elements. j − 1] to decide where to drop key. (15428) ( 1 4 5 2 8 ).2 Bubble sort Bubble sort. elements written in bold are being compared. Insertion sort is stable.2. comparing each pair of adjacent items and swapping them if they are in the wrong order.. also known as sinking sort. it is still a quadratic function of n. • • • For Insertion sort we say the worst-case running time is θ(n2). the key in A[j] is less than half the elements in A[1 . Swap since 5 > 4 (14528) ( 1 4 2 5 8 ). Although the average-case running time is approximately half of the worst-case running time. It takes a time in Ω(n2) in the worst-case. Second Pass: (14258) (14258) 44 . The time of Insertion sort is depends on the original order of a input. Because it only uses comparisons to operate on elements. algorithm does not swap them. is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted. Swap since 5 > 2 (14258) ( 1 4 2 5 8 ). First Pass: (51428) ( 1 5 4 2 8 ). The algorithm gets its name from the way smaller elements "bubble" to the top of the list. it is a comparison sort. which indicates that the list is sorted. the while loop has to look halfway through the sorted subarray A[1 .

8.9}. Find the time complexity .8}.3. The algorithm needs one whole pass without anyswap to know it is sorted.3. 3. the array is already sorted. the array is sorted. 2.4. but our algorithm does not know if it is completed.8. Write an algorithm for bubble sort and selection sort.(14258) ( 1 2 4 5 8 )..2. and the algorithm can terminate. 2. Check your progress 1. Write an algorithm for sequential search and trace it for the input { 1. 6.1. 1.3 SUMMARY: 45 . Find the time complexity of all these algorithms.9.1.9}. Write an algorithm for insertion sort Apply it for the following set of numbers { 6. time complexity of bubble sort = θ(n2 ).. Third Pass: (12458) (12458) (12458) (12458) (12458) (12458) (12458) (12458) Finally. Algorithm :bubbleSort( A : list of sortable items ) n = length(A) for j 1 to n-1 do for i0 to n-j-1 do if A[i] >= A[i+1] then swap(A[i]. + 3 + 2 +1 = n(n+!) / 2 j =1 i =0 j =1 j =1 n −1 n − j −1 n −1 n −1 So. Apply it for the following set of numbers { 5. A[i+1]) end if end for end for Analysis : Time complexity t(n)= ∑ ∑1 = ∑n − j −1 − 0 +1 = ∑n − j = (n −1) + (n − 2) + .6. Swap since 4 > 2 (12458) (12458) (12458) (12458) Now.

2 1.2. 1. 1. 1. bubble sort..10 1. Searching : it is a process of finding the element from the set of n elements.10 SUGGESTED READINGS ANSWERS TO CHECK YOUR PROGRESS 1. . binary search. Example linear search. 1.2. Inroduction to The design and analysis of algorithms by Anany Levitin 2.9 KEYWORDS Binary search : searching an element by divide and conquer technique. 1 2. 1. Shimon. "Graph Algorithms". Example insertion sort. Answers: SEE 1. 1.1 2.2 1. Analysis and design of algorithms by Padma reddy 4. Apply Binary search algorithm to search ‘G’ from the set A B C F H Z X .2. Nandagopalan 3.1 & 1.7 UNIT-END EXERCISES AND ANSWERS 5. 6. Apply insertion sort algorithm for the set A L G O R I T H M to sort it in ascending order.Sorting : It is the process of arranging the elements in either ascending or descending manner. 46 .3rd edition by Prof. selection sort etc. Analysis and design of algorithms with C/C++ . Even.Computer Science Press.3 3.

7 1.0 1.3 1.0 Objectives Introduction General Structure of Divide and conquer Applications finding minimum and maximum Recurrence Equations Summary Key words Answers to check your progress Unit-end exercises and answers Suggested readings OBJECTIVES At the end of this unit you will be able to • • • • 4. Now. The divide and conquer technique of solving a problem involves three steps at Each level of the recursion: • .4 1. let us see "What is divide and conquer technique? What is the general plan using which these algorithms work?" Definition: Divide and conquer is a top-down technique for designing algorithms that consist of dividing the problem into smaller sub problems hoping that the solutions of the sub problems are easier to find.UNIT 2: : DIVIDE AND CONQUER Structure 2. The solutions of all smaller problems are then combined to get a solution for the original problem.1 1.8 1.2 1.1 Find how to apply divide conquer Find the time complexity of a divide and conquer algorithms Identifying recurrence relations for the algorithm Know how to solve recurrence relations INTRODUCTION Divide and conquer method of designing the algorithm is the best known method of Solving a problem. Divide: The problem is divided into a number of sub problems 47 .MODULE-3.10 4.6 1.9 1.

the constants and function take on the following significance: n is the size of the problem. (Here it is assumed that all subproblems are essentially the same size. 1. the problem can be solved using straightforward method . of element comparisons.) f (n) is the cost of the work done outside the recursive calls. 48 . The problem is to find the maximum and minimum items in a set of ‘n’ elements.• • .1. which includes the cost of dividing the problem and the cost of merging the solutions to the subproblems. n/b is the size of each subproblem. a is the number of subproblems in the recursion. Combine: The solutions of sub problems are combined to get the solution for the larger problem. Conquer: The sub problems are conquered by solving them recursively. we once again concentrate on the no. 4.1 Master’s theorem to solve the recurrence relation The master theorem concerns recurrence relations of the form: In the application to the analysis of a recursive algorithm. d is the power of n in f(n). It is possible to determine an asymptotic tight bound in these three cases: T(n)= { θ(nd ) if a< bd if a=bd if a>bd } θ(nd logn) θ(nd logn) Note: Here. In analyzing the time complexity of this algorithm.2 • • • MAXIMUM AND MINIMUM Let us consider another simple problem that can be solved by the divide-andconquer technique. If the sub Problems are smaller in size.

{ 4.• More importantly. If(a[I]>max) then max:=a[I]. the avg. the time is determined mainly by the total cost of the element comparison. when the elements in a[1:n] are polynomials. The no. max:=min:=a[1]. of elements in the list (a[I].……. The no. if(a[I]<min) then min:=a[I]. An immediate improvement is possible by realizing that the comparison a[I]<min is necessary only when a[I]>max is false. The worst case occurs when the elements are in decreasing order.a[j]) denote an arbitrary instance of the problem. average & worst cases. } 10. a[I] . { 7. or strings of character.and conquer algorithm for this problem would proceed as follows: • • • • • Let P=(n. 9. for I:=2 to n do 6. of comparison is 3n/2-1. Else if (a[I]<min) then min:=a[I].n. } Algorithm: Straight forward Maximum & Minimum • • • Straight MaxMin requires 2(n-1) element comparison in the best. vectors. • 1.. the cost of an element comparison is much higher than the cost of the other operations. A divide.a[j]) and we are interested in 49 . of elements comparison is 2(n-1) The average no. no. Now the best case occurs when the elements are in increasing order.…. Hence. 8. very large numbers. of element comparison is < than 2(n-1) On the average a[I] is > than max half the time. Hence we can replace the contents of the for loop by. Here ‘n’ is the no.min) 2. // set max to the maximum & min to the minimum of a[1:n] 3. Algorithm straight MaxMin(a. and so. of element comparison is (n-1). 5. if(a[I]>max) then max:=a[I].max.

P1=([n/2]. else if (I=j-1) then // Another case of small(p) 9. we might divide ‘P’ into the 2 instances.a[1]. min:=a[I]. { 17. parameters I & j 3.min) 2. //combine the solution 29.mid. } 20. } 21..j. divide P into subproblems.The effect is to 4. 1<=I<=j<=n.a[n]) After having divided ‘P’ into 2 smaller sub problems..max. we can solve them by recursively invoking the same divide-and-conquer algorithm.max1.…. { 23. } 15.max. 28. if(min>min1) then min = min1.. // find where to split the set mid:=[(I+j)/2].a[n/2]) & P2= (n-[n/2]. 24. For example . MaxMin(mid+1. //set max & min to the largest & smallest value 5.min). // if P is not small. 25. //are integers.a[[n/2]+1]. if (a[I]<a[j]) then 11. P has to be divided into smaller instances. 18. ……. } 50 . max:=a[I]. if (max<max1) then max=max1. max:=a[j]. //solve the subproblems 26. Algorithm: Recursively Finding the Maximum & Minimum Using Divide and conquer technique 1. } 32. 31. min:=a[j]. 30. { 12. MaxMin(I. 14. 19. else 22.j. Algorithm MaxMin (I. respectively. • • • If the list has more than 2 elements. 13.finding the maximum and minimum of the list.min1). else 16. { 10. 6. 27. //in a[I:j]. 8. //a[1:n] is a global array. { 7. if(I=j) then max:= min:= a[I].

• Examining fig: we see that the root node contains 1 & 9 as the values of I &j corresponding to the initial call to MaxMin.x. • From the tree.n. then T(n) = 2T(n/2) +2 = 2(2T(n/4)+2)+2 = 4T(n/4)+4+2 * * = 2^k-1T(2)+ = 2^k-1+2^k-2 = 2^k/2+2^k-2 = n/2+n-2 = (n+2n)/2)-2 T(n)=(3n/2)-2 *Note that (3n/3)-3 is the best-average. max & imin.y) Suppose we simulate MaxMin on the following 9 elements A: [1] [2] [3] [4] [5] [6] [7] [8] [9] 22 13 -5 -8 15 60 17 31 47 • A good way of keeping track of recursive calls is to build a tree by adding a node each time a new call is made. 9 respectively & thus split the set into 2 subsets of approximately the same size. then the resulting recurrence relations is T(n)={ T([n/2]+T[n/2]+2 1 IF n=2 0 IF n=1 n>2 When ‘n’ is a power of 2. • This execution produces 2 new calls to MaxMin. j. (including the 1st call) • The include no.s in the upper left corner of each node represent the order in which max & min are assigned values. 51 . MaxMin(1. and worst-case no. 5 & 6. n=2^k for some +ve integer ‘k’. • For this Algorithm.. of comparisons when ‘n’ is a power of 2. each node has 4 items of information: I. where I & j have the values 1. of element Comparison: • If T(n) represents this no. we can immediately see the maximum depth of recursion is 4. No.• • The procedure is initially invoked by the statement.

. . + aktn-k = 0. 52 . x(n-2) [x2 –3x –4] = 0. . x = -1. Or. . (x+1)(x-4) = 0. .1 Homogeneous recurrence equation A homogeneous recurrence equation is written as: a0tn + a1tn-1 + . 1. circuits. Say. . 4. . and t1 = -c1 + 4c2 = 1. c1 = -(1/5). . rk.4. So. Or. x2 – 3x – 4 = 0. Or. for n >= 2. c2 = (1/5).3. x2 + x – 4x – 4 = 0. Solution technique: Step 1: Set up a corresponding Characteristic Equation: a0 xn + a1 x(n-1) + …. there are k solutions for k-th order polynomial equation. {Initial condition: t0=0. x(x+1) –4(x+1) = 0. Or. . + ak = 0 [ for x =/= 0] Step 2: Solve the characteristic equation as a polynomial equation. [Note. roots are. . Therefore. we need two initial conditions for two coefficients. Note. Example 1: Solve the recurrence equation tn – 3tn-1 – 4tn-2 = 0. Or.3 SOLVING RECURRENCE EQUATIONS We solve recurrence equations often in analyzing complexity of algorithms. r2. a0xk + a1xk-1 + . t1=1} Characteristic equation: xn – 3x(n-1) – 4x(n-2) = 0. the real roots are r1. + ak x(n-k) = 0. Step 3: The general solution for the original recurrence equation is: tn = ∑i=1kcirin Step 4: Using initial conditions (if available) solve for the coefficients in above equation in order to find the particular solution. x(n-k) [a0 xk + a1 x(k-1) + … +ak ] = 0. the general solution of the given recurrence equation is: tn = c1*(-1)n + c2*(4n) Use t0 = c1 + c2 = 0.] Sove for c1 and c2. and such other cases. .

3. Example 2: tn – 2tn-1 = 3n. Characteristic equation: x2 – 5 x + 6 = 0. + aktn-k = bnp(n).] Transform with n->n+1: tn+1 – 2tn = 3n+1 Eqn(1). Solution technique: Step 0: Homogenize the given equation to an equivalent homogeneous recurrence equation form. this is a homogeneous recurrence equation which is equivalent to the given inhomogeneous equation. Step 1 through 3 (or 4) are the same as in the case of solving homogeneous recurrence equation.So. So. Multiply original eqn with 3 on both the sides: 3tn – 6tn-1 = 3n+1 Eqn(2). .3. . Subtract Eqn(2) from Eqn(1): tn+1 – 5tn + 6tn-1 = 0. roots are x = 2. . the particular solution is: tn = (1/5)[4n – (-1)n] = Θ (4n) 1. polynomial of 0-th order. and there is no initial condition – so we get the general solution only. this is a special case with p(n) =1. Which is (x-2)(x-3) = 0. where b is a constant and p(n) is a polynomial of order n. [Note. Example 3: tn – 2tn-1 = n 53 . General solution of the given recurrence equation is: tn = c1*(2n) + c2*(3n) = Θ (3n) Homogenizing may need multiple steps.2 Inhomogeneous recurrence equation a0tn + a1tn-1 + .

Example-Matrix Multiplication (Strassen’s algorithm) Consider the problem of computing the product of two matrices. tn+2 – 3tn+1 + 2tn = 1 Subtract once again. a > bi. the solution is T(n) = O(nlog_b a). given two matrices. compute the given by matrix . the elements of which are can be written as submatrices. In this instance we view each of the matrices as a matrix. the original matrix multiplication. A and B.3 Solving recurrence equations using Master’s theorem A special type of recurrence equation that is frequently encountered in algorithms' analyses T(n) = aT(n/b) + cni. Second stage of homogenizing.2tn-1 = 0 Now it is a homogeneous recurrence equation and one can solve it in the usual way. the solution is T(n) = O(ni). I. In this section we show that the use of a divide-and-conquer strategy results in a slightly better asymptotic running time. tn+1 – 3tn + 2tn-1 = 1 Still not a homogeneous eqn. tn+2 – 4tn+1 + 5tn .3. for some constant integer i.. 54 . tn+1 – 2tn = n+1 Subtracting the former (given) eqn from the latter eqn. and constants of coefficients a and c.e. Thus. a < bi. the solution is T(n) = O(ni log b n). 1. To implement a divide-and-conquer algorithm we must break the given problem into several subproblems that are similar to the original one. Three cases: a = bi.So. the elements of which are Section shows that the direct implementation of Equation results in an running time.

and is an matrix. the total running time is better than the original. a=8. Since matrix addition is an operation. But this no Fortunately. We can obtain the solution directly from Equation . Consider the following series of seven matrices: 55 . b=2. direct algorithm! . Since . the total running time for the multiplication operation is given by the recurrence: Note that Equation is an instance of the general recurrence given in Equation . In this case. are taken to mean addition and multiplication (respectively) In order to compute the original eight matrix multiplication we must compute matrix sums matrix products (divide) followed by four (conquer).where each From Equation . we get that the result submatrices can be computed as follows: Here the symbols + and of matrices. and k=2. it turns out that one of the eight matrix multiplications is redundant.

a=7. Given elements of the product matrix C as follows: through .Each equation above has only one multiplication. matrix multiplication strategy is Example 2 : Solve the following recurrence equation t(n)=3t(n/2)+1 Where t(1)=1 Sol: It is of the form: 56 . Equation is an instance of the general recurrence given in Equation we obtain the solution directly from Equation . Ten additions and seven multiplications are required to compute through . the worst-case running time is given by the following As above. In this case. we can compute the Altogether this approach requires seven 18 recurrence: matrix multiplications and additions. Consequently. and Note straightforward .b=2 and k=2. and the total running time is (Use Master’s Theorem) . the running time of the divide-and-conquer which is better (asymptotically) than the approach. Therefore. Therefore.

Many efficient algorithms are based on this technique. although it can be both inapplicable and inferior to simpler algorithmic solutions. 3. 1.2 3. Time efficiency T(n) of many divide nad conquer technique satisfies the equation T(n)=aT(n/b)+f(n).807 multiplications. and then combining their solutions to get a solution to the original instance of the problem. Answers: SEE 1.1 57 .Where a=3 .4 SUMMARY Divide and conquer: is a general algorithm design technique that solves a problem’s instance by dividing it into several smaller instances (ideally of equal size). Strassn’s algorithm needs only seven multiplications to multiply two 2-by-2 matrices but requires more additions than definition based algorithm. Using divide and conquer approach write an algorithm for finding the minimum and maximum in the array. this algorithm can multilply two n-by-n matrices about n2. 1. 1. Explain the process of divide and conquer. solving each of them recursively. The Master’s theorem establishes the order of growth o fthis equation’s solution.1 2. 1. By exploring the divide and conquer technique. State Master’s theprem. 2.b=2 and f(n)=nd = 10 Therefore d=0 Here a> bd Hence the solution is o(nlog32 ) T(n)=o(nlog32 ) = o(3logn2) Check your progress 1.

4} and b={1.1.Homogeneous and inhomogeneous form –these are forms of recurrence euations. Analysis and design of algorithms by Padma reddy 58 . 3. 1. Nandagopalan 3.2 3.11 KEYWORDS 1 θ – Asymptoic notation 2. Multiply two matrices a={1. Discuss the different ways of solving the recurrence equations and time complexities. Inroduction to The design and analysis of algorithms by Anany Levitin 2. 9.3 1. Master’s theorem – to solve the recurrence relations of divide and conquer of specific form.12 1.7 UNIT-END EXERCISES AND ANSWERS 7.11 SUGGESTED READINGS ANSWERS TO CHECK YOUR PROGRESS 1.2.3. 1. 1. Discuss the efficiency of an algorithm for finding the minimum and maximum in the array.2 3. 1. 1. . Answers: SEE 1. 1.4} using Strassen’s algorithm.3rd edition by Prof. Analysis and design of algorithms with C/C++ .3 2. 8.4 1. 1.2 2.3.3 4. 1.2.

2 1.8 1.4 1.3 1.0 1.0 Objectives Introduction Merge sort Quick sort Binary search Summary Key words Answers to check your progress Unit-end exercises and answers Suggested readings OBJECTIVES At the end of this unit you will be able to • • • • • Identify various types of Sorting Find the time complexity of a sorting algorithm Different cases of the algorithm (worst.1 1.7 1. average cases) Identify which sorting is better and why? Identify why binary search and its time complexity 5.UNIT 2: : SORTING Structure 3. best.6 1.MODULE-3.10 5.9 1.1 INTRODUCTION 59 .

let us see "What are the steps involved in merge sort?" The various steps that are involved while sorting using merge sort are shown below: . Conquer: Sort the left part of the array and right part of the array recursively using merge sort. 2. This process of merging of two sorted vectors into a single sorted vector is called simple merge. or reordering. More formally.2 MERGE SORT Conceptually. Divide: Divide the given array consisting of n elements into two parts of nl2 Element search . 4. Now. 3. . Sort each sub list recursively by re-applying the merge sort. then it is already sorted. j and k). Merge the two sub lists back into one sorted list. a sorting algorithm is an algorithm that puts elements of a list in a certain order. The output is a permutation. The following procedure is used to merge these two sorted vectors: . let us see "How to design simple merge algorithm?" Design: Suppose we have two sorted vectors A and B with m and n elements respectively. Otherwise: Divide the unsorted list into two sub lists of about half the size. The only necessary condition for this problem is that both arrays should be sorted. of the input. 2. The equivalent code can be written as shown below: 60 . The two sorting techniques that closely follows the divide and conquer method are • Merge sort • Quick sort (partition exchange sort) 5. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly. . . the output must satisfy two conditions: 1. Combine: Merge the sorted left part and sorted right part to get a single sorted array. If the list is of length 0 or 1.In computer science. Compare ith item of vector A with jth item of vector B and copy the lesser item into kth position of resultant vector C (with 0 as the initial value for the variables i. Now. The output is in non decreasing order (each element is no smaller than the previous element according to the desired total order). The key operation in merge sort is combining the sorted left part and sorted right part into a single sorted array. The most-used orders are numerical order and lexicographical order. a merge sort works as follows 1.

i low.1 Example for merge sort) Algorithm SimpleMerge(A.if ( A[i] < B[j] ) then C[k] A[i] k k +l. k low while ( i <= mid and j <= high) if ( A[i] < A[j] ) then C[k] A[i] //Copy the lowest element from first part of A to C ii+1 //Point to next item in the left part of A kk+1 //Point to next item in C 61 .mid. //Input: A is a sorted from the index position low to mid // A is a sorted from the index position mid+1 to high //Output: A is a sorted from index low to high.low. ii+l // Copy the lowest element from A to C // Point to next item in C and A ( Fig 2. j mid+l.high) //Purpose : Merge two sorted arrays where the first array starts from low to mid and //the second starts from mid+1 to high.

ii+l end while while (j <= high) //Copy the remaining items from right part of A to C C[k] A[j] kk+l. low. If low and high are lower limit and upper limits in an array.else C[k] A[j] jj+l k k + 1. the general procedure to sort the items using merge sort is shown below: if ( low < high) Divide the array into equal parts Sort the left part of the array recursively Sort the right part of the array recursively Merge the left part and right part . we can easily arrange the numbers in ascending order using merge sort. high) //Purpose: Sort the elements of the array between the lower bound and upper bound //lnput:A is an unsorted vector with low and high as lower bound and upper bound //Output: A is a sorted vector if(low < high) mid (low + high)/2 // Divide the array into equal parts 62 . end if The complete algorithm to sort the numbers using merge sort is shown below: AlgorithmMergeSort(A. end if end while // Copy the lowest element from second part of A to C // Point to next item in the second part of A // Point to next item in C while (i <= mid) //Copy the remaining items from left part of A to C C[k] A[i] kk+l . jj+l end while for i = low to high a[i] c[i] // Copy the elements from vector C to vector A end for //End of the algorithm Once the merging is over.

’a’ a constant n>1. If the time for the merging operations is proportional to ‘n’. low. n= 2^k. When ‘n’ is a power of 2. 63 . Disadvantages . The algorithm uses extra space proportional to N. // Sort the right part of the array // Merge the left part and right part SimpleMerge(a. high) End if Analysis : It is clear from the algorithm that the problem instance is divided into two equal parts. then T(n)<=T(2^k+1). It is easy to see that if s^k<n<=2^k+1. It uses more memory on the stack because of recursion. T(n) =2(2T(n/4) +cn/2) +cn = 4T(n/4)+2cn = 4(2T(n/8)+cn/4)+2cn * * = 2^k T(1)+kCn. mid + 1. = an + cn log n. then the computing time for merge sort is described by the recurrence relation. low.’c’ a constant. So the algorithm is not in place. 5. high) // Sort the left part of array.MergeSort (a. we can solve this equation by successive substitution. T(n) = { a 2T(n/2)+cn n=1.mid) MergeSort (a. Therefore. mid.3 • QUICK SORT The divide-and-conquer approach can be used to arrive at an efficient sorting method different from merge sort. T(n)=O(n log n) Advantages • Merge sort algorithm is stable algorithm • It can be applied to files of any size.

12. Sort {1. 26. 3. In Quick sort. 5. other choices for the partitioning element than the first item in the set are better in practice. Thus the elements in a[1:m] & a[m+1:n] can be independently sorted. 2} using quicksort. No merge is needed. The function interchange (a. 7. • • Example. 14.j) exchanges a[I] with a[j]. If m=1 & p-1=n. the file a[1:n] was divided at its midpoint into sub arrays which were independently sorted & later merged. This rearranging is referred to as partitioning.• • • • • • • In merge sort. This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I between 1 & n and all j between (m+1) & n for some m. Function partition of Algorithm accomplishes an in-place partitioning of the elements of a[m:p-1] It is assumed that a[p]>=a[m] and that a[m] is the partitioning element. then a[n+1] must be defined and must be greater than or equal to all elements in a[1:n] The assumption that a[m] is the partition element is merely for convenience. 64 . the division into 2 sub arrays is made so that the sorted sub arrays do not need to be merged later.I. 1<=m<=n. 7.

{ 65 .I=m. { 10. //a[q]=t for some q between m and 6. //initially t=a[m]. 9.a[k]<=t for m<=k<q. Algorithm Partition(a..a[m+1]. //Set a[p]=infinite. //within a[m]. // are rearranged in such a manner that if 4.m. repeat 12. 11. q is returned 8. //p-1.a[p-1] the elements 3.….j=p.p) 2. v=a[m]. //a[k]>=t for q<k<p.. and 7.then after completion 5.Algorithm: Partition the array a[m:p-1] about a[m] 1.

11. 5. } 16. I=I+1. { 6. Algorithm Interchange(a.j-1). retun j. //’j’ is the position of the partitioning element. if(p<q) then // If there are more than one element 7. 15. quicksort(p. The quick sort works very well on large set of data. } Design: The quick sort is based on the principle of "Divide and conquer".j) //Exchange a[I] with a[j] { p=a[I]. 20. j=j-1.…. } 1. 14. j=partition(a. //solve the sub problems. until(a[I]>=v). repeat 17.a[q] which resides 2. // divide p into 2 sub problems 9.q). repeat 14. }until(I>=j). 12.q) 1. 3. a[j]=p. a[j]=v. a[I]=a[j].13. until(a[j]<=v). 10. 23. 13. a[m]=a[j]. //Sort the elements a[p]. let us see "What are the steps involved in quick sort?" The various steps that are involved while sorting using quick sort are shown below: Step1: Divide Divide the array into two sub-arrays: 66 .I. 22.p. 2. //is the global array a[1:n] into ascending 3. //order. quicksort(j+1. } Algorithm Quicksort(p. if (I<j) then interchange(a. a[n+1] is considered to be defined 4.j). 19. 7. { 8. 4. // and must be >= all the elements in a[1:n] 5. 6. //There is no need for combining solution. 18.q+1). 15. 16. Now.I. 21.

A[K-1] Pivot element A[K] Right part A[K+1]A[K+2]….1 while (pivot >= a[i]). Once the above condition fails. . The worst partition puts only one element in one array and all other elements in the other array. the best to be a median of the keys in A[p .. on other hand.j . i low .e.e. A very good partition splits an array up into two equal sized arrays. .A[k-l] recursively Sort the right part of the array A[k+l] A[k+2] A[kn-l] recursively To simplify the design. r] every time procedure 'Partition' is called. The procedure 'Partition' always split the array to be sorted into two equal sized arrays. low and high the other variablesthat are used are: . pivot: a[low]is treated as the pivot element. splits an array up into two arrays of very different sizes.A[N-1] Step 2: Conquer: Sort the left part of the array A[O]A[l]... j high . j : The initial value of index j is one more than high i.. This is achieved by storing 00 in a[n]. Keep incrementing the index i as long as pivot a[i]. The general procedure to partition the array is shown below: .Left part A[0]A[1]…. A bad partition. the Quick sort runs asymptotically as fast as merge sort. i : The initial value of index i is low i. Apart from a. If the partitioning is balanced. keep decrementing the index j as long as pivot S a[j]' This is achieved using the statement: do j +. In other words. . if i is less than j exchange a[i] with a[j] and repeat all the above process as long as i <= j. This is achieved using the statement: do i i + 1 while (pivot >= a[i]). . If the procedure 'Partition' produces two regions of size n/2. if partitioning is unbalanced. assume that a very large value is stored at the end of the array... Performance of Quick sort The running time of quick sort depends on whether partition is balanced or unbalanced. the Quick sort runs asymptotically as slow as insertion sort. On the other hand. Best Case The best thing that could happen in Quick sort would be that each partitioning stage divides the array exactly in half. the recurrence relation is then 67 . which in turn depends on which elements of an array to be sorted are used for partitioning.. Once the above condition fails.

T(n)=T(n/2)+T(n/2)+ (n) = 2T(n/2) + (n) And from case 2 of Master theorem T(n) = (n lg n) Worst-case: Let T(n) be the worst-case time for QUICK SORT on input size n.q)2) + (n) (n) Since the second derivative of expression q2 + (n-q)2 with respect to q is positive. Therefore.2(n-1)] + (n) = cn2 .q2)) + = c max (q2 + (n . Substituting our guess in equation 1.We get T(n) = max1≤q≤n-1 (cq2 ) + c(n . each having size at least 1.q)2)) 1 + (n -1)2 = n2 + 2(n -1).2c(n-1) + (n) Since we can pick the constant so that the 2c(n -1) term dominates the have T(n) ≤ cn2 Thus the worst-case running time of quick sort is T(n)= (n2).1 where q runs from 1 to n-1. (n) term we 68 . Continuing with our bounding of T(n) we get T(n) ≤ c [n2 . expression achieves a maximum over the range 1≤ q ≤ n -1 at one of the endpoints. since the partition produces two regions. We have a recurrence T(n) = max1≤q≤n-1 (T(q) + T(n-q)) + (n) --------. Now we guess that T(n) ≤ cn2 for some constant c. This gives the bound max (q2 + (n .

then the recurrence tree has depth (lgn) and (n) work is performed at (lg n) of these level. Then for n > 1. Assume inductively that T(n) ≤ anlgn + b for some constants a > 0 and b > 0. After the split RANDOMIZED_QUICKSORT calls itself to sort two sub arrays. . If we can pick 'a' and 'b' large enough so that n lg n + b > T(1). Let T(n) denotes the average time required to sort an array of n elements. .4 + (n) At this point we are claiming that n-1 ∑k=1 klgk ≤ 1/2 n2 lgn .3 (n) ----.2 (n) + Solve the above recurrence using substitution method. A call to RANDOMIZED_QUICKSORT with a 1 element array takes a constant time.1/8(n2) Stick this claim in the equation 4 above and we get 69 . The average time to sort an array A[1 .q)) + (n) n-1 = 1/n[2 ∑k=1(T(k)] + = 2/n n-1∑k=1(T(k) + (n) --------. so we have T(1) = (1).1 q)) (n) ------.Average-case Analysis If the split induced by RANDOMIZED_PARTITION puts constant fraction of elements on one side of the partition. q] is T[q] and the average time to sort an array A[q+1 .1/8(n2) + 2b/n (n -1) + (n) ≥ n-1 + b) ------. we have T(n) ∑k=1 2/n (aklgk = 2a/n n-1∑k=1 klgk . n] is T[n-q]. We have T(n) = 1/n (T(1) + T(n-1) + n-1∑q=1 T(q) + T(n-q))) + We know from worst-case analysis T(1)= (1)andT(n-1)=O(n2) T(n) = 1/n ( (1) + O(n2)) + 1/n n-1∑q=1 (r(q) + T(n = 1/n n-1∑q=1(T(q) + T(n . This is an intuitive argument why the average-case running time of RANDOMIZED_QUICKSORT is (n lg n).

This will lead to 3 possible situations: If this matches the target K.T(n) ≤ 2a/n [1/2 n2 lgn ≤ anlgn . by printing out the index of the element in the array. This is known as linear search and would have a time complexity of O(n ) in the worst case. 5. we have to sequentially examine each element of the array. All elements to the right of middle can be ignored. (n lg(n)). and we wish to find out the position of an integer target K in the array. we need not make a sequential search over the complete array. How ever.4 BINARY SEARCH Suppose we are given a number of integers stored in an array A. let us say in ascending order. then the method returns a special value such as –1. then search can terminate successfully. if K<A[middle]. we see that (n) + b and an/4 are polynomials and we certainly can choose 'a' large enough so that an/4 dominates (n) + b. then further search is limited to elements to the right of A[middle]. We can make a faster search using the Binary search method. If all elements are exhausted and the target is not found in the array. if the elements of the array are ordered. If it turns out that K >A[middle]. The basic idea is to start with an examination of the middle element of the array.an/4 + 2b + (n) - 1/8(n2)] + 2/n b(n -1) + (n) In the above equation. and we want to locate a specific target integer K in this array. On the other hand. If we do not have any information on how the integers are organized in the array. Here is one version of the Binary Search function: 70 . then search can be limited to elements to the left of A[middle]. We conclude that QUICKSORT's average running time is Conclusion : Quick sort is an in place sorting algorithm whose worst-case running time is (n2) and expected running time is (n lg n) where constants hidden in (n lg n) are small.

if ( K= =A[Mid] ) return Mid.. int n. time complexity is given by Tavg(n) € ( log2 n) Advantages of binary search • Simple technique • Very efficient searching technique Disadvantages of binary search • The array should be sorted. In such situations. int K) { L=0. R= n-1. } Analysis of Binary search Best case : The best case occurs when the item to be searched is present in the middle of the array. else R = Mid – 1 .. …………. Take log on both sides i*log2 =log2 n i= log2 n So. Therefore. maximum number of elements comparisons are required and the time complexity is given by T(n)={ 1 if n=1 T(n/2) + 1 otherwise Consider. Mid. else if ( K > A[Mid] ) L = Mid + 1. t(n)=t(n/2)+1 This recurrence relation can be solved using repeated substitution as shown below: t(n)=t(n/2)+1 replace n by n/2 2 t(n)=1+1+t(n/2 ) t(n)=1+2+t(n/23 ) replace n by n/2 …………. while (L<=R) { Mid = (L +R)/2. } return –1 . Worst case: This case occurs when the key to be searched is in either at the first position or at the last position in the array. In general.let 2i = n t(n)=i+t(1) Where t(1)=0 t(n)=i We have n=2i . 71 . So the total number of comparisons required will be 1. the time complexity of binary search in the best case is given by Tbest(n)=Ω(1).Algorithm : BinarySearch (int A[ ].. t(n)=i+t(n/2i ) Finally to get the initial condition t(1).

sorting them recursively.5 SUMMARY Merge sort is a divide and conquer algorithm. 11.4 UNIT-END EXERCISES AND ANSWERS 10. What is a sorting? 2. 1. Binary search is o(logn) algorithm for searching in sorted arrays.81.e θ(nlogn).39. 72 . Quick sort is a divide and conquer algorithm that works by partitioning its input’s elements according to their value relative to some pre-selected element. a. 1. and then merging the two sorted halves to get the original array sorted.27. 4. worst case and average case time complexity. Quick sort is noted for its superior efficiency among nlogn algorithms for sorting randomly ordered arrays but also for the quadratic worst-case efficiency. Explain the working of binary search with example? Write an algorithm and its time complexity.)What is largest number of key comparisions made by binary search insearching for a key in the following array? { 3. 5. Apply quick sort to the list A N A L Y S I S in alphabetical order.42. 3.55.74.Check your progress 1. The algorithm’s time efficiency is same in all cases i.3 4. 1. Write an algorithm for quick sort? calculate its best case. 1.31.1 2.98 } b) List all the keys of this array that will require the largest number of key comparisons when searched for by binary search. It works by dividing an array into two halves.2 3.70. Write an algorithm for merge sort? Explain its working.14. It is typical example of an application of the divide and conquer technique because it needs to solve just one problem of half the size on each of its iterations.93. ANSWERS TO CHECK YOUR PROGRESS 1.85.

Inroduction to The design and analysis of algorithms by Anany Levitin 2.3rd edition by Prof.4 2. Apply merge sort algorithm to sort A L G O R I T H M in alphabetical order? Is merge sort a stable algorithm? Answers: SEE 1. 1.12. 1. Analysis and design of algorithms with C/C++ . Nandagopalan 73 .3 3.6 SUGGESTED READINGS 1. 1.2 5.

Many problems cannot be solved correctly by greedy approach.4 1.1 1.1 Find how to apply Greedy technique Identifying whether to solve problem by using greedy technique Know how to find single source shortest path Construct Huffman tree and to generate Huffman’s code. They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future.3 1.7 6. They are easy to invent.5 1.0 1.MODULE-3.1 1.1.0 Objectives Introduction GREEDY TECHNIQUE Concept of greedy mehod Optimization Problems Summary Key words Answers to check your progress Unit-end exercises and answers Suggested readings OBJECTIVES At the end of this unit you will be able to • • • • 6. INTRODUCTION Greedy algorithms are simple and straightforward.UNIT 3: Structure 4. Greedy algorithms are used to solve optimization problems 74 .6 1.2 1. easy to implement and most of the time quite efficient.

25. 5.1 Concept of Greedy meyhod Greedy Algorithm works by making the decision that seems most promising at any moment. MAKE-CHANGE (n) { C ← {100. Informal Algorithm • • • Start with nothing. at every stage without passing the given amount. Coins available are: • • • • • dollars (100 cents) quarters (25 cents) dimes (10 cents) nickels (5 cents) pennies (1 cent) Problem : Make a change of a given amount using the smallest possible number of coins.1. As an example consider the problem of "Making Change". Formal Algorithm Make change for n units using the least possible number of coins. add the largest to the coins already chosen. 10. Sum ← 0 While sum !=n // Set that hold the solution 75 . it never reconsiders this decision.1. whatever situation may arise later. 1} //constants S ← {}.

Moreover. gives the value of a solution. which does not appear explicitly. 3. A function that checks the feasibility of a set. The selection function tells which of the candidates is the most promising. 1. solution set. it never changes its mind in the sense that once a coin has been included in the solution set.89 (289 cents) here n = 2.e. At each step o item will be added in a solution set by using selection function. An objective function. Algorithm maintains two sets. 3 quarters. 76 .1. The greedy algorithm consists of four (4) function. The algorithm is greedy because at every stage it chooses the largest coin without worrying about the consequences. A function that checks whether chosen set of items provide a solution. Structure Greedy Algorithm • • Initially the set of chosen items is empty i. o IF the set would no longer be feasible reject items under consideration (and is never consider again).. 1 dime and 4 pennies. o ELSE IF set is still feasible THEN add the current item. 4.x=largest item in set C such that sum+x <= n if no such item then return “No solution” S ← S{valueofx} sum ← sum+x RETURN S } Example : Make a change for 2.89 and the solution contains 2 dollars.2 Characteristics and Features of Problems solved by Greedy Algorithms To construct the solution in an optimal way. it remains there. 1. One contains chosen items and the other contains rejected items. 2.

the empty set is always promising why? (because an optimal solution always exists) Unlike Dynamic Programming.000 5. The characters in the data occur with following frequencies.3 Definitions of feasibility A feasible set (of candidates) is promising if it can be extended to produce not merely a solution.2.000 77 . a greedy strategy usually progresses in a top-down fashion.1 Huffman Codes Huffman code is a technique for compressing data.000 9. a b c d e f Frequency 45. 6. Greedy-Choice Property The "greedy-choice property" and "optimal substructure" are two ingredients in the problem that lend to a greedy strategy. Huffman's greedy algorithm look at the occurrence of each character and it as a binary string in an optimal way. making one greedy choice after another. but an optimal solution to the problem.000 13.2 OPTIMIZATION PROBLEMS 1.000 characters that we want to compress. In particular.000 12.1. which solves the subproblems bottom-up.000 16.1. Example Suppose we have a data consists of 100. reducing each problem to a smaller one. Greedy-Choice Property It says that a globally optimal solution can be arrived at by making a locally optimal choice.

Consider the problem of designing a "binary character code" in which each character is represented by a unique binary string. Fixed Length Code In fixed length code, needs 3 bits to represent six(6) characters.

Frequency Fixed Length code

a b c d 45,000 13,000 12,000 16,000 000 001 010 011

e 9,000 100

f 5,000 101

This method require 3000,000 bits to code the entire file. How do we get 3000,000?

• •

Total number of characters are 45,000 + 13,000 + 12,000 + 16,000 + 9,000 + 5,000 = 1000,000. Add each character is assigned 3-bit codeword => 3 * 1000,000 = 3000,000 bits.

Conclusion Fixed-length code requires 300,000 bits while variable code requires 224,000 bits. Saving of approximately 25%.

Prefix Codes In which no codeword is a prefix of other codeword. The reason prefix codes are desirable is that they simply encoding (compression) and decoding. Can we do better? A variable-length code can do better by giving frequent characters short codewords and infrequent characters long codewords.

Frequency Fixed Length code

a b c d 45,000 13,000 12,000 16,000 0 101 100 111

e 9,000 1101

f 5,000 1100

78

Character 'a' are 45,000 each character 'a' assigned 1 bit codeword. 1 * 45,000 = 45,000 bits. Characters (b, c, d) are 13,000 + 12,000 + 16,000 = 41,000 each character assigned 3 bit codeword 3 * 41,000 = 123,000 bits Characters (e, f) are 9,000 + 5,000 = 14,000 each character assigned 4 bit codeword. 4 * 14,000 = 56,000 bits. Implies that the total bits are: 45,000 + 123,000 + 56,000 = 224,000 bits Encoding: Concatenate the codewords representing each characters of the file.

String Encoding TEA 10 00 010 SEA 011 00 010 TEN 10 00 110 Example From variable-length codes table, we code the3-character file abc as:

a 0

b

c => 0.101.100 = 0101100

101 100

Decoding Since no codeword is a prefix of other, the codeword that begins an encoded file is unambiguous. 79

To decode (Translate back to the original character), remove it from the encode file and repeatedly parse. For example in "variable-length codeword" table, the string 001011101 parse uniquely as 0.0.101.1101, which is decode to aabe. The representation of "decoding process" is binary tree, whose leaves are characters. We interpret the binary codeword for a character as path from the root to that character, where 0 means "go to the left child" and 1 means "go to the right child". Note that an optimal code for a file is always represented by a full (complete) binary tree.

1.2.2 Dijkstra's Algorithm (Single Source shortest path algorithm ) Dijkstra's algorithm solves the single-source shortest-path problem when all edges have non-negative weights. It is a greedy algorithm.Algorithm starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S. Vertices are added to T in order of distance i.e., first S, then the vertex closest to S, then the next closest, and so on. Following implementation assumes that graph G is represented by adjacency. Problem Statement : To find out the shortest distance from a single source to different cities. Algorithm : DIJKSTRA (G, w, s) 1. INITIALIZE SINGLE-SOURCE (G, s) 2. S ← { } // S will ultimately contains vertices of final shortest-path weights from s 3. Initialize priority queue Q i.e., Q ← V[G] 4. while priority queue Q is not empty do 5. u ← EXTRACT_MIN(Q) // Pull out new vertex 6. S ← S ∪ {u} // Perform relaxation for each vertex v adjacent to u 7. for each vertex v in Adj[u] do 8. Relax (u, v, w)

Example: Step by Step operation of Dijkstra algorithm. Step1. Given initial graph G=(V, E). All nodes nodes have infinite cost except the source node, s, which has 0 cost.

80

node y is the closest node. Now. Choose the closest node. Add it to S. s.Step 2. which is closest to the source node. Update predecessors for nodes u. x. Relax all nodes adjacent to source. We initialize d[s] to 0. Relax all nodes adjacent to node x. Step 3. Update predecessor (see red arrow in diagram below) for all nodes updated. v and y (again notice red arrows in diagram below). s. Relax node v and adjust its predecessor (red arrows remember!). Step 4. so add it to S. 81 . First we choose the node.

Step 6. Choose this node and adjust its neighbor node v. The predecessor list now defines the shortest path from each node to the source node. 82 . Now we have node u that is closest.Step 5. add node v. Finally. s.

and irrevocable. Write a note on Huffman coding. 3.5 ANSWERS TO CHECK YOUR PROGRESS 1. Check your progress 1. 1.1 83 . 2.Analysis Like Prim's algorithm. 1. 1. 2. each expanding a partially constructed solution obtained so far. This is accomplished by a greedy construction of a binary tree whose edges are labeled with 0’s and 1’s. On each step. locally optimal .. Explain the greedy method of problem solving with the example.4 KEYWORDS 1 Greedy Technique – It is a method (approach) of problem solving. Dijkstra’s algorithm solves the single source shortest path problem of finding shortest paths from a given vertex (the source) to all the other vertices of weighted graph or digraph. Huffman code is an optimal prefix free variable length encoding scheme that assigns bit string to characters based on their frequencies in a given text. 1. until acompletesolution to the problem is reached. Huffman’s tree – It is a binary tree generated by Huffman algorithm having left child edge weight as 0 and right child weight as 1.3 SUMMARY The Greedy Technique suggests constructing a solution to an optimization problem through a sequence of steps. Dijkstra's algorithm runs in O(|E|lg|V|) time. the choice made must be feasible. Write an algorithm for a single source shortest path by using greedy technique explain it with the example.

3 1. 1.2 1. Prove that how variable length encoding is better than the fixed length encoding. Answers: SEE 1.a.defend your answer with the example.6 UNIT-END EXERCISES AND ANSWERS 1. Is that the Huffman encoding can be used for data compression . Analysis and design of algorithms by Padma reddy 84 .2 3. Analysis and design of algorithms with C/C++ . 1. Discuss how Dijkstra’s algorithm belongs to greedy technique with the example. 1.2. Compare fixed length encoding with the variable length encoding b. c. Nandagopalan 3.7 SUGGESTED READINGS 1.3rd edition by Prof.3 2. 1. Introduction to The design and analysis of algorithms by Anany Levitin 2. . 2.

1 Solve container loading and knapsack problems.1 1. INTRODUCTION • • Greedy method is the most straightforward designed technique. Identifying the difference between graph tree and minimum spanning tree.4 1.8 1.3 1.2 CONTAINER LOADING PROBLEM 85 .2 1.7 1. DEFINITION: • A problem with N inputs will have some constraints .0 Objectives Introduction APPLICATIONS OF GREEDY METHOD Container loading problem 0/1 Knapsack problem Minimum cost spanning tree algorithms Summary Key words Answers to check your progress Unit-end exercises and answers Suggested readings OBJECTIVES At the end of this unit you will be able to • • • 7.9 1.10 7.0 1. As the name suggest they are short sighted in their approach taking decision on the basis of the information immediately at the hand without worrying about the effect these decision may have in the future. Find the shortest paths using Prim’s and Kruskal’s algorithm. • A feasible solution that either maximize can minimize a given objectives function is called an optimal solution.6 1.any subsets that satisfy these constraints are called a feasible solution.UNIT 3: Structure 5.MODULE-3. 7.

80. Formally we can define the problem as. 1 .1.190.The container laoding problem is almost similar to the knapsack problem and also similar to another interesting problem called packing problem. fortunately.0.140.40. the solution is {1. The objective of this problem is to load the ship with maximum number of containers. 1.w5. {w1.w2.1.1. from i=1 to n.w3. 0.w6.w5.10} and C=300.40.0. Example Consider a container loading instances with n=7. There are n items and ith item weigh wi and is worth vi dollars.90. 1. taking values 0 or 1. The container loading is stated as follows: We have equal size containers to be loaded on to the cargo and in turn the cargo is to be loaded on to the ship. 40.w2.w7}={90. wi.w4. Maximise Subjected to the constraints.e.40.190} ={ 1. Since the objective is to load the maximum number of containers. 0 } Therefore.80. ascending order of weights) weights so that we could pack more containers.w7}={ 10.w6. What items should thief take? 86 .xi<= C Greedy Strategy In this problem. wi and the cargo has a maximum capacity of c units.w3. 1 . we do not have the profit that has to be considered for constraints. we get {w1. Let xi is a variable.1} Total number of containers=5 Total weight=260 7. Each container has weight.w4. 40. the greedy strategy that we use is : Include the containers from lowest to highest (i.140. A 1 indicates that container is to be loaded and a 0 means it should not.3 KNAPSACK PROBLEM Statement : A thief robbing a store and can carry a maximal weight of w into their knapsack. Sol : When the containers are arranged in the ascending order of their weights.

87 . ????? 0-1 knapsack problem The setup is the same.3. Greedy algorithm exists. but may not take a fraction of an item. But in this algorithm.1 0-1 Knapsack Problem using Dynamic programming • • • • • • we are given n objects and knapsack or bag with capacity M object I has a weight Wi where I varies from 1 to N. but the thief can take fractions of items. so thief may decide either to take an item or to leave it (binary choice). Exhibit greedy choice property. No greedy algorithm exists. but the items may not be broken into smaller pieces. Exhibit optimal substructure property. where 0 ≤ xi ≤ 1. There are so many ways to solve this problem. Exhibit No greedy choice property. it will generate only one solution which is going to be feasible as well as optimal. The problem is we have to fill the bag with the help of N objects and the resulting profit has to be maximum. First.There are two versions of problem Fractional knapsack problem The setup is same. we find the profit & weight rates of each and every object and sort it according to the descending order of the ratios. 1. Formally the problem can be stated as Maximize xipi subject to XiWi<=M Where Xi is the fraction of object and it lies between 0 to 1. Exhibit optimal substructure property. which will give many feasible solution for which we have to find the optimal solution. meaning that the items can be broken into smaller pieces so that thief may decide to carry only a fraction of xi of item i. Only dynamic programming algorithm exists.

If so place 1 unit of the first object and decrement . Mathematically the 0-1-knapsack problem can be formulated as: maximize subject to The bounded knapsack problem restricts the number xi of copies of each kind of item to a maximum integer value ci.in this case place a fraction of the object and come out of the loop.w.• • • • Select an object with highest p/w ratio and check whether its height is lesser than the capacity of the bag.j]=0 88 . Mathematically the bounded knapsack problem can be formulated as: maximize subject to Algorithm: Knapsack(n.p.capacity of the knapsack w – weight of all the objects p.the optimal solution for the number of objects selected with specified remaining capacity For i 0 to n do For j0 to m do If(i=0 or j=0) v[i.profits of all the objects //Output: v. The most common formulation of the problem is the 0-1 knapsack problem.m. Whenever you selected. Repeat the above steps until the capacity of the bag becomes less than the weight of the object you have selected .the capacity of the bag by the weight of the object you have placed. which restricts the number xi of copies of each kind of item to zero or one.v) //Input : n – number of objects to be selectd m.

w. Each item has some weight and some value.j]=v[i-1.Else if(w[i]>j) v[i. j=m While( i != 0 and j != 0) { If(v[I.v. pack the knapsack to get the maximum total value. j=j-w[i] } i=i-1 } For i1 to n do If(x[i]=1) Write “ object i selected” End if End for Example : Given some items.e.v[i-1.j].profits of all the objects //Output : x.j-w[i]]+p[i]) end if end for end for Algorithm: objectselected(n.x) //Input : n – number of objects to be selectd m. Total weight that we can carry is no more than some fixed number W. Item #Weight Value 1 2 12 2 1 10 Maximum capacity is i.capacity of the knapsack w – weight of all the objects p.j]=max(v[i-1.j] != v[i-1.m.the information of objects selected and not selected For i0 to n-1 do X[i]=0 End for I=n. M=5 3 3 20 4 2 15 89 .j]) { x[i]=1.j] else v[i. So we must consider weights of items as well as their value.

factories. with two properties: it spans the graph. Little more formally. etc. In other words. An edge of a spanning tree is called a branch. it includes every vertex of the graph.e. It starts from an arbitrary vertex (root) and at each stage.4 MINIMUM COST SPANNING TREE ALGORITHM A spanning tree of a graph is any tree that includes every vertex in the graph. the algorithm halts when all the vertices in the graph have been reached. We construct spanning tree whenever we want to find a simple. but typically attributed to Prim. • Let G=(V.). a spanning tree of a graph G is a subgraph of G that is a tree and contains all the vertices of G. Minimum spanning tree A minimum spanning tree (MST) of a weighted graph G is a spanning tree of G whose edges sum is minimum weight. 90 .. cites. Spanning trees are important because of following reasons. a MST is a tree formed from a subset of the edges in a given undirected graph. 2 and 4. Each edge has a given non negative length. 1. i.. add a new branch (edge) to the tree already constructed.e. an edge in the graph that is not in the spanning tree is called a chord. 1 PRIMS ALGORITHM: This algorithm was first proposed by Jarnik. cheap and yet efficient way to connect a set of terminals (computers. undirected graph where V is a set of vertices (nodes) and E is the set of edges.Sol : optimal profit obtained is 37 by selecting the objects 1. E) be a connected. i. the total weight of all the edges is as low as possible. • it is a minimum.

l) be an edge of minimum cost in E.1]:=j. T[1.This strategy is greedy in the sense that at each step the partial spanning tree is augmented with an edge that is the smallest among all possible adjacent edges.t) { Let (k. add a new branch (edge) to the tree already constructed. the algorithm halts when all the vertices in the graph have been reached. Else near[i]:=k.near[j]] is minimum. t[i.l].n.1]:=k. 91 . At each stage. T[i. Near[k]:=near[l]:=0.2]:=l.k]) then near[i]:=l. Example : Start from an arbitrary vertex (root). For I:=1 to n do If (cost[i. t[1.cost.near[j]]. Algorithm prims(e. Mincost:=mincost+ Cost[j. For i:=2 to n-1 do { Let j be an index such that near[j]≠0 and Cost[j.l]<cost[i.2]:=near[j]. Mincost :=cost[k.

The working of prims will be explained by following diagram Step 2: Step 1: Step 3: Step 4: Step 5: Step 6: 92 .j).j) to be added in such that I is a vertex included in the tree.j] is minimum among all the edges. j is a vertex not yet included.Near[j]:=0. edges are added to the tree one by one. cost[i. For k:=0 to n do If near((near[k]≠0) and (Cost[k. } Return mincost.near[k]]>cost[k. the next edge (i.j])) then Near[k]:=j. Then. } • • • The prims algorithm will start with a tree that includes only a minimum cost edge of G. and cost of (i.

Analysis : The algorithm spends most of its time in finding the smallest edge. 2. time of the algorithm basically depends on how do we search this edge. { for i=1 to n do parent[I]=-1. Straightforward method Just find the smallest edge by searching the adjacency list of the vertices in V.tset of edge in minimum cost spanning tree // the first cost is returned. • Edge are considered for inclusion in ‘T’ in increasing order of their cost. //cost[u.n. While((I<n-1)and (heap not empty)) do { j=find(n). 93 . KRUSKAL’S ALGORITHM: In kruskal's algorithm the selection function chooses edges in increasing order of length without worrying too much about their connection to previously chosen edges.cost. In this case. I=0. each iteration costs O(m) time.t) //Eset of edges in G has ‘n’ vertices. except that never to form a cycle.0. • • An edge is included in ‘T’ if it doesn’t form a cycle with edge already in T.v]cost of edge (u. So.mincost=0.v). yielding a total running time of O(mn). • In this algorithm. To find the minimum cost spanning tree the edge are inserted to tree in increasing order of their cost Algorithm: Algorithm kruskal(E. The result is a forest of trees that grows until all the trees in a forest (all the components) merge in a single tree. a minimum cost-spanning tree ‘T’ is built edge by edge.

i) creates the second tree. mincost=mincost+cost[u. Step 1. Either vertex g or vertex h could be representative. Choose vertex c as representative for second tree. } } if(i notequal n-1) then write(“No spanning tree”) else return minimum cost. the Edge(g. } Analysis • The time complexity of minimum cost spanning tree algorithm in worst case is O(|E|log|E|).k). 94 . In the graph. h) is shortest. The edge (c.1]=u. where E is the edge set of G. Step 2. Example: Step by Step operation of Kurskal algorithm. union(j. Lets choose vertex g arbitrarily.2]=v. if(j not equal k) than { i=i+1 t[i.k=find(v). t[i.v].

but if we add this edge a cycle would be created. Step 5. Vertex c is chosen as the representative. Edge (a. Add edge (c. f) and merge two trees. g) is the next shortest edge. b) creates a third tree. i) is the next next cheapest. Edge (g. 95 . Instead. Step 6. Step 7. d). Add this edge and choose vertex g as representative. Vertex c is the representative of both.Step 3. add edge (c. Step 4. Edge (g.

Step 8. edge(h. 96 . If we add edge (h. In this spanning tree all trees joined and vertex c is a sole representative. h). Step 10. i). e) instead to complete the spanning tree. Step 9. i) would make a cycle. i) add edge (a. it would create a cycle. Again. Add edge (d. Instead of adding edge (h. c). if we add edge (b.

So that one can easily traverse the whole graph with less cost.Check your progress 1. 14.4(2) 1.7 UNIT-END EXERCISES AND ANSWERS 13.14 1. 1.13 KEYWORDS 1 Minimum spanning tree – its is tree consists of all the vertices of the graph with very few number of edges and also the total weight of the spanning tree is minimum. 4. 1. Prims and Kruskal’s are the two algorithms to generate minimum spanning trees.4 (1 & 2) ANSWERS TO CHECK YOUR PROGRESS 97 . 1.? Explain it with the example. 1. Compare container loading problem with the knapsack problem Answers: SEE 1.4(1) 4. List out the difference between Prim’s algorithm and Kruskal’s algorithm. Discuss 0/1 knapsack problem with example 3. Write Kruskals algorithm for minimum spanning tree generation. SUMMARY: Container loading problem is to fill more number of containers into the ship subjected to some specified constraints.3 3. Knapsack problem is to selecting the objects from n objects which yields more profit and in which the sum of selected objects weight should not cross the knapsack weight. Explain the container loading problem with example 2. 1. 1. 1. Write Prims algorithm for minimum spanning tree generation?Explain it with the example.2 2.

Shimon.Computer Science Press.3 1. Nandagopalan 3. Inroduction to The design and analysis of algorithms by Anany Levitin 2. 1. "Graph Algorithms". Analysis and design of algorithms by Padma reddy 4. Even. .3rd edition by Prof. Analysis and design of algorithms with C/C++ .2 & 1..2.12 SUGGESTED READINGS 1. 98 .

we introduce some definitions on graphs. which specify graph and its structure. Identify which method of representation graph is better and when.4 Summary 1.3 Depth First Search (DFS) traversal 1. For instance. Then you can turn to basic graph algorithms.1 Introduction to graph : Graphs are widely-used structure in computer science and different computer applications. how graphs are represented inside of a computer. 99 . Next. We don't say data structurehere and see the difference.6 Answers to check your progress 1. First set is V. Road network.1.7 Unit.2 Graph representation • Adjacency matrix • Adjacency list 1. First of all.0 OBJECTIVES At the end of this unit you will be able to • • • 8. we are going to show.MODULE-4. if one city can be reached from another one or find the shortest route between two cities.1 Represent the graph in a computer by using adjacency matrix or adjacency list type. Traverse the graph using DFS traversal and its time complexity.8 Suggested readings 8. the connections. which is called vertex-set. There are two important sets of objects.0 Objectives 1. Graphs mean to store and analyze metadata. consider cities in your country.end exercises and answers 1.UNIT1 INTRODUCTION TO GRAPHS Structure 1. which present in data. We can examine. GRAPHS AS DATA STRUCTURE 1.5 Keywords 1.1 Graphs as data structures 1. In the example with road network cities are vertices. can be represented as a graph and then analyzed. which connects them.

but significant differences are also present. have a direction. each edge connects two vertices. the last vertex in the path is called the end vertex. 100 . including a case. is called path. undirected graph directed graph Now. which is called edge-set. Path is called simple. All graphs are divided into two big groups: directed and undirected graphs.Each vertex can be drawn as a circle with vertex's number inside. Edge can be drawn as a line. If start and end vertices are the same. only once. vertices Next important set is E. First vertex in the path is called the start vertex. The difference is that edges in directed graphs. if it includes every vertex only once. we present some basic graph definitions. Cycle is called simple. We will accentuate which kind of graphs is considered in the particular algorithm description. Simply speaking. These kinds of graphs have much in common with each other. If a graph is directed. except start (end) one. each line has an arrow. Let's see examples of path and cycle. called arcs. E is a subset of V x V. when a vertex is connected to itself (such an edge is called a loop). if it includes every vertex. such that there is an edge from each vertex to the next in sequence. path is called cycle. • Sequence of vertices.

For instance. a ) Adjacency matrix Each cell aij of an adjacency matrix contains 0. weighted graph 1. called edge weight.path (simple) cycle (simple) The last definition we give here is a weighted graph. if every edge is associated with a real number. We will discuss two of them: adjacency matrix and adjacency list. in the road network example.2 Graphs representation There are several possible ways to represent a graph inside the computer. if there is an edge between i-th and j-th 101 . Graph is called weighted. weight of each road may be its length or minimal time needed to drive along.

adjacent to the current vertex. All graphs can be divided into two categories. It means that its adjacency matrix is symmetric. while it can be reduced to O(|V| + |E|). let us see an example. a graph is 102 . correspond to the diagonal elements of an adjacency matrix. Add (remove) an edge can be done in O(1) time. Adjacency matrix is optimal for dense graphs. when using adjacency list. On the other hand. if they are allowed in a graph. The last disadvantage. dense graphs contain number of edges comparable with square of number of vertices. In case. sparse and dense graphs. if there is an edge between two vertices. use of the adjacency matrix results in overall complexity of O(|V|2). the same time is required to check. 5) then there is also an edge (5. The graph presented by example is undirected. that square of number of vertices. Disadvantages. why there are two cells for every edge in the sample. For the algorithms like DFS or based on it. Advantages. Also it is very simple to program and in all our graph tutorials we are going to work with this kind of representation. in undirected graph. Sparse ones contain not much edges (number of edges is much less. Adjacency matrix is very convenient to work with. is that adjacency matrix requires huge efforts for adding/removing a vertex. if there is an edge (2. |E| << |V|2). This is also the reason. Before discussing the advantages and disadvantages of this kind of representation. Loops. which results in O(|V|) complexity. but for sparse ones it is superfluous. To draw out such an information from the adjacency matrix you have to scan over the corresponding row. • • • Adjacency matrix consumes huge amount of memory for storing big graphs. Next drawback of the adjacency matrix is that in many algorithms you need to know the edges. 2). we want to draw you attention to.vertices. Indeed. and 1 otherwise.

but the difference decreasing as a graph becomes denser. Next advantage is that adjacent list allows to get the list of adjacent vertices inO(1) time. which implies having constant number of vertices. Check.O(|E| / |V|) time. but if you want to construct fully dynamic structure. using of adjacency matrix make it quite slow for big graphs. than adjacency matrix. Disadvantages. which is a big advantage for some algorithms. adjacency matrix is a good solution for dense graphs. 103 . in particular situations even can outperform adjacency matrix. 14 245 35 425 Graph Adjacency list Advantages. on the average. b. if there is an edge between two vertices can be done in O(|E| / |V|) when list of adjacent vertices is unordered or O(log2(|E| / |V|)) when it is sorted.used for analysis only. • • Adding/removing an edge to/from adjacent list is not so easy as for adjacency matrix. For every vertex adjacency list stores a list of vertices. It requires. which may result in cubical complexity for dense graphs to add all edges. It requires less amount of memory and. This operation stays quite cheap. Adjacent list allows us to store graph in more compact form. it is not necessary. which are adjacent to current one.) Adjacency list This kind of the graph representation is one of the alternatives to adjacency matrix. Let us see an example. To sum up.

Adding new vertex can be done in O(V). which are based on DFS. Algorithm Algorithm : DFS Traversal(G) //Implement a depth first search traversal of a graph //Input : Graph G=<V. 1. The principle of the algorithm is quite simple: to go forward (in depth) while there is such possibility.1 Depth-first search (DFS) for undirected graphs Depth-first search.3 Algorithms associated with graphs and its time complexities 1. but removal results in O(E) complexity. than if using an adjacent matrix. Therefore. or DFS. is a way to traverse the graph. Initially it allows visiting vertices of the graph only. Conclusion : Adjacency list is a good solution for sparse graphs and lets us changing number of vertices more efficiently. if dynamically change of vertices number is required. otherwise to backtrack.3. But still there are better solutions to store fully dynamic graphs.E> //Output: Graph G with its vertices marked with consecutive integers in order they //have been first encountered by the DFS traversal Mark each vertex in V with 0 as a mark of being “unvisited” Count0 For each vertex v in V do If v is marked with 0 dfs(v) //end DFS Traversal Routine dfs (v) //visits recursively all unvisited vertices connected to vertex v and assigns them the //numbers in the order they are encountered via global variable count CountCount+1 Mark v with Count For each vertex w in V adajacent to v do 104 . but there are hundreds of algorithms for graphs. understanding the principles of depth-first search is quite important to move ahead into the graph theory.• Adjacent list doesn't allow us to make an efficient implementation.

Initially all vertices are white (unvisited). using DFS. For each edge (u. v). 2. Mark vertex u as black and backtrack to the parent. Start from a vertex with number 1. Source graph. Example : Traverse a graph shown below. NB. Mark vertex u as gray (visited). where u is white. black: DFS has finished processing the vertex. run depth-first search for u recursively. 105 .If w is marked woth 0 Dfs(w) //end dfs In DFS. 3. DFS starts in arbitrary vertex and runs as follows: 1. but we show general case here. each vertex has three possible colors representing its state: white: vertex is unvisited. gray: vertex is in progress. For most algorithms boolean classification unvisited / visited is quite enough.

106 . Go there. 4) and a vertex 4 is unvisited. Mark the vertex 4 as gray. There is an edge (1.Mark a vertex 1 as gray.

Go there. 5) and a vertex 5 is unvisited. Mark the vertex 2 as gray. 2) and vertex a 2 is unvisited. Go there. There is an edge (2. 107 .There is an edge (4.

108 . Go there. 3) and a vertex 3 is unvisited. There is an edge (5.Mark the vertex 5 as gray. Mark the vertex 3 as gray.

Mark it as black and backtrack to the vertex 2. There is an edge (5. 4). but the vertex 4 is gray.There are no ways to go from the vertex 3. Mark it as black and backtrack to the vertex 5. There are no ways to go from the vertex 5. 109 .

but the vertex 5 is black. Mark it as black and backtrack to the vertex 1.There are no more edges. There are no more edges. There is an edge (4. Mark it as black and backtrack to the vertex 4. adjacent to the vertex 4. 110 . adjacent to vertex 2. 5).

e. Write an algorithm for DFS traversal analyze its complexity. adjacent to a vertex can't be found efficiently. 2. adjacent to the vertex 1. As you can see from the example. which depth-first search has visited is a tree. DFS doesn't go through all edges. see finding connected components algorithm. 111 . What are the advantages and disadvantages of adjacency matrix and adjacency list method of representing the graph. Therefore. If a graph is disconnected.There are no more edges. This tree contains all vertices of the graph (if it is connected) and is called graph spanning tree. 1. What are the different ways of representing a graph explain it with the example. DFS won't visit all of its vertices. Depth-first search visits every vertex in the graph and checks every edge its edge. The vertices and edges. 3. if an adjacency matrix is used for a graph representation. This tree exactly corresponds to the recursive calls of DFS. Mark it as black. Complexity analysis Assume that graph is connected. DFS is over. For details. adjacency matrix and adjacency list method.4 SUMMARY: Graph can be represented in two ways i. As it was mentioned before. that results in O(V2) complexity. Check your progress 1. DFS complexity is O(V + E). then all edges.

2 1. Digraph is a graph with directions on its edges. loop.) Represent the matrix1 in adjacency list. cycle.15 KEYWORDS Graph : a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. 3.16 2. 1. 3. matrix1).) Write the equivalent graph for the above matrix (i. and the links that connect some pairs of vertices are called edges.2. Answers: SEE 112 .Adjacency matrix is a good solution for dense graph and adjacency list is good for sparse graph.e. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking. 1.1 Matrix 1 16. Depth-first search (DFS) is an algorithm for traversing or searching a tree. a. The interconnected objects are represented by mathematical abstractions called vertices. 1. weighted graph. Write a note on path .7 UNIT-END EXERCISES AND ANSWERS 15. 1. or graph. 1. b. tree structure. Apply a DFS traversal for a graph having an adjacency matrix ANSWERS TO CHECK YOUR PROGRESS 1.3.

Data structures. By Sartaj Sahni. . Analysis and design of algorithms with C/C++ .2 3. "Graph Algorithms".1. 5. Introduction to The design and analysis of algorithms by Anany Levitin 2.3. 1. Algorithms and applications in C++ -2nd edition. Shimon. Nandagopalan 3.1 1. Even.1 2.13 SUGGESTED READINGS 1.Computer Science Press.. Analysis and design of algorithms by Padma reddy 4.3rd edition by Prof. 1. 1. 113 .

114 .

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd