daa unit - i

37
DESIGN AND ANALYSIS OF ALGORITHMS – MC9223 Part – A 1. What is meant by algorithm? An algorithm is a step by step process for solving a problem or program. It is easy to understand by the programmer who is going to implement the algorithm. 2. Definition / Notation of algorithm. An algorithm is a sequence of unambiguous instructions for solving a computational problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. . 3. Write the rules / important points to develop an algorithm. The non-ambiguity requirement for each step of an algorithm. The range of inputs for which an algorithm works has been specified. The same algorithms are representing in different ways. Several algorithms for solving the same problem. 4. List out the implementation of algorithm. Finding a method for solving a problem. Every step of an algorithm should be in a clear manner. Psuedcode and flowchart are also used to describe the algorithm. All the steps in an algorithm should be done in manually. Finally implement the algorithm in terms of programming language. UNIT I INTRODUCTION Fundamentals of algorithmic problem solving – Important problem types – Fundamentals of the analysis of algorithm efficiency – analysis frame work – Asymptotic notations – Mathematical analysis for recursive and non-recursive Proble m Algorith m Compiler Input Output

Upload: subathra-devi-mourougane

Post on 27-May-2017

275 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: DAA Unit - I

DESIGN AND ANALYSIS OF ALGORITHMS – MC9223

Part – A1. What is meant by algorithm?

An algorithm is a step by step process for solving a problem or program. It is easy to understand by the programmer who is going to implement the algorithm.

2. Definition / Notation of algorithm.An algorithm is a sequence of unambiguous instructions for solving a computational

problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

.

3. Write the rules / important points to develop an algorithm. The non-ambiguity requirement for each step of an algorithm. The range of inputs for which an algorithm works has been specified. The same algorithms are representing in different ways. Several algorithms for solving the same problem.

4. List out the implementation of algorithm. Finding a method for solving a problem. Every step of an algorithm should be in a clear manner. Psuedcode and flowchart are also used to describe the algorithm. All the steps in an algorithm should be done in manually. Finally implement the algorithm in terms of programming language.

5. What are characteristics of an algorithm?

INPUT Zero or more quantities are externally supplied. OUTPUT At least one quantity is produced. DEFINITENESS Each instruction is clear and unambiguous. FINITENESS If we trace out the instructions of an algorithm, then for all cases, the

algorithm terminates after a finite number of steps. EFFECTIVENESS Every instruction must very basic so that it can be carried out, in

principle, by a person using only pencil & paper.

UNIT I INTRODUCTION

Fundamentals of algorithmic problem solving – Important problem types – Fundamentals of the analysis of algorithm efficiency – analysis frame work – Asymptotic notations – Mathematical analysis for recursive and non-recursive algorithms.

Problem

Algorithm

CompilerInput Output

Page 2: DAA Unit - I

6. List out the types of algorithms. Euclid’s algorithm Consecutive integer checking algorithm Middle-school algorithm.

7. What are the fundamentals steps involved in Algorithmic Problem Solving? [JUN 2011]

Understanding the problem Deciding on

Exact vs. approximate problem solving Appropriate data structure

Design an algorithm Proving correctness Analyzing an algorithm

Time efficiency : how fast the algorithm runs Space efficiency: how much extra memory the algorithm needs. Simplicity and generality

Coding an algorithm

8. Define sequential algorithms.The central assumption is that instructions are executed one after another, one

operation at a time. Accordingly algorithms are designed to be executed on such machines are called sequential algorithms.

9. Define parallel algorithmsSome new computers can execute operations concurrently; i.e. in parallel. Algorithms

that take advantage of this capability are called parallel algorithms

10. How to Choosing between Exact and Appropriate Problem Solving?

The principal decision is to choose between solving the problem exactly and solving it appropriately. The former case algorithm is called exact algorithm latter case, an algorithm is called appropriate algorithm.

11. What is Data Structure?

Algorithms + Data structures = Programs

A data structure is a specialized format for organizing and storing data. General data structure types include the array, the file, the record, the table, the tree, and so on. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways. 12. What is an algorithm design technique?

An algorithm design technique is a general approach to solving problems algorithmically that is applicable to a variety of problems for different areas of computing.

13. What is pseudocode?A pseudocode is a mixture of a natural language and programming language like

constructs. A pseudocode is usually more precise than an natural language.

14. What are the measures used to analyze an algorithm? [DEC 2010]There are two kinds of algorithm efficiency:

Time efficiency: indicates how fast the algorithm runs.

Page 3: DAA Unit - I

Space efficiency: indicates how much extra memory the algorithm needs.

15. Write the Important Problem Types of algorithm? Sorting Searching String processing Graph problems Combination problems Geometric problems Numerical problems

16. Define Sorting with an examples.Rearrange the items of a given list in ascending order. Sorting can be done to sort list

of numbers, characters from an alphabet, character strings and records maintained by schools about students and libraries about books and companies about their employees.

Examples of sorting algorithms Selection sort Bubble sort Insertion sort Merge sort Heap sort

17. What is meant by Sorting key?A specially chosen piece of information used to guide sorting. I.e., sort student

records by names.

18. What are the two properties of sorting algorithm? Stability: A sorting algorithm is called stable if it preserves the relative order of any

two equal elements in its input. In place: A sorting algorithm is in place if it does not require extra memory, except,

possibly for a few memory units.

19. Define Searching with examplesThe searching problem deals with finding a given value, called a search key, in a given

set.

Examples of searching algorithms Sequential searching Binary searching

20. What is meant by String?A string is a sequence of characters from an alphabet. Strings comprises of Text

strings: letters, numbers, and special characters. Bit strings comprises of zeros and ones.

21. What is String matching?String matching means searching for a given word / pattern in a text.

