TOC H INSTITUTE OF SCIENCE AND TECHNOLOGY

Size: px
Start display at page:

Download "TOC H INSTITUTE OF SCIENCE AND TECHNOLOGY"

Transcription

1 TOC H INSTITUTE OF SCIENCE AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK& ANSWER KEY CS 1704 ANALYSIS AND DESIGN OF ALGORITHMS (2012 Scheme)

2 SYLLABUS Module I Analyzing Algorithms and problems. Classifying functions by their asymptotic growth rate. Recursive procedures. Recurrence equations - Substitution Method, Changing variables, Recursion Tree, Master Theorem. Design Techniques- Divide and Conquer, Dynamic Programming, Greedy, Backtracking Module II Analysis of searching and sorting. Insertion sort, Quick sort, Merge sort and Heap sort. Binomial Heaps and Fibonacci Heaps, Lower bounds for sorting by comparison of keys. Comparison of sorting algorithms.amortized Time Analysis. Red-Black Trees - Insertion & Deletion. Module III Graphs and graph traversals. Strongly connected components of a Directed graph. Biconnected components of an undirected graph. Transitive closure of a Binary relation. Warshalls algorithm for Transitive closure. All pair shortest path in graphs. Dynamic programming. Constructing optimal binary search trees. Module IV Complexity Theory - Introduction. P and NP. NP-Complete problems. Approximation algorithms. Bin packing,graphcoloring.travelingsalespersonproblem. References: 1. Allen Van Gelder, Sara Baase, "Computer Algorithms - Introduction to Design and Analysis", 3 rd Edition, Pearson ISBN T. H. Cormen, C. E. Lieserson, R. L. Rivest, Introduction to Algorithms, Prentice Hall India, 3/e ISBN AnanyLevitin, "Introduction to the design and analysis of algorithms", Pearson Education,secondedition,ISBN A.V.Aho, J.E.Hopcroft and J.D. Ullman, "The Design and Analysis of Computer Algorithms", Addison Wesley Publishing House, Reading, MA, ISBN E Horowitz and S Sahni, "Fundamentals of Computer Algorithms", ComputerScience Press, Rockville 6.JeffreyH.Kingston, "Algorithms and Data Structures - Design, Correctness andanalysis",addisonwesley,singapore, Knuth, "Art of Computer Programming Vol II, Sorting and Searching,", Prentice Hall Type of Questions for University Exam. Q 1. Eight short answer questions of 5 marks each with two questions from each of the four modules. (8 x5 = 40 marks) Q 2. to Q.5 : Two questions A & B of 15 marks from each modules with option to answer either A or B.(4 x 15 = 60 marks) 2

3 Short Questions (5 marks) MODULE I 1. Explain with example divide and conquer technique. 2. Solve the recurrence equation T(n) = 2T( n)+1 3. Explain the significance of notations o and Ω in the complexity analysis of algorithms. 4. How do we analyze algorithm based on i. Amount of space used. ii. Optimality 5. Briefly explain backtracking technique in algorithm design. 6. What are recursive procedures? How is their complexity expressed? 7. State Master s Theorem. 8. Solve the recurrence relation T(n)=2T(n/2) + n 3 9. Write short note on recurrence equations. What are the different methods to solve recurrence equations? 10. Explain (i) Big Oh (ii) Big Omega (iii) Little Oh (iv) Little Omega (v) Theta 11. What is the use of asymptotic notations? Explain with examples. 12. T(n) = 2T(n/2) + 3n, solve by substitution method. 13. Show that, if c is a positive real number, then g(n)=1+ c + c c n is (i) θ(n) if c=1. (ii) θ(c n ) if c>1 14. Give the definition and explain asymptotic notations. 15. Explain greedy method of solving a problem. 16. Explain divide and conquer method with quick sort as an example. 17. Explain dynamic programming with an example. 18. Explain substitution method for solving recurrence equations. 3

4 Essay Questions (15 marks) 1. a) Explain the different asymptotic notations used for specifying the growth rate of functions.(10) b) Explain the dynamic programming method of solving a problem.(5) 2. Explain the various criteria used for analyzing algorithms with suitable examples. 3. What do you understand by complexity of an algorithm? Explain worst case complexity and average case complexity with a suitable example. 4. What is recurrence relation? Explain with suitable examples, different methods to solve recurrence relation. 5. Explain the significance of recurrence equation. Solve the recurrence equation T (N) = 16T(n/4) + n 6. a) Explain the need for analyzing algorithm. Why time requirement for execution of an algorithm more important than space requirement? b) Which are the basic asymptotic notations used to express complexity of an algorithm? 7. a) State Master theorem. b) Explain Greedy strategy in designing an algorithm. c) What is meant by divide and conquer technique? What are the basic steps involved in this technique. 8. a) Explain the different asymptotic notations used for specifying the growth rate of functions. (10) b) Explain the greedy method of solving a problem.(5) 9. a) Explain the substitution method with example (9) b) Explain the dynamic programming method of solving a problem. (6) 10. a) State Master theorem. What are the conditions in case master method cannot be used to solve recurrence. (7) b) Solve the following recurrence equations: (i) T(n) = 2T(n/4) + n (2) (ii) T(n) = T(n/2) + 1 (2) (iii) T(n) = 2T(n/2) + n log n (4) 11. a) Distinguish between time complexity and space complexity of algorithms. Explain various criteria used for analysis of algorithms. (10) b) Describe the significance of recursion tree. How is it used to analyze algorithms? (5) 12. a) Distinguish between worst case complexity and average case complexity analysis with example of sequential search in unordered array. (7) 4

5 b) Explain divide and conquer method with suitable example. (8) 13. a) Solve the recurrence T(n) = 16T(n/4) + n!. (7) b) Generate all permutations of {1,2,3,4} by backtracking. (8) 14. a) Write a backtracking algorithm to find Hamiltonian circuit in a graph. (8) b) Solve the recurrence T(n) = 2T(n/2) + n/log n. (7) 15. a) Explain various criteria used for analysis of algorithms with suitable examples. (12) b) Solve the recurrence T(n)= 9T(2n/3) +1 by master theorem. (3) 16. a) Explain divide and conquer method with quick sort as an example. Also derive the worst case and average case time complexity (12) b) Solve the recurrence T(n)= 2T(2n/3) +1 by recurrence tree method. (3) 17. a) Explain Recursion tree. (5) b) Explain various asymptotic notations used for specifying the growth rate of functions. (10) 18. Explain the significance of recurrence equation. Solve the recurrence equation T (N) = T(n/4) + n 5

6 Short Question (5 marks) MODULE II 1. Explain any one searching algorithms with an example. 2. What is Amortized time Analysis? 3. Derive an expression to find the worst case complexity of insertion operation in Red Black tree 4. Explain the union operation in Binomial heap. 5. Explain the heap sort procedure with elements 29, 30, 8, 39, Write short note on Red Black Tree. 7. What are the features of binomial heap data structure? Give an example. 8. What are the features of Quick Sort algorithm? Write expression for its worst and average case complexity. 9. Distinguish between binomial heap and Fibonacci heap. 10. Derive the average case complexity of insertion sort procedure. 11. Write the properties of red black tree. Also explain any one operation. 12. Mention properties of binomial heap. Draw binomial heap B4 13. List all the operations supported by Fibonacci heap and its time complexity. 14. What are the properties of red black tree? Write the insertion procedure in red-black tree 15. Explain heap sort procedure. 16. Define a height balanced tree. 6

7 Essay Question (15 marks) 1. Explain quick sort algorithms with an example. Analyzing the worst case, best case and average case behavior of quick sort. 2. a) What are the properties of Red Black Trees? Explain the insertion procedures to a Red Black Tree. (10) b) Explain the union operation in Binomial Heap. (5) 3. Explain any one searching algorithm with an example. Also derive the worst case and average case time complexity. 4. Explain Heap Sort Algorithm. Derive the Worst case and average case time complexity. 5. Write algorithm for insertion sorting method. Analyze its complexity. 6. Explain binary search algorithm as an example of divide and conquer strategy and find its average case complexity. 7. a) Write insertion sort algorithm and analyse its complexity in worst and average case. (10) b) What are the properties of Red Black tree.(5) 9. a) Write the procedure of insertion in Red Black Tree. (7) b) Define amortized time analysis. (8) 8. Explain Merge Sort Algorithm. Derive the Worst case and average case time complexity. 9. a) Write insertion sort algorithm and analyse its complexity in worst and average case. (10) b) Explain amortized time analysis. (5) 10. a) Explain union operation in Fibonacci heap with example. (8) b) Write an algorithm for deletion operation in Red Black tree. Give example. (7) 11. What are the properties of Red Black Trees? Explain the insertion procedures and deletion operation on Red Black Tree with suitable examples. 12. Write the procedure for heap sort. Explain the heap sort procedure with elements 15, 23, 26, 10, 9, 45, 20, 8, a) What are the properties of Fibonacci heap? (3) b) Analyze the complexity of merge sort algorithm. (12) 14. a) Describe the properties of binomial heap. (4) b) What is the need of amortized time analysis? (3) c) Write procedure for heap sort. Give its complexities. (8) 15. Apply a sorting algorithm for binary search in an unsorted array and calculate the complexity for binary search. 7

