Daa Course File Final 2012
Daa Course File Final 2012
COURSE FILE
DESIGN & ANALYSIS OF ALGORITHMS
CS - 307 5TH SEM C.S.E
RATIONALE :
Informally, an algorithm is any well-dened computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output. We can also view an algorithm as a tool for solving a well-specied computa-tional problem. The statement of the problem species in general terms the desired input/output relationship. The algorithm describes a specic computational proce- dure for achieving that input/output relationship. For example, one might need to sort a sequence of numbers into non decreasing order. This problem arises frequently in practice and provides fertile ground for introducing many standard design techniques and analysis tools. Here is how we formally dene the sorting problem:
Input: A sequence of n numbers a1 , a2 , . . . , an . Output: A permutation (reordering) a1 , a2 , . . . , an of the input sequence such that a1 a2 an .
For example, given the input sequence 31, 41, 59, 26, 41, 58, a sorting algorithm returns as output the sequence 26, 31, 41, 41, 58, 59. Such an input sequence is called an instance of the sorting problem. In general, an instance of a problem consists of the input (satisfying whatever constraints are imposed in the problem statement) needed to compute a solution to the problem. Sorting is a fundamental operation in computer science (many programs use it as an intermediate step), and as a result a large number of good sorting algorithms
Which algorithm is best for a given application depends onamong other factorsthe number of items to be sorted, the extent to which the items are already somewhat sorted, possible restrictions on the item values, and the kind of storage device to be used: main memory, disks, or tapes. An algorithm is said to be correct if, for every input instance, it halts with the correct output. We say that a correct algorithm solves the given computational problem. An incorrect algorithm might not halt at all on some input instances, or it might halt with an answer other than the desired one. Contrary to what one might expect, incorrect algorithms can sometimes be useful, if their error rate can be controlled.
An algorithm can be specied in English, as a computer program, or even as a hardware design. The only requirement is that the specication must provide a precise description of the computational procedure to be followed.
2|Page
What kinds of problems are solved by algorithms? Sorting is by no means the only computational problem for which algorithms have been developed. (You probably suspected as much when you saw the size of this book.) Practical applications of algorithms are ubiquitous and include the follow-ing examples:
The Human Genome Project has the goals of identifying all the 100,000 genes in human DNA, determining the sequences of the 3 billion chemical base pairs that make up human DNA, storing this information in databases, and devel-oping tools for data analysis. Each of these steps requires sophisticated algo-rithms. The savings are in time, both human and machine, and in money, as more information can be extracted from laboratory techniques.
The Internet enables people all around the world to quickly access and retrieve large amounts of information. In order to do so, clever algorithms are employed to manage and manipulate this large volume of data. Examples of problems which must be solved include nding good routes on which the data will travel (techniques for solving such problems ), and using a search engine to quickly nd pages on which particular information resides
1.1 Algorithms
Electronic commerce enables goods and services to be negotiated and ex-changed electronically. The ability to keep information such as credit card num-bers, passwords, and bank statements private is essential if electronic commerce is to be used widely. Public-key cryptography and digital signatures are among the core technologies used and are based on numerical algorithms and number theory.
In manufacturing and other commercial settings, it is often important to allo-cate scarce resources in the most benecial way. An oil company may wish to know where to place its wells in order to maximize its expected prot. A candidate for the presidency of the United States may want to determine where to spend money buying campaign advertising in order to maximize the chances of winning an election. An airline may wish to assign crews to ights in the least expensive way possible, making sure that each ight is covered and that government regulations regarding crew scheduling are met. An Internet service provider may wish to determine where to place additional resources in order to serve its customers more effectively.
3|Page
SYLLABUS
4|Page
CS - 307 DESIGN AND ANALYSIS OF ALGORITHMS External Marks: 60 L T P Internal Marks: 40 3 1 Total Marks: 100 PREREQUISITES: Discrete Structures and Data Structures. OBJECTIVES: Date Structures are an integral part of algorithm design and Discrete Structures covers topics like graph theory. COURSE CONTENTS: Models of computation. Algorithm analysis, order arithmetic, time and space complexities and average and worst case analysis, lower bounds. [L-8] Algorithm design techniques: divide and conquer, search and traversals. Dynamic programming. Backtracking. Branch and bound. [L-16] Sorting and searching algorithms, combinatorial algorithms, string processing algorithms. Algebraic algorithms, set algorithms. Hard problems and approximation algorithms. [L12] Problem classes P, NP, NP-hard and NP-complete, deterministic and non deterministic polynomial time algorithms., Approximation algorithms for some NP-complete problems. [L-6] TEXT BOOKS 1.V. Aho, J.E.Hopcroft, J.D. Ullman, design and Analysis of Algorithms, Addison Wesley, 1976. 2.Horowitz, S. Sahni, Fundamentals of Computer Algorithms, Galgotia Publishers, 1984. REFERENCES: 1. D.E.Knuth, The Art of Computer Programming, Vols. 1 and 3, Addison Wesley, 1968,1975. 2. K.Mehlhorn, Data Structures and Algorithms, Vols. 1 and 2, Springer Verlag, 1984. 3. Purdom, Jr.and C. A. Brown, The Analyses of Algorithms, Holt Rinechart and Winston, 1985.
5|Page
INDEX
S.NO TOPICS PAGE NO.
1 2 3 4 5 6 7
Time Table Assignments Sessionals Question Bank University Papers Viva Voice Questions Tutorial Sheet
7 9 11 16 23 28 34
6|Page
TIME TABLE
Period/Days Monday Tuesday Wednesday Thursday Friday LAB T 1 L L 2 3 4 LAB LAB LAB L L T 5 6 7 8 9
2 T
7 LAB
L L L L T
LAB LAB
LAB
7|Page
ASSIGNMENTS
8|Page
ASSIGNMENT 1 Q1 ) Explain all the Searching techniques. Q2 ) Explain all the Sorting techniques.
9|Page
SESSIONALS
10 | P a g e
5 * 2 = 10 marks
b. The order of complexity of Binary Search in Best case is --------in the average case is -------in the worst case is ----------Q 2 ) How the time and space complexities measured for an algorithm? Q 3 ) What is the computing time for the following statement { for i=1to m do for i=1 to n do c[i,j]:= a[i,j]+ b[i,j]; } Q 4 ) What do you mean by the term Lower bound & Upper bound. Q 5 ) What do you mean by Growth of Functions.
SECTION B
2 * 5 = 10 marks
Q 1 ) Explain the use of asymptotic notations in the analysis of algorithms. Q 2 ) Explain an algorithm to insert 5 elements in a Stack and in a Queue.
Q 1 ) Write an algorithm for the following & calculate their Time Complexitya. Quick Sort b. Binary Search Q 2 ) Explain various Models of Computation.
11 | P a g e
SECTION-A Q1. Define a) Cyclic interchange b) Minimum Spanning Tree Q2. What do you mean by Order Arithmetic? Q3. Differentiate between Krushkal Algorithm & Prims Algorithm? Q4. Differentiate between BFS& DFS? Q5.(a) Define lower bound & Upper bound. (b) Differentiate between Iteration and Recursion. SECTION-B
5*2=10
2*5=10
Q1. Given S<a,b,c,d,e,f> ; Probability(P)=<1,1,2,3,5,8>. Build binary tree according to Greedy Strategy. Q2. Explain Floyd-Warshall Algorithm with an example. Q3. Explain Knacksap Problem using Branch and Bound. Q4. Explain the Subset-Sum problem using the followingS=<3,4,5,6> ; X=9 SECTION-C Q1. Write a note on String Matching Algorithm? Q2. Starting from vertex V4, performa) BFS or DFS b) All spanning tree 1*10=10
12 | P a g e
Q3 From the given graph, draw a) Minimum Spanning Tree by Krushkal Algorithm b) Minimum Spanning Tree by Prims A8lgorithm c) Shortest Path from V1 to V
13 | P a g e
SECTION A Q 1 ) (a) What is stable sorting? (b) Define Cooks Levin Theorm. Q 2 ) Given an example of an algorithm which is infinite in nature Q 3 ) What are Stored RAM model?
5 * 2 = 10 marks
Q 4 ) What is time complexity. What is the time complexity of Merge Sort? Q 5 )Name three conditions under which sequential search of a list is preferable to binary search. SECTION B 2 * 5 = 10 marks
Q 1 ) Differentiate between N-P Hard and N-P Complete problems with example. Q 2 ) Write a algorithm for (a) Quick sort. (b) Merge Sort Q 3 ) Explain the relationship between Turing Machine and RAM models. Q 4 ) Design an algorithm which finds number of words, lines and characters in a given text. Q 5 ) Write an algorithm for Knapsack problem using greedy method.
SECTION C Q 1 ) Write short note on following: (a) Techniques for algebraic problems. (b) Polynomial time algorithm.
1*10 = 10 marks
Q 2 ) What are approximation algorithms? Differentiate between deterministic and nondeterministic algorithms . Q 3 ) Among Merge sort, Insertion sort and Bubble sort which sorting technique is the best in worst case. Support your arguments with an example and analysis. 14 | P a g e
QUESTION BANK
15 | P a g e
30. What is Binary Search Tree? 31. What are various steps used in design of an algorithms?Give an example of algorithm which is infinite in nature. 32. What is the order of the computation for the following loop. for(i=1,i<n,i+1) for(j=1,j<n,j+1) 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. Give the definition for 0/1 knapsack problem. What are the various set operations possible? What is algorithm of Pre Order Traversal. What is NP Hard Problem? What is Ordering Paradigm? Differentiate between full and complete Binary Tree. Write an algorithm for insertion sort. Also give its computing time. What is lower bound? What are explicit and implicit constraints? What is algebraic simplification? 16 | P a g e
.Explain with the help of suitable algorithms. 60. What is time and space complexity of Insertion Sort?Explain. 61. Write an string processing algorithm to identify whether a particular sequence of character is in string or not. 62. What are the various methods or techniques in which various algorithms can be expressed? 63. What is criterion function in backtracking?What is solution space for backtracking problem? Explain by taking sum of subset as a problem 64. What is LC Search. How does it help in finding a solution for Branch and Bound Algorithm. 17 | P a g e
110) Solve 15 Puzzle Problem using Branch and Bound .Initial arrangement of tiles is given below 111) Prove using the formal definition of Big O,(you need to give a ,c,,n0) 5n-3=O(n) 2n2+3n+20=O(n 2) 112) Prove using definition of Omega( you need to give a ,c,,n0) 3n-10=Omega(n) n2-5n-20=Omega(n 2) 113) By taking suitable example explain how the sum of Subset problem can be solved by using Backtracking algorithm design technique. 114) Explain the following algorithm design techniques ;
19 | P a g e
122) What do you mean by deterministic and Non Deterministic Algorithms. Differentiate between them. Write example for each of them. 123) Define NP Hard and NP Complete .Represent the relation between them. Prove that P is a subset of NP. 124) What is Clique Decision Problem. Show that clique optimization reduces to clique decision problem. 125) What are approximation Algorithms. Define absolute approximation and Eapproximation with example. 126) What is Algorithm .Write the various performance analysis techniques of Algorithm . 127) Discuss advantages and disadvantages of each 128) What is data structure ?Explain various data structures with examples 129) Write algorithm for Quick Sort using divide and Conquer . 130) What is Divide and Conquer algorithm. Use this algorithm to find maximum and minimum from a given array 131) Write an algorithm for Merge Sort using Divide and Conquer. 132) What do you mean by dynamic programming. Explain multistage Graphs using dynamic Programming 133) What are Strings. What are various String Algorithms. Explain any two of them. 20 | P a g e
21 | P a g e
22 | P a g e
23 | P a g e
Paper ID [A 0467]
(Please fill this Paper ID in OMR Sheet)
Section - A QI) (10x2 =20) a) Define Big Omega Notation and Little Omega Notation . b) What is re-entrantp rogram? c) What is Akermann's function give example? d) What is the basic principal of Divide and Conquer? e) What is stable sorting? f) Differentiate between Top-down and Bottom-up approach. g) Define recurrencere lation. h) Given an example of an algorithm which is infinite in nature. i) Namet hreec onditionsu nderw hich sequentiasl earcho f a list is preferable to binarv search. j) What is an algorithm? Section - B (4x5=20) Q2)Differentiate betu'een N-P hard and N-P complete problems with example' Q3)Find lower bound for multiplying m x n matrix with n x 1 vector' Q4)Write a algorithm for Quick sort and complexity' Q5)Explain how the knapsack problem can be solved using branch and bound algorithms. Section- C (2x10=20) Q6)Find the shortest path from node 1 to all vertices of the graph given below' Show all the intermediates teps.T he numberso n the edgesa re the weights' Q7) Compare general and recursive back tracking methods and also write a algorithm to find all Hamiltoman cycle for graph using back tracking method. Q8) Deftne a minimum spanning tree. Write Prim's algorithm to find minimum spanning tree. Q9) Write short note on following: (a) Techniques for algebraic problems (b) Polynomial time algorithm.
24 | P a g e
Roll No. TotalN o. of Questions: 09 Total N o. of pages: 02 DESIGN AND ANALYSIS OF AI-GORITHMS SUBJECT CODE : CS - 307
Paper ID IA 0467l
Time : 03 Hours Maximum Marks : 60 Instruction to Candidates: 1) Section- A is Compulsory. 2) Attempt any Four questions from Section - B. 3) Attempt any Two questions from Section - C. Section - A QI) (10x2=20) a)Define Big oh Notation (O) and Little oh Notarion (o). I b) What is re-entrant program? c) What is NP complete? d) What are row major and column major ordering? e)What is the purpose ofAVL Tree? f)Differentiate between space complexity and Time space tradeoff. g) What is a solution space in the backtracking? h) Given an example o f an algorithm which is infinite in nature. i) Name the three conditions under which sequential search of a list is preferable to binary search. j) State the knapsack problem using branch and bound technique. Section - B (4x5:20) Q2) Does greedy algorithm always give an optimal solution?Give arguments to support your answer. Q3)Among Merge sort, Insertion sort and Bubble sort which sorting technique is the best in worst case. Support your arguments with an example and analysis. Q4) Write a recursive algorithm for Binary search Tree and complexity. Q5) What aret he featureso f brancha nd bounda lgorithms?D iscussi n detail. Section - C (2 x 10:20) Q6) Sortt he following usingG eapsortte chniqueL :'-5,9,20,8,4,100,11,3,3>0. Q7) (a) What are approximation algorithms?Explain approximation vertex cover. (b) Using Knuth-Marries-pratt algorithm find whether the pattern P : <0010> is in the text T - < 11 010 00010 10001010100 010 >. Q8) What do you mean by dynamic programming?Explain assignment problem with example. Q9) Define a minimum spanning tree. Write Kruskal's algorithm to find minimum Spanning tree.
25 | P a g e
TotalN o. of Pages: 02
Paper ID I IA0467l
[Note: Pleasefi ll subjectc odea nd paperI D on OMRI
Time : 03 Hours Instruction to Candidates: 1) Section - A is Compulsory. 2) Attempt any Four questions from Section - B. 3) Attempt any Two questions from Section - C. Section - A' QI) a) Explain the term the order of complexity. b) Give the general characteristics of greedy algorithm" c) Describe the general principle of Divide and conquer. d) Explain the asymptotic notation. e) What is re-entrant program? 0 What is stable sorting? g) Differentiate between NP-Hard and Np-Complete. h) What do you mean by worst case analysis? i) Define Non-deterministic algorithm. j) Give an xample of an algorithm which is infinite in nature.
Maximum Marks : 60
(10x2-20)
Section B (4 x.5 - 20) Q2) Differentiate between deterministic and non-deterministic algorithms? Q3) Explain how the knapsack problem can be solved using branch and bound algorithms. Q4) Explain the backtracking problem with the help of suitable example? Q5) Explain how to validate and analyze the algorithms. Q6) Explain the algorithm of a non-deterministic finite automation. Section-C (10x2-20) Q7) Explain in detail basic concepts of P, NP, NP-hard and NP-complete problems. Q8) Explain in detail the structure of string storage. Design an algorithm which finds number of words, lines and characters in a given text. Q9) What do you mean by complexity of an algorithm? Define time and space complexity with examples.
26 | P a g e
27 | P a g e
1. What are the notations used in Evaluation of Arithmetic Expressions using prefix and postfix forms? Polish and Reverse Polish notations. 2. Convert the expression ((A + B) * C (D E) ^ (F + G)) to equivalent Prefix and Postfix notations. Prefix Notation: ^ - * +ABC - DE + FG Postfix Notation: AB + C * DE - - FG + ^ 3. Sorting is not possible by using which of the following methods? (a) Insertion (b) Selection (c) Exchange (d) Deletion (d) Deletion. 4. A binary tree with 20 nodes has 21 null branches?
Null Branches
It will have only 6 (ie,5+1) null branches. In general, A binary tree with n nodes has exactly n+1 null nodes. 5. What are the methods available in storing sequential files ? Straight merging, Natural merging, Polyphase sort, Distribution of Initial runs.
28 | P a g e
ii
iii
iv
7. In tree construction which is the suitable efficient data structure? (a) Array (b) Linked list (c) Stack (d) Queue (e) none (b) Linked list 8. What is the type of the algorithm used in solving the 8 Queens problem? Backtracking 9. There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a full binary tree? 15. In general: There are 2n-1 nodes in a full binary tree. By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15. Note: Full and Complete binary trees are different. All full binary trees are complete binary trees but not vice versa 10. In the given binary tree, using array you can store the node 4 at which location?
1 2 4 5 3 29 | P a g e
At location 6 1 2 3 4 5
Root
LC1
RC1
LC2
RC2
LC3
RC3
LC4
RC4
where LCn means Left Child of node n and RCn means Right Child of node n 11. Sort the given values using Quick Sort? 65 70 75 80 85 60 55 50 45
Sorting takes place from the pivot value, which is the first value of the given elements, this is marked bold. The values at the left pointer and right pointer are indicated using L and R respectively. 65 70L 75 80 85 60 55 50 45R
Since pivot is not yet changed the same process is continued after interchanging the values at L and R positions
65
45
75 L
80
85
60
55
50 R
70
65
45
50
80 L
85
60
55 R
75
70
65
45
50
55
85 L
60 R
80
75
70
65
45
50
55
60 R
85 L
80
75
70
When the L and R pointers cross each other the pivot value is interchanged with the value at right pointer. If the pivot is changed it means that the pivot has occupied its original position in the sorted order (shown in bold italics) and hence two different arrays are formed, one from start of the original array to the pivot position-1 and the other from pivot position+1 to end.
30 | P a g e
60 L
45
50
55 R
65
85 L
80
75
70 R
55 L
45
50 R
60
65
70 R
80 L
75
85
50 L
45 R
55
60
65
70
80 L
75 R
85
45
50
55
60
65
70
75
80
85
12. For the given graph, draw the DFS and BFS?
A X H E G P M J Y
BFS: DFS:
AXGHPEMYJ AXHPEYMJG
13. Of the following tree structure, which is, efficient considering space and time complexities? (a) Incomplete Binary Tree (b) Complete Binary Tree (c) Full Binary Tree
31 | P a g e
14. What is a spanning Tree? A spanning tree is a tree associated with a network. All the nodes of the graph appear on the tree once. A minimum spanning tree is a spanning tree organized so that the total edge weight between nodes is minimized.
15. Does the minimum spanning tree of a graph give the shortest distance between any 2 specified nodes? No. Minimal spanning tree assures that the total weight of the tree is kept at its minimum. But it doesnt mean that the distance between any two nodes involved in the minimumspanning tree is minimum.
16. Convert the given graph with weighted edges to minimal spanning tree.
1
410 2
3
310
200
5
400
410 2
32 | P a g e
TUTORIAL SHEETS
33 | P a g e
8. The order of complexity of Binary Search in Best case is -----in the average case is -----in the worst case is -------9. If W= MNOP,list all substrings of W. 10. What are the various methods or techniques in which various algorithms can be expressed?
TUTORIAL SHEET - 2
1. 2. 3. 4. 5. 6. Define the term Divide and Conquer. Explain the backtracking Problem with 4 Queens on a 4*4 chess board. What are the advantages of dynamic programming over greedy method? What are the conditions under which backtracking can be used? What is solution space in Backtracking Techniques ? How will Merge Sort sort the 6 numbers: 40, 10, 20 , 18, 16,62. by divide and conquer technique.
7. Give the definition for 0/1 knapsack problem. 8. In the following Graph ,find out the shortest distance of all the nodes from the node A
.Explain with the help of suitable algorithms. 9. Write an string processing algorithm to identify whether a particular sequence of character is in string or not. 10. What is criterion function in backtracking? What is solution space for backtracking problem? Explain by taking sum of subset as a problem
34 | P a g e
6. Explain in detail various Set algorithms 7. Solve 15 Puzzle Problem using Branch and Bound .Initial arrangement of tiles is given below 8. Define Job Sequencing with Deadlines .Solve the following problem using Job Sequencing with Deadlines: n=4,(P1,P2,P3,P4)=(100,10,15,27) (D1,D2,D3,D4)=(2,1,2,1) 9. Define Optimal Storage on Tapes. Solve the following using this technique: n=3, (L1,L2,L3)=(5,10,3) 10. What is Clique Decision Problem. Show that clique optimization reduces to clique decision problem.
35 | P a g e