22. What is meant by Graph Problems?A graph is a collection of points called vertices, some of which are connected by line

segments called edges. Graphs can be used for:

Modeling real-life problems Modeling WWW Transportation Communication networks

Page 4: DAA Unit - I

Project scheduling

23. List out the examples of widely used graph algorithms. Graph traversal algorithms Shortest-path algorithms Topological sorting

24. Define traveling salesman problem. Traveling salesman problem which finds the shortest tour through n cities that visits

every city exactly once.

25. Define Graph coloring problem.The graph coloring problem assigns the smallest number of colors to vertices of a

graph so that no two adjacent vertices are the same color.

26. What is Combinational Problems?The traveling salesman problems and the graph coloring problems are examples of

combination problems. These problems ask to find a combinatorial object – such as permutation, a combination, or a subset – that satisfies certain constraints and has some desired property. 27. Define Geometric Problems.

Geometric algorithms deal with geometric objects such as points, lines and polygons. Ancient Greeks have invented procedures for solving problems of constructing simple geometric shapes – triangles, circles etc.

28. Define Numerical Problems:Numerical problems another large area of application, are problems that involve

mathematical objects of continuous nature: solving equations and systems of equations, computing definite integrals, evaluating functions.

29. How to analysis Framework of algorithm? Measuring an input’s size Measuring running time Orders of growth (of the algorithm’s efficiency function) Worst-base, best-case and average efficiency

30. How will you measuring an Input’s Size of a problem?Most programs runs longer on larger inputs example it takes longer to sort larger

arrays, multiply larger matrices. Efficiency is defined as a function of input size. Input size depends on the problem.

Example 1, what is the input size of the problem of sorting n numbers?Example 2, what is the input size of adding two n by n matrices?

31. Define Basic operation.The most important operation of the algorithm, the operation contributing the most

to the total running time.

For example, the basic operation is usually the most time-consuming operation in the algorithm’s innermost loop.

ExamplesProblem Input size measure Basic operation

Page 5: DAA Unit - I

Search for a key in a list of n items Number of items in list, n Key comparison

Add two n by n matrices Dimensions of matrices, n addition

Polynomial Evaluation Order of the polynomial multiplication

31. Explain the formula of Basic operation. Let cop - Be the time of execution of an algorithms basic operation on a particular

computer Let C(n) – be the number of times this operation needs to be executed for this

algorithm. Then we can estimate the running time T(n) of a program implementing this algorithm on that computer by the formula. T(n) ≈ copC (n).

32. Define Worst case EfficiencyThe worst case efficiency of an algorithm is its Efficiency for the worst case input of

size n. For which the algorithm runs the longest among all possible inputs of size n.

Analyze the algorithm to see what kind of inputs yield largest value of the basic operations count C(n) among the possible inputs of size n and compute the worst case value Cworst(n).

33. Define Best case EfficiencyThe best case efficiency of an algorithm is its Efficiency for the best case input of size

n. For which the algorithm runs the fastest among all possible inputs of size n. Then we should ascertain the value of C(n) on these inputs.

Example for sequential search, best case inputs will be lists of size n with their first elements equal to a search key according to Cbest(n) =1.

34. Define Average case EfficiencyThe average case efficiency of an algorithm is its Efficiency for a typical/random input

of size n. NOT the average of worst and best case.

35. What are the standard assumptions of Average case Efficiency? The probability of a successful search is equal to p (0<=p<=1) The probability of the first match occuririn in the ith position of the list is the same for

every i.

36. Define Asymptotic Notations with examples. [DEC 2010]

Asymptotic notations used to compare orders of growth of an algorithm’s basic operation count for the algorithms efficiency. Types are

O (big Oh) Ω (big omega) Θ (big theta)

37. Write a brief note on O-notation. [DEC 2012]