8 Short Question (5 marks) MODULE III 1. Explain strongly connected component algorithm with an example. 2. Explain transitive closure of a binary relation with an example. 3. Explain graph traversal techniques. 4. Explain one method used to represent graphs. 5. Explain the dynamic programming technique used in algorithm design. 6. What is depth first traversal in a graph? Which data structure is used in depth first traversal? 7. Explain Warshall s algorithm for finding transitive closure. 8. Explain bi-connected component of a graph with the help of an example. 9. Explain BFS in a graph with example. 10. Explain Floyd method to find all pair shortest path in a graph. 11. Explain an algorithm to find transitive closure. 12. Write recursive version for longest commission subsequence problem. 13. The reverse of a directed graph G=(V,E) is another directed graph G R = (V,E R ) on the same vertex set, but with all edges reversed; that is E R ={(v, u): (u, v) ε E). Give a linear time algorithm for computing the reverse of a graph in adjacency list format. 14. Explain divide and conquer method used in algorithm design. 8

9 Essay Question (15 marks) 1. Explain any one algorithm for finding all pair shortest path in graphs. 2. Explain BFS with an example. 3. What is a binary search tree? Explain an algorithm for constructing an optimal binary search tree. Analyze its complexity. 4. a) Explain with an example strongly connected component of a directed graph. (5) b) Explain any one algorithm for finding the all pair shortest path in graph. (10) 5. Write algorithm to find bicomponents of an undirected graph. 6. Explain Warshall s algorithm for finding transitive closure of binary relation. 7. Explain the transitive closure of a binary relation. Write an algorithm to find transitive closure. 8. What are bi connected components of a graph? Explain with an example. What is the physical significance of bi-connected components in a graph? 9. a) Explain transitive closure of binary relation. (5) b) Explain any one algorithm for finding the all pair shortest path in graph. (10) 10. a) Explain with an example strongly connected component of a directed graph. (7) b) Write an algorithm for DFS on a graph. Analyze its complexity. (8) 11. a) Explain any one algorithm for finding the all pair shortest path in graph. (10) b) Explain BFS with example. (5) 12. a) Write an algorithm to find transitive closure of a graph. Give one example Obtain its time complexity. (7) b) Write algorithm to find bicomponents of an undirected graph.give one example Obtain its time complexity. (8) 13. a) Write an algorithm to construct optimal binary search tree. Give one example. (8) b) Write algorithm to find shortest path between every pair of vertices in a graph.give one example. (7) 14. Explain BFS and DFS with examples. 15. a) Compare 2 graph traversal methods. (10) b) Explain dynamic programming. (5) 16. a) Explain strong component algorithm with a suitable example. (10) b) What is optimal binary search tree? (5) 17. What are bi connected components of a graph? Explain with an example. Write algorithm to identify it. 18. What are strongly connected components of a graph? Explain with an example. Write algorithm to find strongly connected components of a graph. 9

10 MODULE IV Short Question (5 marks) 1. Explain the significance of approximation algorithm. 2. Explain graph coloring problem with an example. 3. Distinguish between NP hard and NP complete problems. 4. Explain any one graph coloring algorithm. 5. Give significance of classifying problem set as class P. 6. Write notes about approximation algorithms. 7. Explain the bin packing problem. 8. Explain Graph coloring problem. 9. Define P, NP, NP complete and NP hard problems with examples. 10. Explain P class and NP class problems with examples. 11. Explain optimization problem with examples. 12. Give the significance of classifying problem set as class P. 13. Explain first fit decreasing strategy for solving bin packing problem. 10

11 Essay Question (15 marks) 1. What is a Bin packing problem? Explain the first fit decreasing strategy for solving bin packing problem. 2. Explain various bin packing strategies. Analyze the behavior. 3. Define Travelling salesman problem. Explain the tree possible strategies for TSP. 4. Explain 2 graph coloring algorithms with suitable examples. 5. a) Explain the sequential coloring algorithm for graph coloring problem. (5) b) Explain the first fit decreasing strategy for bin packing problem. (10) 6. a) What do you know by optimization problem? Give an example. (5) b) Explain various strategies applied for TSP problem. (10) 7. Define i) P ii) iii) NP NP complete classes of problem with an example for each. 8. a) Explain the travelling salesperson problem. b) Explain first fit strategy for Bin packing problem 9. a) Explain various strategies applied for travelling salesperson problem. (10) b) What do you know by dynamic programming? (5) 10. a) Explain the Widgerson s algorithm for graph coloring problem. (10) b) Explain approximation of algorithms. (5) 11. a) Distinguish between NP hard and NP complete problem. (5) b) What is a Bin packing problem? Explain the first fit decreasing strategy for solving bin packing problem. (10) 12. a) Write graph coloring algorithm. Give one example. (7) b) Prove that Bin packing problem is NP complete. (8) 13. a) Show that knapsack problem is NP complete. (8) b) Show that P is subset of NP. Give one example. (7) 14. a) Explain various strategies applied for travelling salesperson problem. (8) b) Explain the first fit decreasing strategy for bin packing problem. (7) 15. a) Explain any one method for graph coloring problem. (8) b) Explain approximation algorithms. (7) 11

12 MODULE I 19. Explain with example divide and conquer technique. In the divide and conquer method the large problem is divided into smaller sub problems and the solutions for the smaller problems are found. These solutions are combined to get the solution for the larger problem. The sub problems are ideally of the same size. These sub problems are solved independently. Recursion will be a handy tool in solving such problems. (1mk) The solution for the sub problems are combined to get the solution for the original problem. The divide and conquer strategy can be viewed as one which has three steps. 1. Divide 2. Conquer 3. Combine (1mk) 1. The first step is called Divide : dividing the given problems into smaller sub problems which are identical to the original problem and also these sub problems are of the same size. 2. The second step is called Conquer :where in we solve these sub problems recursively. 3. The third step is called Combine :where in we combine the solutions of the sub problems to get the solution for the original problem 20. Solve the recurrence equation T(n) = 2T( n)+1 Ans: O(log n) 21. Explain the significance of notations o and Ω in the complexity analysis of algorithms. -notation (g(n)) = {f(n) : positive constants c and n 0, such that n n 0, we have 0 cg(n) f(n)} o-notation 12

13 o(g(n)) = {f(n): c> 0, n 0 > 0 such that n n 0, we have0 f(n)<cg(n)}. 22. How do we analyze algorithm based on iii. iv. Amount of space used. Optimality Space Usage The space needed by a program has the following components. 1. Instruction space : space needed to store the object code 2. Data space : space needed to store constants and variables. 3. Environment Stack space : space needed when functions are called Optimality Each problem has inherent (inbuilt) complexity,i.e, there is some minimum amount of work required to solve it. 23. Briefly explain backtracking technique in algorithm design. Backtracking is a technique used to solve problems with a large search space, that systematically tries and eliminates possibilities. A standard example of backtracking would be going through a maze. At some point in a maze, you might have two options of which direction to go: Junction portion A portion B One strategy would be to try going through portion A of the maze. If you get stuck before you find your way out, then you "backtrack" to the junction. At this point in time you know that portion A will NOT lead you out of the maze, so you then start searching in portion B. Clearly, at a single junction you could have even more than two choices. The backtracking strategy says to try each choice, one after the other, if you ever get stuck, "backtrack" to the junction and try the next choice. If you try all choices and never found a way out, then there is no solution to the maze. 24. What are recursive procedures? How is their complexity expressed? Any problem can be solved either by writing recursive algorithm or by writing nonrecursive algorithm. A recursive algorithm is one which makes a recursive call to itself with smaller inputs. We often use a recurrence relation to describe the running time of a recursive algorithm. 13

14 A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs or as a function of preceding (or lower) terms. Like all recursive functions, a recurrence also consists of two steps: 1. Basic step: Here we have one or more constant values which are used to terminate recurrence. It is also known as initial conditions or base conditions. 2. Recursive steps: This step is used to find new terms from the existing (preceding) terms. Thus in this step the recurrence compute next sequence from the k preceding values. This formula is called a recurrence relation (or recursive formula). This formula refers to itself, and the argument of the formula must be on smaller values (close to the base value). Hence a recurrence has one or more initial conditions and a recursive formula, known as recurrence relation. A Recurrence Relation is any equation for a function T, where T appears on both the left and right sides of the equation. We always want to solve these recurrence relation by get- ting an equation for T, where T appears on just the left side of the equation. T(n) = T(n/2) + 1 is an example of a recurrence relation Whenever we analyze the run time of a recursive algorithm, we will first get a recurrence relation. To get the actual run time, we need to solve the recurrence relation 25. State Master s Theorem. The Master Theorem applies to recurrences of the following form: T(n) = at(n/b) + f(n) where a 1 and b > 1 are constants and f(n) is an asymptotically positive function. There are 3 cases: 1. If f(n) = O(nlogb a ) for some constant > 0, then T(n) = Θ(nlogb a). 2. If f(n) = Θ(nlogb a logk n) with1 k 0, then T(n) = Θ(nlogb a logk+1 n). 3. If f(n) = Ω(nlogb a+) with > 0, and f(n) satisfies the regularity condition, then T(n) = Θ(f(n)). Regularity condition: af(n/b) cf(n) for some constant c < 1 and all sufficiently large n. 26. Write short note on recurrence equations. What are the different methods to solve recurrence equations? Any problem can be solved either by writing recursive algorithm or by writing nonrecursive algorithm. A recursive algorithm is one which makes a recursive call to itself with smaller inputs. We often use a recurrence relation to describe the running time of a recursive algorithm. A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs or as a function of preceding (or lower) terms. Like all recursive functions, a recurrence also consists of two steps: 14

15 1. Basic step: Here we have one or more constant values which are used to terminate recurrence. It is also known as initial conditions or base conditions. 2. Recursive steps: This step is used to find new terms from the existing (preceding) terms. Thus in this step the recurrence compute next sequence from the k preceding values. This formula is called a recurrence relation (or recursive formula). This formula refers to itself, and the argument of the formula must be on smaller values (close to the base value). Hence a recurrence has one or more initial conditions and a recursive formula, known as recurrence relation. A Recurrence Relation is any equation for a function T, where T appears on both the left and right sides of the equation. We always want to solve these recurrence relation by get- ting an equation for T, where T appears on just the left side of the equation. T(n) = T(n/2) + 1 is an example of a recurrence relation Various methods of solving the recurrence equation: 1. The Substitution Method (Guess the solution & verify by Induction) 2. Iteration Method (unrolling and summing) 3. The Recursion-tree method 4. Master method 5. Changing Variable Method 27. Explain (i) Big Oh (ii) Big Omega (iii) Little Oh (iv) Little Omega (v) Theta Asymptotic notations The 3 basic asymptotic notations are Big Oh, Big Theta, Big Omega -notation (g(n)) = {f(n) : positive constants c 1, c 2, and n 0, such that n n 0, we have 0 c 1 g(n) f(n) c 2 g(n)} O-notation O(g(n)) = {f(n) : positive constants c and n 0, such that n n 0, we have 0 f(n) cg(n) } -notation (g(n)) = {f(n) : positive constants c and n 0, such that n n 0, we have 0 cg(n) f(n)} o-notation o(g(n)) = {f(n): c> 0, n 0 > 0 such that n n 0, we have0 f(n)<cg(n)}. w notation 15