A function t(n) is said to be in O(g(n)), denoted t(n) ÎO(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that

t(n) £ cg(n) for all n ³ n0

Page 6: DAA Unit - I

38. Define W-notation.A function t(n) is said to be in W(g(n)), denoted t(n) Î W(g(n)), if t(n) is bounded below

by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that

t(n) ³ cg(n) for all n ³ n0

39. Define Q-notation (Theta). [JUN 2012]A function t(n) is said to be in Q(g(n)), denoted t(n) Î Q(g(n)), if t(n) is bounded both

above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constant c1 and c2 and some nonnegative integer n0 such that

c2 g(n) £ t(n) £ c1 g(n) for all n ³ n0

40. What is recursive algorithm?An algorithm is said to be recursive if the same algorithm is invoked in the body. An

algorithm that calls itself is direct recursive. Algorithm A is said to be indeed recursive if it calls another algorithm, which in turn calls A.

41. Write the General plan for analyzing the efficiency for non recursive algorithms? [JUN 2011]

Decide on a parameter indicating an input’s size. Identify the algorithm’s basic operation. Check whether the number of times the basic operation is executed depends only in

the size of an input. If it depends on some additional properties the worst case, average case, best case efficiencies have to be investigated.

Set up sum expressing the number of times the algorithm’s basic operation is executed.

Using standard formulas and rules of sum manipulation, either find a closed form formula for the count or, at the very least, establish its order of growth.

42. Write the General plan to follow in analysis recursive algorithms? Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for input of size n Set up a recurrence relation and initial condition(s) for C(n)-the number of times the

basic operation will be executed for an input of size n. Solve the recurrence or estimate the order of magnitude of the solutions

43. Write demerits of recursive algorithms: Many programming languages do not support recursion; hence, recursive

mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion it is

always at the cost of execution time and memory space. 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, a return to the proper location will result when the return to a calling statement is made.

The recursive programs needs considerably more storage and will take more time.

44. What is performance measurement?Performance measurement is concerned with obtaining the space and the time

requirements of a particular algorithm.

45. Define input size.

Page 7: DAA Unit - I

The input size of any instance of a problem is defined to be the number of words (or the number of elements) needed to describe that instance.

46. Write algorithm using Non-recursive function to fine sum of n numbers.algorithm sum(a,n){s : = 0.0for i=1 to n dos : - s + a[i];return s;}

47. Write an algorithm using Recursive function to fine sum of n numbers,algorithm rsum (a,n){if(n_ 0) thenreturn 0.0;elsereturn rsum(a, n- 1) + a(n);

49. Define Doubly Linked list. [DEC 2011]A Doubly linked list is a linked data structure that consists of a set of sequentially

linked records called nodes. Each node contains two fields, called links that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.50. Distinguish between Worst case and Best case efficiency. [DEC 2011]

The worst case scenario, on the other hand, describes the absolute worst set of input for a given algorithm. Let's look at a quick sort, which can perform terribly if you always choose the smallest or largest element of a sub list for the pivot value. This will cause quick sort to degenerate to O(n2).

The best case scenario for an algorithm is the arrangement of data for which this algorithm performs best. Take a binary search for example. The best case scenario for this search is that the target value is at the very center of the data you're searching. So the best case time complexity for this would be O(1).

Discounting the best and worst cases, we usually want to look at the average performance of an algorithm. These are the cases for which the algorithm performs "normally."

51. Difference between sequential and parallel algorithms. [JUN 2012]Sequential algorithms:

The central assumption is that instructions are executed one after another, one operation at a time. Accordingly algorithms are designed to be executed on such machines are called sequential algorithms.

Parallel algorithms:Some new computers can execute operations concurrently; i.e. in parallel. Algorithms

that take advantage of this capability are called parallel algorithms

52. Why is an algorithm important in problem solving? [DEC 2012]

Page 8: DAA Unit - I

The term “algorithmic problem solving” is deliberately ambiguous. On the one hand, it can be read as “algorithmic-problem solving”; this means solving problems that require the formulation of an algorithm for their solution. On the other hand, it can be read as “algorithmic problem-solving”; this means exploiting what has been learned from the experience of developing computer software over the last 50 years that has helped us to hone our problem-solving skills.

Part – B

1. Discuss about algorithm and its types with examples.

An algorithm is a sequence of unambiguous instructions for solving a computational problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

Types of algorithms:

Euclid’s algorithm Consecutive integer checking algorithm Middle-school algorithm.

As an example of illustrating the notion of algorithm, we consider three methods for solving the same problem: Computing the greatest common divisor.

Greatest common divisor of two non negative, not- both-zero integers m and n, denoted by gcd(m,n), is defined as the largest integer that divides both m and n evenly.

(i) Euclid’s algorithm:

Euclid’s algorithm is a famous method which applies repeated equality.

For example: gcd(60,24) = gdc(24,12)=gcd(12,0)=12.

gcd(m, n): the largest integer that divides both m and n. First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n)

Step1: If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step2: Divide m by n and assign the value of the remainder to r.Step3: Assign the value of n to m and the value of r to n. Go to Step 1.

Pseudocode of Euclid’s Algorithm:

Algorithm Euclid(m, n)//Computes gcd(m, n) by Euclid’s algorithm//Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n ‡ 0 do

r ß m mod nm ß nn ß r

return m

(ii) Consecutive Integer Algorithm

Page 9: DAA Unit - I

Step1: Assign the value of min{m, n} to t. Step2: Divide m by t. If the remainder of this division is 0, go to Step3;otherwise, go

to Step2 Step3: Divide n by t. If the remainder of this division is 0, return the value of t as the

answer and stop; otherwise, proceed to Step4. Step4: Decrease the value of t by 1. Go to Step2.

(iii) Middle-school procedure

Step1: Find the prime factors of m. Step2: Find the prime factors of n. Step3: Identify all the common factors in the two prime expansions found in Step1

and Step2. (If p is a common factor occurring Pm and Pn times in m and n, respectively, it should be repeated in min{Pm, Pn} times.)

Step4: Compute the product of all the common factors and return it as the gcd of the numbers given.

Thus for the numbers 60,24 we get:

60 = 2 . 2 . 3 . 524 = 2 . 2 . 2 . 3gcd(60,24) = 2 . 2 . 3 = 12

2. List out the fundamentals steps of Problem solving algorithm. Discuss briefly about analyze phase of algorithm. [DEC 2011, JUNE & DEC 2012]

(OR)Discuss in detail: Features of the top down design and bottom up design approaches in problem solving. [DEC 2012]

Structure of Fundamentals of Algorithmic Problem Solving

Page 10: DAA Unit - I

Understanding the problemo Reading the problems description carefully, Asking questions do a few

examples by hand; think about special cases, etc. Deciding on

o Exact vs. approximate problem solvingo Appropriate data structure

Design an algorithm Proving correctness Analyzing an algorithm

o Time efficiency : how fast the algorithm runso Space efficiency: how much extra memory the algorithm needs.o Simplicity and generality

Coding an algorithmUnderstanding the problem:

An input to an algorithm specifies an instance of the problem the algorithms solves. It is very important to specify exactly to range of instances the algorithms needs to handle. Failure to do this, the algorithm may work correctly for a majority of input but crash on some “boundary” value. A correct algorithm is not one that works most of the time but one that works correctly for all legitimate inputs.

Ascertaining the Capabilities of a Computational Device:

Page 11: DAA Unit - I

Once having understood a problem, one must ascertain the capabilities of the computational device the algorithm is intended for. In the case of random-access memory (RAM), its central assumption is that instructions are executed one after another, one operation at a time. Accordingly algorithms are designed to be executed on such machines are called sequential algorithms.

Some new computers can execute operations concurrently; i.e. in parallel. Algorithms that take advantage of this capability are called parallel algorithms

Choosing between Exact and Appropriate Problem Solving:

The next principal decision is to choose between solving the problem exactly or solving it appropriately. The former case algorithm is called exact algorithm latter case, an algorithm is called appropriate algorithm.

Deciding on Appropriate Data Structure:

In the new world of object oriented programming, data structures remain crucially important for both design and analysis of algorithms.Algorithms + Data structures = Programs

Algorithm Design Techniques:An algorithm design technique is a general approach to solving problems

algorithmically that is applicable to a variety of problems for different areas of computing.

Methods of Specifying an Algorithm:Once having designed an algorithm, you need to specify it in some fashion. Using natural language is obviously difficult.

A pseudocode is a mixture of a natural language and programming language like constructs. A pseudocode is usually more precise than an natural language.

Proving an Algorithm’s Correctness:Once an algorithm has been specified, you have to prove its correctness. That is you

have to prove that the algorithm yields a required result for every legitimate input in a finite amount of time. For algorithms, a proof of correctness is to use mathematical induction because an algorithm’s iteration provides a natural sequence of steps needed for such proofs.

The notion of correctness for approximation algorithms is less straightforward than it is for exact algorithms. Errors for approximation algorithms should not exceed a predefined limit.Analyzing an Algorithm:

After correctness, the most important is efficiency. There are two kinds of algorithm efficiency:

Time efficiency: indicates how fast the algorithm runs. Space efficiency: indicates how much extra memory the algorithm needs.

Another characteristic of an algorithm is simplicity. Simplicity is an important algorithm characteristic. Because simple algorithms are easier to understand and easier to program. Resulting programs usually contain fewer bugs. Simper algorithms are also more efficient than more complicated alternatives.

Another characteristic of an algorithm is generality. There are two issues here:

Page 12: DAA Unit - I

Generality of the problem the algorithm solves – it is easier to design an algorithm for a more general problem. But there are situations where designing a more general algorithm is unnecessary or difficult or even impossible.

The range of inputs it accepts – your main concern should be designing an algorithm that can handle a range of inputs that is natural for the problem at hand.

Coding an Algorithm:

Most algorithms are ultimately implemented as computer programs. Transition of algorithm to a program must be done carefully. Validity of the computer program is established by testing. Testing of computer programs is an art rather than science.

3. Enumerate and explain briefly important types of problem. [JUN 2011]

Sorting Searching String processing Graph problems Combination problems Geometric problems Numerical problems

Sorting:

Rearrange the items of a given list in ascending order. Sorting can be done to sort list of numbers, characters from an alphabet, character strings and records maintained by schools about students and libraries about books and companies about their employees.

Input: A sequence of n numbers <a1, a2, …, an>Output: A reordering <a´1, a´2, …, a´n> of the input sequence such that a´1≤ a´2 ≤ … ≤ a´n.

Sorting key

A specially chosen piece of information used to guide sorting. I.e., sort student records by names.

Examples of sorting algorithms Selection sort Bubble sort Insertion sort Merge sort Heap sort

Evaluate sorting algorithm complexity: the number of key comparisons.

Two properties

Stability: A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input.

In place: A sorting algorithm is in place if it does not require extra memory, except, possibly for a few memory units.

Page 13: DAA Unit - I

Searching:The searching problem deals with finding a given value, called a search key, in a given

set. The examples of searching algorithms are as follows,

Sequential searching Binary searching

String A string is a sequence of characters from an alphabet. Strings comprises of text strings: letters, numbers, and special characters. Bit strings comprises of zeros and ones. String matching: searching for a given word/pattern in a text.

Graph Problems A graph is a collection of points called vertices, some of which are connected by line segments called edges. Graphs can be used for:

Modeling real-life problems Modeling WWW transportation communication networks Project scheduling …

Examples of widely used graph algorithms:

Graph traversal algorithms Shortest-path algorithms Topological sorting

The widely known graph problems are:

Traveling salesman problem – Which finds the shortest tour through n cities that visits every city exactly once.

The graph coloring problem – assigns the smallest number of colors to vertices of a graph so that no two adjacent vertices are the same color.

Combinational Problems:

The traveling salesman problems and the graph coloring problems are examples of combination problems. These problems ask to find a combinatorial object – such as permutation, a combination, or a subset – that satisfies certain constraints and has some desired property. Geometric Problems:

Geometric algorithms deal with geometric objects such as points, lines and polygons. Ancient Greeks have invented procedures for solving problems of constructing simple geometric shapes – triangles, circles etc.

Numerical Problems:

Numerical problems another large area of application, are problems that involve mathematical objects of continuous nature: solving equations and systems of equations, computing definite integrals, evaluating functions.

Page 14: DAA Unit - I

4. What is an algorithm? What are the analysis frameworks in measuring the efficiency? Give examples. [DEC 2010]

Algorithm:

An algorithm is a sequence of unambiguous instructions for solving a computational problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

Analysis of algorithms means to investigate an algorithm’s efficiency with respect to resources: running time and memory space.

Time efficiency: how fast an algorithm runs.Space efficiency: the space an algorithm requires.

Analysis Framework

Measuring an input’s size Measuring running time Orders of growth (of the algorithm’s efficiency function) Worst-base, best-case and average efficiency

Measuring an Input’s Size

Most programs runs longer on larger inputs example it takes longer to sort larger arrays, multiply larger matrices.

Efficiency is defined as a function of input size. Input size depends on the problem.

Example 1, what is the input size of the problem of sorting n numbers? Example 2, what is the input size of adding two n by n matrices?

Units for Measuring Running Time Measure the running time using standard unit of time measurements, such as

seconds, minutes.o This Depends on the speed of the computer.

Count the number of times each of an algorithm’s operations is executed.o This approach is Difficult and unnecessary

Count the number of times an algorithm’s basic operation is executed.

Basic operation: the most important operation of the algorithm, the operation contributing the most to the total running time.

For example, the basic operation is usually the most time-consuming operation in the algorithm’s innermost loop.

Input Size and Basic Operation Examples

Problem Input size measure Basic operation

Search for a key in a list of n items Number of items in list, n Key comparisonAdd two n by n matrices Dimensions of matrices, n addition

Page 15: DAA Unit - I

Polynomial Evaluation Order of the polynomial multiplication

Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size.

Let cop - Be the time of execution of an algorithms basic operation on a particular computer Let C(n) – be the number of times this operation needs to be executed for this algorithm. Then we can estimate the running time T(n) of a program implementing this algorithm on that computer by the formula.

T(n) ≈ copC (n)Order of growth:

An order of growth is a set of functions whose asymptotic growth behavior is considered equivalent. For example, 2n, 100n and n + 1 belong to the same order of growth, which is written O(n) in Big-Oh notation and often called linear because every function in the set grows linearly with n.

Order ofgrowth

Name

O(1) constantO(logb n) logarithmic (for any b)

O(n) linearO(n logb n) “en log en”

O(n2) quadraticO(n3) cubicO(cn) exponential (for any c)

Worst-Case, Best-Case, and Average-Case Efficiency

Algorithm efficiency depends on the input size n but for some algorithms efficiency depends on not only on the input size also on type of input.

Consider an example: Sequential Search

Problem: Given a list of n elements and a search key K, find an element equal to K, if any.

Algorithm: Scan the list and compare its successive elements with K until either a matching

element is found (successful search) of the list is exhausted (unsuccessful search)

Sequential Search Algorithm

ALGORITHM SequentialSearch(A[0..n-1], K)//Searches for a given value in a given array by sequential search//Input: An array A[0..n-1] and a search key K//Output: Returns the index of the first element of A that matches K or –1 if there are no matching elementsi ß0while i < n and A[i] ‡ K do

i ß i + 1

Page 16: DAA Unit - I

if i < n //A[I] = Kreturn i

elsereturn -1

Worst case EfficiencyThe worst case efficiency of an algorithm is its Efficiency for the worst case input of

size n. For which the algorithm runs the longest among all possible inputs of size n.

Analyze the algorithm to see what kind of inputs yield largest value of the basic operations count C(n) among the possible inputs of size n and compute the worst case value Cworst(n).

Best case EfficiencyThe best case efficiency of an algorithm is its Efficiency for the best case input of size

n. For which the algorithm runs the fastest among all possible inputs of size n.

Then we should ascertain the value of C(n) on these inputs. Example for sequential search, best case inputs will be lists of size n with their first elements equal to a search key according to Cbest(n) =1.

Average case EfficiencyThe average case efficiency of an algorithm is its Efficiency for a typical/random input of size n. NOT the average of worst and best case. How to find the average case efficiency?Consider the sequential search.

The standard assumptions are:a) The probability of a successful search is equal to p (0<=p<=1)b) The probability of the first match occurring in the position of the list is the same for