16 w(g(n)) = {f(n): c> 0, n 0 > 0 such that n n 0, we have0 cg(n) <f(n)}. 28. What is the use of asymptotic notations? Explain with examples. The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbersn D f0; 1; 2; : : :g. Such notations are convenient for describing the worst-caserunning-time function T.n/, which usually is defined only on integer input sizes. We sometimes find it convenient, however, to abuse asymptotic notation in a variety of ways. We will use asymptotic notation primarily to describe the running times of algorithms, as when we wrote that insertion sort s worstcase running time is.n 2. Asymptotic notation actually applies to functions. These notations are used to describe the Running time of an algorithm, in terms of functions, whose domains are the set of natural numbers, N = {1, 2, }. Such notations are convenient for describing the worst case running time function. T(n) (problem size input size). The complexity function can be also be used to compare two algorithms P and Q that perform the same task. The basic Asymptotic Notations are: 1. O(Big- Oh ) Notation. [Maximum number of steps to solve a problem, (upper bound)] 2. Ω (Big- Omega ) Notation [Minimum number of steps to solve a problem, (lower bound)] 3. θ(theta) Notation [Average number of steps to solve a problem, (used to express both upper and lower bound of a given f(n)). 29. Explain greedy method of solving a problem. Greedy algorithms are usually designed to solve optimization problems in which a quantity is to be minimized or maximized. However, unlike dynamic programming algorithms, greedy algorithms typically consist of an iterative procedure that tries to find a local optimal solution. In some instances, these local optimal solutions translate to global optimal solutions. In others, they fail to give optimal solutions. A greedy algorithm makes a correct guess on the basis of little calculation without worrying about the future. Thus, it builds a solution step by step. Each step increases the size of the partial solution and is based on local optimization. The choice made is that which produces the largest immediate gain while maintaining feasibility. Since each step consists of little work based on a small amount of information, the resulting algorithms are typically efficient. The hard part in the design of a greedy algorithm is proving that the algorithm does indeed solve the problem it is designed for. Eg: A scheduling problem. Consider a set of activities 1, 2,..., n. Activity istarts at time siand finishes at time fi >si. Two activities iand j overlap if [si, fi] 16

17 [sj,fj]. The objective is to select a maximum number of pairwise non-overlapping activities. 30. Explain dynamic programming with an example. Dynamic Programming Dynamic programming is a technique for solving problems with overlapping subproblems. Typically, these subproblems arise from a recurrence relating a solution to a given problem with solutions to its smaller subproblems of the same type. Rather than solving overlapping subproblems again and again, dynamic programming suggests solving each of the smaller subproblems only once and recording the results in a table from which we can then obtain a solution to the original problem. E.g. Fibonacci Numbers 0,1,1,2,3,5,8,13,21,34,..., which can be defined by the simple recurrence F(0) = 0, F(1)=1. and two initial conditions F(n) = F(n-1) + F(n-2) for n Explain substitution method for solving recurrence equations. A substitution method is one, in which we guess a bound and then use mathematical induction to prove our guess correct. It is basically two step process: Step1: Guess the form of the Solution. Step2: Prove your guess is correct by using Mathematical Induction 17

18 18

19 32. a) Explain the different asymptotic notations used for specifying the growth rate of functions.(10) The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbersn D f0; 1; 2; : : :g. Such notations are convenient for describing the worst-caserunning-time function T.n/, which usually is defined only on integer input sizes. We sometimes find it convenient, however, to abuse asymptotic notation in a variety of ways. We will use asymptotic notation primarily to describe the running times of algorithms, as when we wrote that insertion sort s worstcase running time is.n 2. Asymptotic notation actually applies to functions. These notations are used to describe the Running time of an algorithm, in terms of functions, whose domains are the set of natural numbers, N = {1, 2, }. Such notations are convenient for describing the worst case running time function. T(n) (problem size input size). The complexity function can be also be used to compare two algorithms P and Q that perform the same task. The basic Asymptotic Notations are: 1. O(Big- Oh ) Notation. [Maximum number of steps to solve a problem, (upper bound)] 2. Ω (Big- Omega ) Notation [Minimum number of steps to solve a problem, (lower bound)] 3. θ(theta) Notation [Average number of steps to solve a problem, (used to express both upper and lower bound of a given f(n)). THE NOTATION O (BIG Oh ) Big Oh Notation is used to express an asymptotic upper bound (maximum steps) for a given function f(n). Let f(n) and g(n) are two positive functions, each from the set of natural numbers (domain) to the positive real numbers. We say that the function f(n)= O(g(n)), if there exist two positive constants C and n0 such that 0 f(n) cg(n): n n0 THE NOTATION Ω (BIG Omega ) Big Omega Notation is used to express an asymptotic lower bound (minimum steps) for a given function f(n). Let f(n) and g(n) are two positive functions, each from the set of natural numbers (domain) to the positive real 19

20 numbers. We say that the function f(n)= O(g(n)), if there exist two positive constants C and n0 such that f(n) cg(n): n n0 Theta Notation (Θ) Θ(g(n)) = {f(n) : there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2g(n) for all n n0 } If f(n) = Θ(g(n)), all values of n right to n0 f(n) lies on or above c1g(n) and on or below c2g(n). Hence it is asymptotic tight bound for f(n). o-notation o(g(n)) = {f(n): c> 0, n 0 > 0 such that n n 0, we have0 f(n)<cg(n)}. w notation w(g(n)) = {f(n): c> 0, n 0 > 0 such that n n 0, we have0 cg(n) <f(n)}. 20

21 33. Explain the various criteria used for analyzing algorithms with suitable examples. (a) Different criteria for analyzing algorithms are (1mk) i. Correctness ii. Amount of work done iii. Average and worst case analysis iv. Space usage v. Simplicity vi. Optimality 1. Correctness (1mk) There are three major steps involved in establishing the correctness of an algorithm. 1. Determine whether an algorithm is correct : given inputs and what result it is to produce for each input. 2. Once the method is established, we implement it in a program. 3. Most programs are very large and very complex. To prove the correctness of a large program, we can try to break the program into smaller modules. 2. Amount of work done (1mk) How we measure the amount of work done by an Algorithm? Comparing two algorithms for the same problem, so that we can determine one is more efficient than the other. 3. Average and worst-case Analysis (1mk) The analysis of the algorithm can be performed based on the nature of the problem. Thus we have Worst-case analysis: Under what condition does the algorithm when executed consumes maximum amount of resources. Best case analysis : Under what condition does the algorithm when executed consumes minimum amount of resources. Average case analysis: This is between worst-case and best case. 4. Space Usage (1mk) The space needed by a program has the following components. 2. Instruction space : space needed to store the object code 2. Data space : space needed to store constants and variables. 3. Environment Stack space : space needed when functions are called 5.Simplicity (1mk) It may make verifying the correctness of the algorithm easier, and it makes writing, debugging and modifying a program easier. 6. Optimality (1mk) Each problem has inherent (inbuilt) complexity,i.e, there is some minimum amount of work required to solve it. 34. What do you understand by complexity of an algorithm? Explain worst case complexity and average case complexity with a suitable example. 21

22 An algorithm is a sequence of unambiguous instruction for solving a problem, for obtaining a required output for any legitimate input in a finite amount of time. The complexity of an algorithm M is the function f(n), which give the running time and/or storage space requirement of the algorithm in terms of the size n of the input data. Frequently, the storage space required by an algorithm is simply a multiple of the data size n. In general the term complexity given anywhere simply refers to the running time of the algorithm. There are 3 cases, in general, to find the complexity function f(n): 1. Best case: The minimum value of for any possible input. 2. Worst case: The maximum value of for any possible input. 3. Average case: The value of which is in between maximum and minimum for any possible input. Generally the Average case implies the expected value of To understand the Best, Worst and Average cases of an algorithm, consider a linear array, where the array A contains n-elements. Students may you are having some problem in understanding. Suppose you want either to find the location LOC of a given element (say ) in the given array A or to send some message, such as LOC=0, to indicate that does not appear in A. Here the linear search algorithm solves this problem by comparing given, one-by-one, with each element in A. That is, we compare with A[1], then A[2], and so on, until we find LOC such that. 22

23 23

24 35. What is recurrence relation? Explain with suitable examples, different methods to solve recurrence relation. Any problem can be solved either by writing recursive algorithm or by writing nonrecursive algorithm. A recursive algorithm is one which makes a recursive call to itself with smaller inputs. We often use a recurrence relation to describe the running time of a recursive algorithm. A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs or as a function of preceding (or lower) terms. Like all recursive functions, a recurrence also consists of two steps: 1. Basic step: Here we have one or more constant values which are used to terminate recurrence. It is also known as initial conditions or base conditions. 2. Recursive steps: This step is used to find new terms from the existing (preceding) terms. Thus in this step the recurrence compute next sequence from the k preceding values. This formula is called a recurrence relation (or recursive formula). This formula refers to itself, and the argument of the formula must be on smaller values (close to the base value). Hence a recurrence has one or more initial conditions and a recursive formula, known as recurrence relation. A Recurrence Relation is any equation for a function T, where T appears on both the left and right sides of the equation. We always want to solve these recurrence relation by get- ting an equation for T, where T appears on just the left side of the equation. T(n) = T(n/2) + 1 is an example of a recurrence relation Various methods of solving the recurrence equation: 1. The Substitution Method (Guess the solution & verify by Induction) 2. Iteration Method (unrolling and summing) 3. The Recursion-tree method 4. Master method 5. Changing Variable Method 36. Explain the significance of recurrence equation. Any problem can be solved either by writing recursive algorithm or by writing non-recursive algorithm. A recursive algorithm is one which makes a recursive call to itself with smaller inputs. We often use a recurrence relation to describe the running time of a recursive algorithm. A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs or as a function of preceding (or lower) terms. Like all recursive functions, a recurrence also consists of two steps: 24

25 1. Basic step: Here we have one or more constant values which are used to terminate recurrence. It is also known as initial conditions or base conditions. 2. Recursive steps: This step is used to find new terms from the existing (preceding) terms. Thus in this step the recurrence compute next sequence from the k preceding values. This formula is called a recurrence relation (or recursive formula). This formula refers to itself, and the argument of the formula must be on smaller values (close to the base value). Hence a recurrence has one or more initial conditions and a recursive formula, known as recurrence relation. A Recurrence Relation is any equation for a function T, where T appears on both the left and right sides of the equation. We always want to solve these recurrence relation by get- ting an equation for T, where T appears on just the left side of the equation. T(n) = T(n/2) + 1 is an example of a recurrence relation 37. a) Explain the need for analyzing algorithm. Why time requirement for execution of an algorithm more important than space requirement? Analysis of algorithm is a field in computer science whose overall goal is an understanding of the complexity of algorithms (in terms of time Complexity), also known as execution time & storage (or space) requirement taken by that algorithm. Suppose M is an algorithm, and suppose n is the size of the input data. The time and space used by the algorithm M are the two main measures for the efficiency of M. The time is measured by counting the number of key operations, for example, in case of sorting and searching algorithms, the number of comparisons is the number of key operations. That is because key operations are so defined that the time for the other operations is much less than or at most proportional to the time for the key operations. The space is measured by counting the maximum of memory needed by the algorithm. The complexity of an algorithm M is the function f(n), which give the running time and/or storage space requirement of the algorithm in terms of the size n of the input data. Frequently, the storage space required by an algorithm is simply a multiple of the data size n. In general the term complexity given anywhere simply refers to the running time of the algorithm. There are 3 cases, in general, to find the complexity function f(n): 1. Best case: The minimum value of for any possible input. 2. Worst case: The maximum value of for any possible input. 3. Average case: The value of which is in between maximum and minimum for any possible input. Generally the Average case implies the expected value of Time complexity specifies how the running time depends on the size of the input. Its purpose is To estimate how long a program will run. To estimate the largest input that can reasonably be given to the program. To compare the efficiency of different algorithms. 25

26 To help focus on the parts of code that are executed the largest number of times. To choose an algorithm for an application. Space complexity is the amount of memory required by an algorithm to run to completion.some algorithms may be more efficient if data completely loaded into memory. The size required to store certain data/variables, that is independent of the size of the problem.space needed by variables, whose size is dependent on the size of the problem. S(P) = c + S(instance characteristics) void float sum (float* a, int n) { float s = 0; for(inti = 0; i<n; i++) { s+ = a[i]; } return s; } Space: one word for n, one for a [passed by reference], one for i(constant space) 38. a) Distinguish between time complexity and space complexity of algorithms. Time complexity specifies how the running time depends on the size of the input. Its purpose is To estimate how long a program will run. To estimate the largest input that can reasonably be given to the program. To compare the efficiency of different algorithms. To help focus on the parts of code that are executed the largest number of times. To choose an algorithm for an application. Space complexity is the amount of memory required by an algorithm to run to completion.some algorithms may be more efficient if data completely loaded into memory. The size required to store certain data/variables, that is independent of the size of the problem.space needed by variables, whose size is dependent on the size of the problem. S(P) = c + S(instance characteristics) void float sum (float* a, int n) { float s = 0; for(inti = 0; i<n; i++) { s+ = a[i]; } return s; } Space: one word for n, one for a [passed by reference], one for i(constant space) 26

27 39. Describe the significance of recursion tree. How is it used to analyze algorithms? (5) The recursion-tree method converts the recurrence into a tree whose nodesrepresent the costs incurred at various levels of the recursion. In a recursion tree, each node represents the cost of a single subproblemsomewhere in the set of recursive function invocations. We sum the costs withineach level of the tree to obtain a set of per-level costs, and then we sum all theper-level costs to determine the total cost of all levels of the recursion. A recursion tree is best used to generate a good guess. You can use a recursion tree as a direct proof of asolution to a recurrence. (2mks) Eg: T(n) = 3T(n/4)+cn 2 (1mk) 40. a) Distinguish between worst case complexity and average case complexity analysis with example of sequential search in unordered array. (7) An algorithm is a sequence of unambiguous instruction for solving a problem, for obtaining a required output for any legitimate input in a finite amount of time. The complexity of an algorithm M is the function f(n), which give the running time and/or storage space requirement of the algorithm in terms of the size n of the input data. Frequently, the storage space required by an algorithm is simply a multiple of the data size n. In general the term complexity given anywhere simply refers to the running time of the algorithm. There are 3 cases, in general, to find the complexity function f(n): 1. Best case: The minimum value of for any possible input. 2. Worst case: The maximum value of for any possible input. 27