every i.

The average number of key comparisons Cavg(n) as follows:

The first match occurring in the ith position of the list is p/n for every i, and the number of comparisons made by the algorithm is i. In the case of an unsuccessful search, the number of comparisons is n with the probability of such a search being (1-p).

Cavg(n) = [1.p/n + 2.p/n + . . . + i.p/n + . . . + n.p/n] + n. (1-p)

= p/n [ 1+2+. . . + I + . . . + n] + n (1-p)

= p/n . n(n+1)/2 + n(1-p)

= p(n+1)/2 + n(1-p)

5. Briefly explain asymptotic Notations and types with its graphs.

Asymptotic notations used to compare orders of growth of an algorithm’s basic operation count for the algorithms efficiency. Types are

O (big Oh) Ω (big omega) Θ (big theta)

Page 17: DAA Unit - I

O(g(n)): class of functions f(n) that grow no faster than g(n) Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) Θ (g(n)): class of functions f(n) that grow at same rate as g(n)

Where,t(n) – algorithms running timeC(n) – indicates the basic operation countg(n) – simple functions to compare the count with.

O-notation

Formal definition

A function t(n) is said to be in O(g(n)), denoted t(n) ÎO(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that

t(n) £ cg(n) for all n ³ n0

Example: 100n + 5 Î O(n2) 100n + 5 £ 100 n + n (for all n ³5) = 101n Î 101 n2

Thus the value of the constants c and n0 100n + 5£100n + 5n (for all n>1) = 105nTo complete the proof with c = 105 and n0= 1

W -notation

Formal definitionA function t(n) is said to be in W(g(n)), denoted t(n) Î W(g(n)), if t(n) is bounded below by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that

t(n) ³ cg(n) for all n ³

Page 18: DAA Unit - I

n0

Example of the formal proof that n3 Î W(n2):n3 ³ n2 for n³ 0.

i.e. we can select c=1 and n0 = 0

Q -notation Formal definition

A function t(n) is said to be in Q(g(n)), denoted t(n) Î Q(g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constant c1 and c2 and some nonnegative integer n0 such that

c2 g(n) £ t(n) £ c1 g(n) for all n ³ n0

Example let us prove that 1/2n(n-1) Î Q(n2) First we prove the right inequality (upper bound):

Page 19: DAA Unit - I

1/2n(n-1) = 1/2n2 - 1/2n £ 1/2n2 for all n ³ 0.

Second we prove the left inequality (lower bound):

1/2n(n-1) = 1/2n2 - 1/2n ³ 1/2n2 - 1/2n 1/2n for all n ³ 2 = 1/4n2

Hence we can select c2 = ¼, c1= ½ and no = 2

6. Describe briefly about mathematical analysis of non recursive algorithms. [DEC 2011 & JUNE 2012]

Example 1:Consider the problem of finding the value of the largest element in a list of n

numbers. Assume the list is implemented as array. The following is a pseudocode of a standard algorithm for solving the problem.

Algorithm MaxElement (A[0..n-1])//Determines the value of the largest element in a given array//Input : An array A[0..n-1] of real numbers//Output: The value of the largest element in AMaxval ß A[0]for i ß 1 to n – 1 doif A[i] > maxval

Maxval ßA[i]return maxval

Inout Size here is the array n. The operation to be executed most often is for loop. There are two operations in the loops body:

Comparison A[i]>maxval Assignment maxval ßA[i]

Which of these two operations must we consider basic operation?Comparison is executed on each repetition of the loop and not assignment

Let us denote C(n) the number of lines this comparison is executed. The algorithm makes one comparison on each execution of the loop, which is repeated for each value of the loop’s variable i within the bounds between 1 and n-1. Therefore we get the following sum for C(n):

n-1C(n) = Σ 1 = n-1 Î Q(n).

i=1The General plan to follow in analyzing non recursive algorithms is:

1. Decide on a parameter indicating an input’s size.2. Identify the algorithm’s basic operation.3. Check whether the number of times the basic operation is executed depends only in

the size of an input. If it depends on some additional properties the worst case, average case, best case efficiencies have to be investigated.

4. Set up sum expressing the number of times the algorithm’s basic operation is executed.

5. Using standard formulas and rules of sum manipulation, either find a closed form formula for the count or , at the very least, establish its order of growth.

Example 2:

The following algorithm finds the number of binary digits in the binary representation of a positive decimal integer.

ALGORITHM Binary(n)

Page 20: DAA Unit - I

//Input: A positive decimal integer n//Output: The number of binary digits in n’s binary representationcount ß 1while n>1 docount ß count +1nß[n/2]return count

In this the most frequently executed operation is not inside the while loop but rather the comparison n>1 that determines whether the loop’s body will be executed. Since the number if times the comparison will be executed is larger than the number of repetitions of the loop’s body by exactly 1, the choice is not that important.

A more significant feature of this example is the fact that the loop’s variable takes on only a few values between its lower and upper limits; therefore we have to use an alternative way of computing the number of times the loop is executed. Since the value of n is about halved on each repetition of the loop, the answer would be log2n.

7. Define Recursive algorithm. Explain the mathematical analysis procedures with Towers of Hanoi puzzle example. [DEC 2010 & JUNE 2012]

Recursive evaluation of n! Recursive solution to the Towers of Hanoi puzzle Recursive solution to the number of binary digits problem

Example Recursive evaluation of n ! (1)

Iterative Definition Recursive definition

Algorithm F(n)

// Computes n! recursively// Input: A nonnegative integer n//Output: the value of n!if n=0

return 1 //base caseelse

return F (n -1) * n //general case

Example Recursive evaluation of n ! (2)

Two Recurrences o The one for the factorial function value: F(n)

F(n) = F(n – 1) * n for every n > 0 F(0) = 1

o The one for number of multiplications to compute n!, M(n)

M(n) = M(n – 1) + 1 for every n > 0M(0) = 0M(n) ∈ Θ (n)

Steps in Mathematical Analysis of Recursive Algorithms

Page 21: DAA Unit - I

Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for input of size n Set up a recurrence relation and initial condition(s) for C(n)-the number of times

the basic operation will be executed for an input of size n. Solve the recurrence or estimate the order of magnitude of the solutions

Example : The Towers of Hanoi Puzzle

In this puzzle we have n disks of different sizes and three pegs. Initially all the disks are in the first peg in order of sixw, the largest on the bottom and the smallest on top. The goal is to move all the disks to the third peg, using the second one as an auxiliary, if necessary. We can move only one disk at a time, an it is forbidden to place a larger disk on top of the smaller one.

To move n>1 disks from peg 1 to peg 3(with peg 2 as auxiliary), we first moce recursively n – 1 disks from peg 1 to peg 2 (with peg 3 as auxiliary), then move the largest disk directly from peg 1 to peg 3, and finally move recursively n – 1 disks from peg 2 to pe 3(using peg1 as auxiliary). If n=1 then we can directly move the disk directly from the source peg to the destination peg.

The number of moves M(n) depends on n only, and we get the following recurrence equation for it:

M(n) = M(n – 1) + 1+M(n - 1) for n>1.

With the obvious initial condition M(1) = 1, we have the following recurrence relation for the number of moves M(n):

M(n) = 2M(n – 1) + 1 for every n > 1

M(1) = 1

We solve this recurrence by te same method of backward substitutions:

M(n) = 2M(n – 1) + 1 sub. M(n – 1) = 2M(n – 2) + 1=2[2M(n – 2) + 1] + 1 = 22M(n – 2)+2+1 sub. M(n – 2) = 2M(n – 3) + 1

=22 [2M(n – 3)+1]+2+1 = 23 M(n – 3)+ 22+2+1 The next one will be24 M(n – 4)+ 23+ 22+2+1

And after the ith substitution, we get

M(n) = 2i M(n – i)+ 2i-1+ 2i-2+…+2+1 = 2i M(n – i)+ 2i-1

Since the initial condition is specified for n=1, which is achieved for i=n-1, we get the following formula for the solution to recurrence

M(n)= 2 n-1 M(n – (n -1)) + 2 n-1 -1

= 2 n-1 M(1) + 2 n-1 -1 = 2 n-1 + 2 n-1 -1 = 2 n -1

Page 22: DAA Unit - I

Problem

Page 23: DAA Unit - I
Page 24: DAA Unit - I

8. Write an algorithm for finding maximum element of an array perform best, worst and average case complexity with appropriate order notations. [JUN 2011]

Consider the problem of finding the value of the largest element in a list of n numbers. Assume the list is implemented as array. The following is a pseudocode of a standard algorithm for solving the problem.

Algorithm MaxElement (A[0..n-1])//Determines the value of the largest element in a given array//Input : An array A[0..n-1] of real numbers//Output: The value of the largest element in AMaxval ß A[0]for i ß 1 to n – 1 doif A[i] > maxval

Maxval ßA[i]return maxval

Input Size here is the array n. The operation to be executed most often is for loop. There are two operations in the loops body:

Comparison A[i]>maxval Assignment maxval ßA[i]

Which of these two operations must we consider basic operation?Comparison is executed on each repetition of the loop and not assignment

Let us denote C(n) the number of lines this comparison is executed. The algorithm makes one comparison on each execution of the loop, which is repeated for each value of the loop’s variable i within the bounds between 1 and n-1. Therefore we get the following sum for C(n):

n-1C(n) = Σ 1 = n-1 Î Q(n).

i=1Program:

#include<stdio.h>intmain (){ int i; int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 }; int maxval = a[0];

Page 25: DAA Unit - I

for (i = 0; i < 10; i++){ if (a[i] > maxval)

{ maxval = a[i];}

Return maxval }

printf ("Maximum element in an array : %d\n", maxval); return 0;}

Best case:Best case - finding the max element as the first (O(1)),

Worst Case:Worst case - it is the last element checked (O(n)).

Average Case:

o The tricky part is the average case. o To find the average case - we need the expected number of iterations!

o Since you can stop after you find the maximum, we can split the problem into two parts:

o [0,n-1): Since on average (assuming uniform independent distribution for each element) - the number n has probability 1/n to be in each place, then the expected number of iterations for this part is 1/n + 2*((n-1)/n)/n + 3 * ((n-1)/n)^2/n + ... + (n-1) * ((n-1)/n)^(n-2)/n 1

o The above formula yields an ugly formula which is O(n)

o The last element is going to be checked if the first n-1 elements did not contain the value n: so you need to add to the above n* ((n-1)/n)^(n-1), which is O(n) as well (lim to infinity is 1/e * n).

o This totals in O(n) average time solution.

9. Explain Graph and Geometric problems in detail. [DEC 2011 & JUN 2012]

Graph and geometric problems have been studied by computer science researchers using the framework of analysis of algorithms. Graph theory is the study of the properties of graphs. Graph algorithms are one of the oldest classes of algorithms and they have been studied for almost 300 years.

Graph Problems:

A graph is a collection of points called vertices, some of which are connected by line segments called edges. Graphs provide essential models for many applications areas of computer science, and at the same time, they are fascinating objects of study in pure and applied mathematics.

Page 26: DAA Unit - I

There have been a number of exciting recent developments in graph theory that are important for designers of algorithms to know about. Correspondingly, the algorithmic viewpoint of computer science has stimulated much research in graph theory. Graph theory and graph algorithms are inseparably intertwined subjects.

Graphs can be used for: Modeling real-life problems Modeling WWW transportation communication networks Project scheduling …

Examples of widely used graph algorithms:

Graph traversal algorithms Shortest-path algorithms Topological sorting

The widely known graph problems are:

Traveling salesman problem – Which finds the shortest tour through n cities that visits every city exactly once.

The graph coloring problem – assigns the smallest number of colors to vertices of a graph so that no two adjacent vertices are the same color.

Geometric Problems:Geometric algorithms deal with geometric objects such as points, lines and polygons.

Ancient Greeks have invented procedures for solving problems of constructing simple geometric shapes – triangles, circles etc.

The main impetus for the development of geometric algorithms came from the progress in computer graphics, computer-aided design and manufacturing. In addition, algorithms are also designed for geometric problems that are classical in nature. The success of the field can be explained from the beauty of the geometry problems studied, the solutions obtained, and by the many application domains- computer graphics, geographic information systems, robotics and others, in which geometric algorithms play a crucial role.

Graph and geometric algorithms are at the heart of many computer applications. So, it is expected that computer scientists and professional programmers know frequently used algorithms and generic techniques for efficient organization and retrieval of data, modeling, understanding and solving graph and geometric problems.

10. Discuss about Worst-Case efficiency. [DEC 2011]The worst case efficiency of an algorithm is its Efficiency for the worst case input of

size n. For which the algorithm runs the longest among all possible inputs of size n.

Analyze the algorithm to see what kind of inputs yield largest value of the basic operations count C(n) among the possible inputs of size n and compute the worst case value Cworst(n).

Examples:

Sequential Search Assume that the data in the array is in no particular order.

Page 27: DAA Unit - I

Worst-case: Not found or searching for the item that is in the last position. f(n) = n

Ordered Sequential Search : Requires the precondition that the data in the array is ordered by the key field that

will be used for the search. This additional overhead will affect the efficiency of the search, but for now will not be considered (efficiency of sorting algorithms is the topic of the other half of this chapter).

worst-case: Searching for the item that is greater than or equal to the item in the last position. f(n) = n

Binary Search :

Requires the precondition that the data in the array is ordered by the key field that will be used for the search. This additional overhead will affect the efficiency of the search, but for now will not be considered.

Worst-case: Not found or searching for the item that would be the last one checked before determining it is not in the array. f(n) = log2n (see below for the derivation of this result)

Determining the worst-case calculation for the binary search...

The binary search begins by looking at the middle item in the array (at position n/2). This results in three possibilities:

a. This is the item you are looking for, in which case you are now done. But this would not be the worst case, therefore we will ignore this possibility.

b. The item you are looking for is less than (i.e. comes before) the middle item. Therefore, you can throw away the middle item and the second half of the list and search the first half of the list by the same process. This results in searching from among n/2 items.

c. The item you are looking for is greater than (i.e. comes after) the middle item. Therefore, you can throw away the middle item and the first half of the list and search the second half of the list by the same process. This results in searching from among n/2 items.

o The first comparison results in n/2 items left over.o The second comparison results in (n/2)/2 = n/(22) items left over.o The third comparison results in ((n/2)/2)/2 = n/(23) items left over.o The fourth comparison results in n/(24) items left over.o Eventually, the kth comparison results in n/(2k) items left over. If this results in one

item left then only one more check needs to be made.

Therefore, approximately k comparisons are needed to search the array. After k comparisons, there would be only 1 item left over. Therefore...

n/(2k) = 12k = n

k = log2n

Since log2n is less than n, we may conclude that a binary search is more efficient that a linear search.

Page 28: DAA Unit - I

11. Write short notes on sorting [JUN 2012]

Sorting is arranging or ordering the data in some logical order either increasing or decreasing. Many Sorting algorithms are available to sort the given set of elements.

i) Internal Sorting

If the data to be sorted remains in main memory and also the sorting is carried out in main memory it is called internal sorting. The types of internal sorting are as follows,

Insertion sort Selection sort Merge Sort Radix Sort Quick Sort Heap Sort Bubble Sort

ii) External Sorting

If the data resides in auxiliary memory and is brought into main memory in blocks for sorting and then result is returned back to auxiliary memory is called external sorting. The External sorting methods are applied only when the number of data elements to be sorted is too large.

These methods involve as much external processing as processing in the CPU. The following are the examples of external sorting.

Sorting with Disk Sorting with tapes. Sorting Algorithms

o Insertion Sort o Selection Sort o Quicksort o Mergesort

Sorting time may depend on:

initial order of data data structure used to represent data memory available size of record sort algorithm Location of list: internal storage, external device, etc. and more

Selection Sort       O(n^2)

In this sorting we find the smallest element in this list and put it in the first position. Then find the second smallest element in the list and put it in the second position. And so on.

1. Find smallest item in list. 2. Exchange it with "first" item in list. 3. Repeat, beginning with the rest of the list.

Page 29: DAA Unit - I

Quicksort       O(nlog2(n))

Quicksort was invented and named by C. A. R. Hoare and is one of the best general-purpose sorting algorithms. It is built on the ideal of partitions, and it uses divide-and-conquer strategy. The basic algorithm for a one-dimensional array is as follows.