28 3. Average case: The value of which is in between maximum and minimum for any possible input. Generally the Average case implies the expected value of To understand the Best, Worst and Average cases of an algorithm, consider a linear array, where the array A contains n-elements. Students may you are having some problem in understanding. Suppose you want either to find the location LOC of a given element (say ) in the given array A or to send some message, such as LOC=0, to indicate that does not appear in A. Here the linear search algorithm solves this problem by comparing given, one-by-one, with each element in A. That is, we compare with A[1], then A[2], and so on, until we find LOC such that. 28

29 29

30 MODULE II 17. Explain any one searching algorithms with an example. The binary search algorithm is one of the most efficient searching techniques which require the list to be sorted in ascending order. To search for an element in the list, the binary search algorithms split the list and locate the middle element of the list. The middle of the list is calculated as Middle:=(l+r) div 2 n number of element in list The algorithm works by comparing a search key element k with the array middle element a[m] After comparison, any one of the following three conditions occur. 1. If the search key element k is greater than a[m],then the search element is only in the upper or second half and eliminate the element present in the lower half.now the value of l is middle m If the search key element k is less than a[m], then the search element is only in the lower or first half. No need to check in the upper half. Now the value of r is middle m If the search key element k is equal to a[m], then the search key element is found in the position m. Hence the search operation is complete. 18. What is Amortized time Analysis? Provides average cost of each operation in the worst case for successive operations Aggregate method (2.5mks) a. show for a sequence of n operations takes worst-case time T(n) in total b. In the worst case, the average cost, or amortized cost, per operation is therefore T(n)/n Accounting method // spreading a large cost over time (2.5mks) amortized cost = actual cost + accounting cost assign different accounting cost to different operations the sum of accounting costs is nonnegative for any legal sequence of operations to make sure it is feasible to analyze the amortized cost of each operation 19. Derive an expression to find the worst case complexity of insertion operation in Red Black tree A search-tree data structure for which a height of O(lg n) is guaranteed when implementing a dynamic set of n items. This data structure requires an extra one- bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root and leaves (NIL s) are 30

31 black. 3. If a node is red, then its parent is black.4.all simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). Running time of insertion in red black tree iso(lg n) with O(1) rotations. 20. Explain the union operation in Binomial heap. The operation of uniting two binomial heaps is used as a subroutine by most of the remaining operations. The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have the same degree. The following procedure links the Bk 1 tree rooted at node y to the Bk 1 tree rooted at node z; that is, it makesz the parent of y. Node z thus becomes the root of a Bk tree. BINOMIAL-LINK(y, z) 1 p[y] z 2 sibling[y] child[z] 3 child[z] y 4 degree[z] degree[z]+1 Initially, there are at most two roots of the same degree Binomial-heap-merge guarantees that if two roots in h have the same degree they are adjacent in the root list During the execution of union, there may be three roots of the same degree appearing on the root list at some time CASE 1: Occurs when degree [x] degree [next-x] CASE 2: Occurs when x is the first of 3 roots ofequal degree degree [x] = degree [next-x] = degree [sibling[next-x]] CASE 3 & 4: Occur when x is the first of 2 roots of equal degree degree [x] = degree [next-x] degree [sibling [next-x]] Occur on the next iteration after any case Always occur immediately following CASE 2 Two cases are distinguished by whether x or next-x has the smaller key The root with the smaller key becomes the root of the linked tree 21. Write short note on Red Black Tree. A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Red black is a balanced search tree.(1mk) This data structure requires an extra one-bit color field in each node. Red-black properties: 31

32 1. Every node is either red or black. 2. The root and leaves (NIL s) are black. 3. If a node is red, then its parent is black. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). 22. What are the features of binomial heap data structure? Give an example. A BINOMIAL HEAP H is a set of BINOMIALTREES that satisfies the following BinomialHeap Properties 1. Each binomial tree in H isheap-ordered the key of a node is the key of the parent Root of each binomial tree in H contains the smallest key in that tree. 2. There is at most one binomial tree in H whose root has a given degree, n-node binomial heap H consists of at most [lgn] + 1 binomial trees. Binary represantation of n has lg(n) + 1 bits, n b lgn, b lgn-1,...b 1, b 0 > = Σ lgn i=0b i 2 i By property 1 of the lemma (B i contains 2 i nodes) B i appears in H iff bit b i =1 Example: A binomial heap with n=13 nodes =< 1, 1, 0, 1> 2 Consists of B 0, B 2, B What are the features of Quick Sort algorithm? Write expression for its worst and average case complexity. Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out. The steps of Quicksort are as follows: Divide: Rearrange the elements and split the array into two subarrays and an element in between such that so that each element in the left subarray is less than or equal the middle element and each element in the right subarray is greater than the middle element. Conquer: Recursively sort the two subarrays. Given a subarraya[p :: r] such that p r -1, this subroutine rearranges the input subarray into two subarrays, A[p :: q -1] and A[q +1 :: r], so that each element in A[p :: q - 1] is less than or equal to A[q] and each element in A[q +1 :: r] is greater than or equal to A[q] 32

33 Then the subroutine outputs the value of q.use the initial value of A[r] as the \pivot," inthe sense that the keys are compared againstit. Scan the keys A[p :: r - 1] from left to rightand push to the left all the keys that aregreater than or equal to the pivot. Analysis of Quick sort Best case running time: O(n log 2 n) Worst case running time: O(n 2 ) Average case running time: O(n log 2 n) 24. Distinguish between binomial heap and Fibonacci heap. 25. Derive the average case complexity of insertion sort procedure. (a) Insertion sort. Algorithm Insertion-Sort(A) 1 fori= 2 to length(a) 2 j = i 3 while j >1 and A[j 1] > A[j] 4 swap A[j] and A[j 1] 5 j = j 1 Worst case Complexity =θ(n 2 ). Averaget case Complexity =θ(n 2 ). 33

34 Complexity n-1 iterations (stages) i = 1; 2; : : : ; n - 1; j; 1 j i, comparisons and j or j - 1 moves per stage. Insertion sort is correct, since the head sublist is always sorted, and eventually expands to include all elements of A. The worst-case complexity is θ(n 2 ). The worst-case inputs A consist of distinct items in reverse sorted order: a[0] > a[1] > : : : > a[n -1]. _ The total worst-case number of comparisons is : : : + n- 1 = (n-1)n/2 = θ(n 2 ). 26. Write the properties of red black tree. Also explain any one operation. A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Red black is a balanced search tree. This data structure requires an extra one-bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root and leaves (NIL s) are black. 3. If a node is red, then its parent is black. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). 34

35 27. List all the operations supported by Fibonacci heap and its time complexity. a. Finding Minimum Key b. Union of 2 heaps c. Insertion d. Deletion of Minimum e. Decrease key f. Deletion 28. What are the properties of red black tree? Write the insertion procedure in redblack tree A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Red black is a balanced search tree.(1mk) This data structure requires an extra one-bit color field in each node. Red-black properties: 35

36 Every node is either red or black. The root and leaves (NIL s) are black. If a node is red, then its parent is black. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). 29. Explain heap sort procedure. Heapsort also introduces another algorithm design technique: the use of a data structure, in this case one we call a "heap," to manage information during the execution of the algorithm. Not only is the heap data structure useful for heapsort, it also makes an efficient priority queue. The (binary) heap data structure is an array object that can be viewed as a complete binary tree (see Section 5.5.3), as shown in Figure 7.1. Each node of the tree corresponds to an element of the array that stores the value in the node. The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. An array A that represents a heap is an object with two attributes:length[a], which is the number of elements in the array, and heap-size[a], the number of elements in the heap stored within array A. That is, although A[1.. length[a]] may contain valid numbers, no element past A[heap-size[A]], where heap-size[a] [length[a], is an element of the heap. The root of the tree is A[1], and given the index i of a node, the indices of its parent PARENT(i), left child LEFT(i), and right child RIGHT(i) can be computed simply: 36

37 The heapsort algorithm starts by using BUILD-HEAP to build a heap on the input array A[1.. n], where n = length[a]. Since the maximum element of the array is stored at the root A[1], it can be put into its correct final position by exchanging it with A[n]. If we now "discard" node n from the heap (by decrementing heap-size[a]), we observe that A[1.. (n - 1)] can easily be made into a heap. The children of the root remain heaps, but the new root element may violate the heap property (7.1). All that is needed to restore the heap property, however, is one call to HEAPIFY(A, 1), which leaves a heap in A[1.. (n - 1)]. The heapsort algorithm then repeats this process for the heap of size n - 1 down to a heap of size 2. HEAPSORT(A) 1 BUILD-HEAP(A) 2 fori length[a] downto 2 3 do exchange A[1] A[i] 4 heap-size[a] heap-size[a] -1 5 HEAPIFY(A, 1) 30. Define a height balanced tree. AVL trees are used for performing search operations on high dimension external data storage. For example, a phone call list may generate a huge database which may be recorded only on external hard drives, hard-disks or other storage devices. AVL is a data structure that allows storing data such that it may be used efficiently regarding the operations that may be performed and the resources that are needed. AVL trees are binary search trees, wich have the balance propriety. The balance property is true for any node and it states: the height of the left subtree of any node differs from the height of the right subtree by 1. The structure of the nodes of a balanced tree can be represented like: structnodeavl{ int key; intech; nod *left, *right; }; Where: - key represents the tag of the node(integer number), - ech represents the balancing factor - left and right represent pointers to the left and right children. Here are some important notions: [1] Thelenght of the longest road from the root node to one of the terminal nodes is what we call the height of a tree. [2] The difference between the height of the right subtree and the height of the left subtree is what we call the balancing factor. [3] The binary tree is balanced when all the balancing factors of all the nodes are -1,0,+1. Formally, we can translate this to this: hd hs 1, node X being any node in the tree, where hs and hd represent the heigts of the left and the right subtrees 31. Explain quick sort algorithms with an example. Analyzing the worst case, best case and average case behavior of quick sort. Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out. The steps of Quicksort are as follows: 37

38 Divide: Rearrange the elements and split thearray into two subarrays and an element in between such that so that eachelement in the left subarray is less than orequal the middle element and eachelement in the right subarray is greaterthan the middle element. Conquer: Recursively sort the two subarrays. Given a subarraya[p :: r] such that p r -1,this subroutine rearranges the input subarray into two subarrays, A[p :: q -1] anda[q +1 :: r], so that each element in A[p :: q - 1] is less than or equal to A[q] and each element in A[q +1 :: r] is greater than or equal to A[q] Then the subroutine outputs the value of q.use the initial value of A[r] as the \pivot," inthe sense that the keys are compared againstit. Scan the keys A[p :: r - 1] from left to rightand push to the left all the keys that aregreater than or equal to the pivot. (2mks) Algorithm Quicksort(A; n) 1: Quicksort0(A; 1; n) Quicksort(A; p; r) 1: if p r then return 2: q = Partition(A; p; r) 3: Quicksort(A; p; q - 1) 4: Quicksort(A; q +1; r) Partition(A; p; r) 1: x = A[r] 2: i = p -1 3: for j = p to r - 1 do 4: if A[j] x then f 5: i = i+1 6: Exchange A[i] and A[j] 7: Exchange A[i+1] and A[r] 8: return i+1 Example 38

39 Analysis of Quick sort Best case running time: O(n log 2 n) Worst case running time: O(n 2 ) Average case running time: O(n log 2 n) 32. a) What are the properties of Red Black Trees? Explain the insertion procedures to a Red Black Tree. (10) A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Red black is a balanced search tree.(1mk) This data structure requires an extra one-bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root and leaves (NIL s) are black. 3. If a node is red, then its parent is black. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). 39

40 c) Explain the union operation in Binomial Heap. (5) The operation of uniting two binomial heaps is used as a subroutine by most of the remaining operations. The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have the same degree. The following procedure links the Bk 1 tree rooted at node y to the Bk 1 tree rooted at node z; that is, it makesz the parent of y. Node z thus becomes the root of a Bk tree. BINOMIAL-LINK(y, z) 1 p[y] z 2 sibling[y] child[z] 40

41 3 child[z] y 4 degree[z] degree[z]+1 Initially, there are at most two roots of the same degree Binomial-heap-merge guarantees that if two roots in h have the same degree they are adjacent in the root list During the execution of union, there may be three roots of the same degree appearing on the root list at some time CASE 1: Occurs when degree [x] degree [next-x] CASE 2: Occurs when x is the first of 3 roots ofequal degree degree [x] = degree [next-x] = degree [sibling[next-x]] CASE 3 & 4: Occur when x is the first of 2 roots of equal degree degree [x] = degree [next-x] degree [sibling [next-x]] Occur on the next iteration after any case Always occur immediately following CASE 2 Two cases are distinguished by whether x or next-x has the smaller key The root with the smaller key becomes the root of the linked tree 33. Explain any one searching algorithm with an example. Also derive the worst case and average case time complexity. The binary search algorithm is one of the most efficient searching techniques which require the list to be sorted in ascending order. To search for an element in the list, the binary search algorithms split the list and locate the middle element of the list. The middle of the list is calculated as Middle:=(l+r) div 2 n number of element in list The algorithm works by comparing a search key element k with the array middle element a[m] After comparison, any one of the following three conditions occur. 1. If the search key element k is greater than a[m],then the search element is only in the upper or second half and eliminate the element present in the lower half.now the value of l is middle m If the search key element k is less than a[m], then the search element is only in the lower or first half. No need to check in the upper half. Now the value of r is middle m If the search key element k is equal to a[m], then the search key element is found in the position m. Hence the search operation is complete. 41

42 42

43 34. Explain Heap Sort Algorithm. Derive the Worst case and average case time complexity. 43

44 44

45 35. Write algorithm for insertion sorting method. Analyze its complexity. Insertion sort. Algorithm Insertion-Sort(A) 1 fori= 2 to length(a) 2 j = i 3 while j >1 and A[j 1] > A[j] 4 swap A[j] and A[j 1] 5 j = j 1 Worst case Complexity =θ(n 2 ). Averaget case Complexity =θ(n 2 ). Complexity n-1 iterations (stages) i = 1; 2; : : : ; n - 1; 45

46 j; 1 j i, comparisons and j or j - 1 moves per stage. Insertion sort is correct, since the head sublist is always sorted, and eventually expands to include all elements of A. The worst-case complexity is θ(n 2 ). The worst-case inputs A consist of distinct items in reverse sorted order: a[0] > a[1] > : : : > a[n -1]. _ The total worst-case number of comparisons is : : : + n- 1 = (n-1)n/2 = θ(n 2 ). 36. Write an algorithm for deletion operation in Red Black tree. Give example. 46

47 37. a) What are the properties of Fibonacci heap? (3) A Fibonacci heap is a collection of heap-ordered trees. Ideally, we would like it to be a col- lection of binomial trees, but we need more flexibility. It will be important to understand how exactly the nodes of a Fibonacci heap are connected by pointers. Siblings are or- ganized in doubly-linked cyclic lists, and each node has a pointer to its parent and a pointer to one of its children b) Analyze the complexity of merge sort algorithm. (12) Merge sort is a perfect example of a successful application of the divide-and conquer technique. It sorts a given array Al O.. n - 1) by dividing it into two halves A [O n/2-1] and A[ n/2..n - 1], sorting each of them recursively, and then merging the two smaller sorted arrays into a single sorted one. 47

48 STEPS TO BE FOLLOWED The first step of the merge sort is to chop the list into two. If the list has even length, split the list into two equal sub lists. If the list has odd length, divide the list into two by making the first sub list one entry greater than the second sub list. then split both the sub list into two and go on until each of the sub lists are of size one. finally, start merging the individual sub lists to obtain a sorted list. The operation of the algorithm for the array of elements 8,3,2,9,7,1,5,4 is explained as follows, 48

49 The merging of two sorted arrays can be done as follows. Two pointers (array indices) are initialized to point to the first elements of the arrays being merged. Then the elements pointed to are compared and the smaller of them is added to a new array or list being constructed. Then the index of the smaller element is incremented to point to its immediate successor in the array. This operation is continued until one of the two given arrays is exhausted Then the remaining elements of the other array are copied to the end of the new array. 49

50 EFFICIENCY OF MERGE SORT The recurrence relation for the number of key comparison C (n) is C (n) = 2 C (n/2) + Cmerge (n) for n>1, C (1) = 0 Assume, n is a power of 2. Cmerge (n) is the number of key comparison performed during the merge sort. In the merging of two sorted array after one comparison is made the total number of elements in the two array still to be processed and sorted is reduced by one. Hence in the worst case neither of the two arrays becomes empty before the other one contains just one element. Therefore, for the worst case, C worst(n)=2 C worst(n / 2)+n-1 for n > 1, C worst(1)=0 Cmerge(n) = n 1 and we have the recurrence Hence, according to the Master Theorem, C worst (n) (n log n). In fact, it is easy to find the exact solution to the worst-case recurrence for n = 2k C worst(n) = n log2 n - n Apply a sorting algorithm for binary search in an unsorted array and calculate the complexity for binary search. Algorithm Insertion-Sort(A) 1 fori= 2 to length(a) 2 j = i 3 while j >1 and A[j 1] > A[j] 4 swap A[j] and A[j 1] 5 j = j 1 Worst case Complexity =θ(n 2 ). Averaget case Complexity =θ(n 2 ). 50

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Module 1 OBJECTIVE: Algorithms play the central role in both the science and the practice of computing. There are compelling reasons to study algorithms.