1. Partition Step: Select an element to place in its final position in the array. That is, all the elements to the left will be less than selected element, and all the elements to the right will be greater than the chosen element. We will select the first element in the array and put it in its final place in the array. Then we have one element in its proper location and two unsorted subarrays.

2. Recursive Step: Repeat the process on each unsorted subarray.

Each time the partition step is repeated, another element is placed in its final position in the sorted array, and two additional subarrays are created. When a subarray eventually contains only one element, that subarray is sorted and the element is in its final location.

Insertion Sort O ( n 2).

Sorting the given elements from 1 to n, inserting each element into its proper position. An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence. Both average and worst-case time is O(n2).

Merge Sort

Combing the two lists is called as merging. For example A is a sorted list with r elements and B is a sorted list with s elements. The operation that combines the elements of A and B into a single sorted list C with n = r + s elements is called merging. After combing the two lists the elements are sorted by using the following merging algorithm suppose one is given two sorted decks of cards.

ANNA UNIVERSITY QUESTIONS

Page 30: DAA Unit - I

Part - A1. What are the fundamentals steps involved in Algorithmic Problem Solving? [Ref. No.: 7]2. What are the measures used to analyze an algorithm? [Ref. No.: 14]3. Define Asymptotic Notations with examples. [Ref. No.: 36]4. Write a brief note on O-notation. [Ref. No.: 37]5. Define Q-notation (Theta). [Ref. No.: 39]6. Write the General plan for analyzing the efficiency for non recursive algorithms?