More information

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency

More information

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

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

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

O(n): printing a list of n items to the screen, looking at each item once. UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))

More information

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

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

END-TERM EXAMINATION

END-TERM EXAMINATION (Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt

More information

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I PART A(2MARKS) 1. What is an algorithm? 2. What is meant by open hashing? 3. Define Ω-notation 4.Define order of an algorithm. 5. Define O-notation

More information

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

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION 1. What is performance measurement? 2. What is an algorithm? 3. How the algorithm is good? 4. What are the

More information

UNIT 1 BASICS OF AN ALGORITHM

UNIT 1 BASICS OF AN ALGORITHM UNIT 1 BASICS OF AN ALGORITHM Basics of an Algorithm Structure Page Nos. 1.0 Introduction 5 1.1 Objectives 6 1.2. Analysis and Complexity of Algorithms 6 1.3 Basic Technique for Design of Efficient Algorithms

More information

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

Your favorite blog :  (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY Course Code : BCS-042 Course Title : Introduction to Algorithm Design Assignment Number : BCA(IV)-042/Assign/14-15 Maximum Marks : 80 Weightage : 25% Last Date of Submission : 15th October, 2014 (For July

More information

Introduction to Data Structure

Introduction to Data Structure Introduction to Data Structure CONTENTS 1.1 Basic Terminology 1. Elementary data structure organization 2. Classification of data structure 1.2 Operations on data structures 1.3 Different Approaches to

More information

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS 2 marks UNIT-I 1. Define Algorithm. An algorithm is a sequence of unambiguous instructions for solving a problem in a finite amount of time. 2.Write a short note

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016 CSCE 321/3201 Analysis and Design of Algorithms Prof. Amr Goneid Fall 2016 CSCE 321/3201 Analysis and Design of Algorithms Prof. Amr Goneid Course Resources Instructor: Prof. Amr Goneid E-mail: goneid@aucegypt.edu

More information

EECS 2011M: Fundamentals of Data Structures

EECS 2011M: Fundamentals of Data Structures M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James

More information

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

L.J. Institute of Engineering & Technology Semester: VIII (2016) Subject Name: Design & Analysis of Algorithm Subject Code:1810 Faculties: Mitesh Thakkar Sr. UNIT-1 Basics of Algorithms and Mathematics No 1 What is an algorithm? What do you mean by correct algorithm?

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad -500 043 INFORMATION TECHNOLOGY TUTORIAL QUESTION BANK Course Name : DESIGN AND ANALYSIS OF ALGORITHMS Course Code : AIT001 Class

More information

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

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005 CS302 Topic: Algorithm Analysis Thursday, Sept. 22, 2005 Announcements Lab 3 (Stock Charts with graphical objects) is due this Friday, Sept. 23!! Lab 4 now available (Stock Reports); due Friday, Oct. 7

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

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

D.K.M.COLLEGE FOR WOMEN (AUTONOMOUS), VELLORE-1. D.K.M.COLLEGE FOR WOMEN (AUTONOMOUS), VELLORE-1. DESIGN AND ANALYSIS OF ALGORITHM UNIT- I SECTION-A 2 MARKS 1. Define an algorithm? 2. Specify the criteria of algorithm? 3. What is Computational Procedure?

More information

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

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm INTRODUCTION A. Preliminaries: Purpose: Learn the design and analysis of algorithms Definition of Algorithm: o A precise statement to solve a problem on a computer o A sequence of definite instructions

More information

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

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Lecture 3. Recurrences / Heapsort

Lecture 3. Recurrences / Heapsort Lecture 3. Recurrences / Heapsort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS For COMPUTER SCIENCE DATA STRUCTURES &. ALGORITHMS SYLLABUS Programming and Data Structures: Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary

More information

Data Structures and Algorithms Week 4

Data Structures and Algorithms Week 4 Data Structures and Algorithms Week. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Week Divide and conquer Merge

More information

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

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems. 2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper

More information

CS:3330 (22c:31) Algorithms

CS:3330 (22c:31) Algorithms What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.

More information

Lecture 5: Sorting Part A

Lecture 5: Sorting Part A Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines

More information

Midterm solutions. n f 3 (n) = 3

Midterm solutions. n f 3 (n) = 3 Introduction to Computer Science 1, SE361 DGIST April 20, 2016 Professors Min-Soo Kim and Taesup Moon Midterm solutions Midterm solutions The midterm is a 1.5 hour exam (4:30pm 6:00pm). This is a closed

More information

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

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

Introduction to Algorithms Third Edition

Introduction to Algorithms Third Edition Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Introduction to Algorithms Third Edition The MIT Press Cambridge, Massachusetts London, England Preface xiü I Foundations Introduction

More information

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

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation IV/IV B.Tech (Regular) DEGREE EXAMINATION Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation Maximum: 60 Marks 1. Write briefly about the following 1*12= 12 Marks a) Give the characteristics

More information

Algorithm Analysis and Design

Algorithm Analysis and Design Algorithm Analysis and Design Dr. Truong Tuan Anh Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology VNU- Ho Chi Minh City 1 References [1] Cormen, T. H., Leiserson,

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

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

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Introduction to Algorithms Preface xiii 1 Introduction 1 1.1 Algorithms 1 1.2 Analyzing algorithms 6 1.3 Designing algorithms 1 1 1.4 Summary 1 6

More information

Lecture 1. Introduction

Lecture 1. Introduction Lecture 1 Introduction 1 Lecture Contents 1. What is an algorithm? 2. Fundamentals of Algorithmic Problem Solving 3. Important Problem Types 4. Fundamental Data Structures 2 1. What is an Algorithm? Algorithm

More information

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK DESIGN AND ANALYSIS OF ALGORITHMS UNIT1: INTRODUCTION OBJECTIVE: Algorithms play the central role in both the science and the practice of computing. There are compelling reasons to study

More information

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Page 1 UNIT I INTRODUCTION 2 marks 1. Why is the need of studying algorithms? From a practical standpoint, a standard set of algorithms from different

More information

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D. CSE 0 Name Test Fall 00 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to convert an array, with priorities stored at subscripts through n,

More information

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms

More information

FINAL EXAM SOLUTIONS

FINAL EXAM SOLUTIONS COMP/MATH 3804 Design and Analysis of Algorithms I Fall 2015 FINAL EXAM SOLUTIONS Question 1 (12%). Modify Euclid s algorithm as follows. function Newclid(a,b) if a

More information

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

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Concept Exam Code: 16 All questions are weighted equally. Assume worst case behavior and sufficiently large input sizes unless otherwise specified. Strong induction Consider this

More information

Data Structures and Algorithms Chapter 4

Data Structures and Algorithms Chapter 4 Data Structures and Algorithms Chapter. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Chapter Divide and conquer

More information

CS 303 Design and Analysis of Algorithms

CS 303 Design and Analysis of Algorithms Mid-term CS 303 Design and Analysis of Algorithms Review For Midterm Dong Xu (Based on class note of David Luebke) 12:55-1:55pm, Friday, March 19 Close book Bring your calculator 30% of your final score

More information

5105 BHARATHIDASAN ENGINEERING COLLEGE

5105 BHARATHIDASAN ENGINEERING COLLEGE CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS II CSE/IT /IV SEMESTER UNIT I PART A 1. Design an algorithm to compute the area and circumference of a circle?(dec 2016) 2. Define recurrence relation? (Dec 2016)

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

( ) n 3. n 2 ( ) D. Ο

( ) n 3. n 2 ( ) D. Ο CSE 0 Name Test Summer 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n n matrices is: A. Θ( n) B. Θ( max( m,n, p) ) C.

More information

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist. Sorting Popular algorithms: Selection sort* Insertion sort* Bubble sort* Quick sort* Comb-sort Shell-sort Heap sort* Merge sort* Counting-sort Radix-sort Bucket-sort Tim-sort Many algorithms for sorting

More information

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

COE428 Lecture Notes Week 1 (Week of January 9, 2017) COE428 Lecture Notes: Week 1 1 of 10 COE428 Lecture Notes Week 1 (Week of January 9, 2017) Table of Contents COE428 Lecture Notes Week 1 (Week of January 9, 2017)...1 Announcements...1 Topics...1 Informal

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

TOTAL CREDIT UNITS L T P/ S SW/F W. Course Title: Analysis & Design of Algorithm. Course Level: UG Course Code: CSE303 Credit Units: 5

TOTAL CREDIT UNITS L T P/ S SW/F W. Course Title: Analysis & Design of Algorithm. Course Level: UG Course Code: CSE303 Credit Units: 5 Course Title: Analysis & Design of Algorithm Course Level: UG Course Code: CSE303 Credit Units: 5 L T P/ S SW/F W TOTAL CREDIT UNITS 3 1 2-5 Course Objectives: The designing of algorithm is an important

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION Chendu College of Engineering & Technology (Approved by AICTE, New Delhi and Affiliated to Anna University) Zamin Endathur, Madurantakam, Kancheepuram District 603311 +91-44-27540091/92 www.ccet.org.in

More information

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems Divide & Conquer Divide & Conquer The Divide & Conquer approach breaks down the problem into multiple smaller sub-problems, solves the sub-problems recursively, then combines the solutions of the sub-problems

More information

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each CSE 0-00 Test Spring 0 Name Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose f ( x) is a monotonically increasing function. Which of the

More information

Lecture 9: Sorting Algorithms

Lecture 9: Sorting Algorithms Lecture 9: Sorting Algorithms Bo Tang @ SUSTech, Spring 2018 Sorting problem Sorting Problem Input: an array A[1..n] with n integers Output: a sorted array A (in ascending order) Problem is: sort A[1..n]

More information

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# # 1. Prove that n log n n is Ω(n). York University EECS 11Z Winter 1 Problem Set 3 Instructor: James Elder Solutions log n n. Thus n log n n n n n log n n Ω(n).. Show that n is Ω (n log n). We seek a c >,

More information

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment. CSE 3101: Introduction to the Design and Analysis of Algorithms Instructor: Suprakash Datta (datta[at]cse.yorku.ca) ext 77875 Lectures: Tues, BC 215, 7 10 PM Office hours: Wed 4-6 pm (CSEB 3043), or by

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

4. Sorting and Order-Statistics

4. Sorting and Order-Statistics 4. Sorting and Order-Statistics 4. Sorting and Order-Statistics The sorting problem consists in the following : Input : a sequence of n elements (a 1, a 2,..., a n ). Output : a permutation (a 1, a 2,...,

More information

Advanced Algorithms and Data Structures

Advanced Algorithms and Data Structures Advanced Algorithms and Data Structures Prof. Tapio Elomaa Course Basics A new 7 credit unit course Replaces OHJ-2156 Analysis of Algorithms We take things a bit further than OHJ-2156 We will assume familiarity

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

CPSC 320 Midterm 2 Thursday March 13th, 2014

CPSC 320 Midterm 2 Thursday March 13th, 2014 CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5.

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5. Notation Index (there exists) (for all) Fn-4 Fn-4 (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5 ( n ) k (binomial coefficient) CL-15 ( n m 1,m 2,...) (multinomial coefficient)

More information

CS6402 Design and Analysis of Algorithm. 2 Marks Question & Answer UNIT I INTRODUCTION

CS6402 Design and Analysis of Algorithm. 2 Marks Question & Answer UNIT I INTRODUCTION CS6402 Design and Analysis of Algorithm 2 Marks Question & Answer UNIT I INTRODUCTION 1. What is performance measurement? Performance measurement is concerned with obtaining the space and the time requirements

More information

Classic Data Structures Introduction UNIT I

Classic Data Structures Introduction UNIT I ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero

More information

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

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +... Design and Analysis of Algorithms nd August, 016 Problem Sheet 1 Solutions Sushant Agarwal Solutions 1. A d-ary tree is a rooted tree in which each node has at most d children. Show that any d-ary tree

More information

n 2 ( ) ( ) + n is in Θ n logn

n 2 ( ) ( ) + n is in Θ n logn CSE Test Spring Name Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply an m n matrix and a n p matrix is in: A. Θ( n) B. Θ( max(

More information

Data Structures and Algorithms. Part 2

Data Structures and Algorithms. Part 2 1 Data Structures and Algorithms Part 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples displayed

More information

Data Structures and Algorithms Chapter 2

Data Structures and Algorithms Chapter 2 1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Algorithmic Analysis. Go go Big O(h)!

Algorithmic Analysis. Go go Big O(h)! Algorithmic Analysis Go go Big O(h)! 1 Corresponding Book Sections Pearson: Chapter 6, Sections 1-3 Data Structures: 4.1-4.2.5 2 What is an Algorithm? Informally, any well defined computational procedure

More information

Analysis and Design of Algorithm

Analysis and Design of Algorithm CS 6402 DESIGN AND ANALYSIS OF ALGORITHM A Course Material on Analysis and Design of Algorithm By Mr. S.ANBARASU ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING SASURIE COLLEGE OF ENGINEERING

More information

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9) CPSC 311 Lecture Notes Sorting and Order Statistics (Chapters 6-9) Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from

More information

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

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο CSE 0 Name Test Fall 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally

More information

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, ) INTRODUCTION Objective: - Algorithms - Techniques - Analysis. Algorithms: Definition: Pseudocode: An algorithm is a sequence of computational steps that tae some value, or set of values, as input and produce

More information

INTRODUCTION TO ALGORITHMS

INTRODUCTION TO ALGORITHMS UNIT- Introduction: Algorithm: The word algorithm came from the name of a Persian mathematician Abu Jafar Mohammed Ibn Musa Al Khowarizmi (ninth century) An algorithm is simply s set of rules used to perform

More information

Data Structures and Algorithms. Roberto Sebastiani

Data Structures and Algorithms. Roberto Sebastiani Data Structures and Algorithms Roberto Sebastiani roberto.sebastiani@disi.unitn.it http://www.disi.unitn.it/~rseba - Week 0 - B.S. In Applied Computer Science Free University of Bozen/Bolzano academic

More information

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; } if (n

More information

Multiple-choice (35 pt.)

Multiple-choice (35 pt.) CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need

More information

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn) CSE 0 Name Test Fall 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to find the maximum of the n elements of an integer array is in: A.

More information

Sorting & Growth of Functions

Sorting & Growth of Functions Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis

More information

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 4 Divide-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Divide-and-Conquer The most-well known algorithm design strategy: 2. Divide instance of problem into two or more

More information

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

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V PSD1A DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V UNIT I -- Introduction -- Definition of Algorithm -- Pseudocode conventions -- Recursive algorithms -- Time and space complexity -- Big- o notation --

More information

5. For recursive algorithms, what do we need to define and solve to do the runtime analysis?

5. For recursive algorithms, what do we need to define and solve to do the runtime analysis? CS430 Lecture 1 Activities Opening Questions 1. Algorithm A takes 5 seconds to sort 1000 records, and Algorithm B takes 10 seconds to sort 1000 records. You have the code for both algorithms. When deciding

More information

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) ( ) C.  1 n. ( ) $ f n. ( ) B.  log( n! ) ( ) and that you already know ( ) ( )  % g( n) ( )  #& CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i

More information

UNIT 1 ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS UNIT 1 ANALYSIS OF ALGORITHMS Analysis of Algorithms Structure Page Nos. 1.0 Introduction 7 1.1 Objectives 7 1.2 Mathematical Background 8 1.3 Process of Analysis 12 1.4 Calculation of Storage Complexity

More information

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

( ) + n. ( ) = n 1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1 CSE 0 Name Test Summer 00 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose you are sorting millions of keys that consist of three decimal

More information

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS DESIGN AND ANALYSIS OF ALGORITHMS K.PALRAJ M.E.,(Ph.d) Assistant Professor Department of Computer Science and Engineering Sri Vidya College of Engineering & Technology CS6402 DESIGN AND ANALYSIS OF ALGORITHMS

More information

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University Cpt S 223 Course Overview 1 Course Goals Learn about new/advanced data structures Be able to make design choices on the suitable data structure for different application/problem needs Analyze (objectively)

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.) CSE 0 Name Test Spring 006 Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose f ( x) is a monotonically increasing function. Which of the

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element

More information

( D. Θ n. ( ) f n ( ) D. Ο%

( D. Θ n. ( ) f n ( ) D. Ο% CSE 0 Name Test Spring 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to run the code below is in: for i=n; i>=; i--) for j=; j

More information

( ) 1 B. 1. Suppose f x

( ) 1 B. 1. Suppose f x CSE Name Test Spring Last Digits of Student ID Multiple Choice. Write your answer to the LEFT of each problem. points each is a monotonically increasing function. Which of the following approximates the

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Merge Sort & Quick Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm

More information

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming Chapter 1 Algorithms: Efficiency, Analysis, and dod Order Preface techniques for solving problems using computer approach or methodology but not programming language e.g., search Collie Collean in phone

More information