[Ref. No.: 41]7. Define Doubly Linked list. [Ref. No.: 49]8. Distinguish between Worst case and Best case efficiency. [Ref. No.: 50]9. Difference between sequential and parallel algorithms. [Ref. No.: 51]10. Why is an algorithm important in problem solving? [Ref. No.: 52]

Part – B1. List out the fundamentals steps of Problem solving algorithm. Discuss briefly about analyze phase of algorithm. [Ref. No.: 2]2. Discuss in detail: Features of the top down design and bottom up design approaches in problem solving. [Ref. No.: 2]3. Enumerate and explain briefly important types of problem. [Ref. No.: 3]4. What is an algorithm? What are the analysis frameworks in measuring the efficiency? Give examples. [Ref. No.: 4]5. Describe briefly about mathematical analysis of non recursive algorithms. [Ref. No.: 6]6. Define Recursive algorithm. Explain the mathematical analysis procedures with Towers of Hanoi puzzle example. [Ref. No.: 7]7. Write an algorithm for finding maximum element of an array perform best, worst and average case complexity with appropriate order notations. [Ref. No.: 8] 8. Explain Graph and Geometric problems in detail. [Ref. No.: 9]9. Discuss about Worst-Case efficiency. [Ref. No.: 10]10. Write short notes on sorting [Ref. No.: 11]