# 970 -SREE SOWDAMBIKA COLLEGE OF ENGINEERING Chettikurichi, Aruppukottai Sub. Code Sub.

Name : CS41 : Design and Analysis Of Algorithms CLASS TEST-1 1. What are asymptotic notations? Explain in detail.(Nov/Dec 2007)(6) 2. Derive the analysis (all cases) for linear search method.(Apr/May 2008)(10) CLASS TEST-2 3. Solve the homogeneous and inhomogeneous recurrence equation(12) 4. Give the smoothness rule applied for recurrence relation (Nov/Dec 2007) (2) 5. How efficiency of an algorithm is defined?(Nov/Dec 2007)(2) CLASS TEST-3 6. Write an algorithm to sort a given list of elements using merge sort. Show the operation of the algorithm on the list 38, 27, 43,3,9,82,10 (Apr/May 2008) (12) 7. List out any two drawbacks of binary search algorithm(Nov/Dec 2007) (2) 8. Write a pseudo code for a divide and conquer algorithm for finding the position of the largest element in an array of ‘n’ numbers. (Nov/Dec 2008) (2) CLASS TEST-4 9. Explain Greedy approach to solve the knapsack problem. Find an optimal solution to the knapsack instance n=3, m=20, (p1,p2,p3)=(25,24,15) & (w1,w2,w3)=(18,15,10).(10) 10. Explain pseudo algorithm for container loading problem. Analyses your algorithm. (6) CLASS TEST-5 11. For the given digraph, obtain optimum cost Tour. (14) (Apr/May 2008) Year & Branch: II - CSE Staff In-charge: Ms.S.Vijaya Iswariya Lakshmi

Hamiltonian Problem. Solve the all pair shortest path problem for the diagraph with the weighted matrix given below. Define traveling Salesperson problem (May/June 2009) (2) CLASS TEST-7 15. a b c d a 0 2 ∞ 6 b ∞ 0 7 ∞ c 3 ∞ 0 ∞ d ∞ ∞ 1 0 (Nov/Dec 2006) (12) 14. (Nov/Dec 2008).10 13 1 5 6 15 8 12 3 9 4 9 20 8 10 2 (14) 12. (May/June 2007) • • Queens Problem. . (14) 16. State the principle of backtracking (Nov/Dec 2005) (2) CLASS TEST-8 . Using Backtracking enumerate how can you solve the following problems. Comparison between divide and conquer and dynamic programming (2) CLASS TEST-6 13.

(Apr/May 2008) & d=13. c2 and n0 are Constants> 0. Define articulation point (Apr/May 2008) (2) CLASS TEST-10 21. . 2Analysis of Linear Search. (Nov/Dec 2008) Item 1 2 3 4 (14) Weight 10 7 8 4 Values \$100 \$63 \$56 \$12 W=16 S= {3. where c & n0 are constants > 0 • O (g (n)): class of functions f (n) that grow no faster than g (n) Big ‘Θ’ notation: (Asymptotic Tight Bound) T (n) = f (n) = Θ (g (n)) • If c1*g (n) <= f (n) <= c2*g (n) for all n > n0. Cbest=1 Worst case time complexity . (12) CLASS TEST-9 19. Best case time complexity • It is time complexity algorithm runs for short time • If the key element present at first location in the list (X [0…. (Nov/Dec2008). • Θ (g (n)): class of functions f (n) that grow at same rate as g (n) Big ‘Ω’ notation: (Asymptotic lower bound) T (n) = f (n) = Ω (g (n)) • If f (n) >= c*g (n) for all n > n0.Big ‘Oh’ notation: (Asymptotic upper bound) T (n) = f (n) = O (g (n)) • If f (n) <= c*g (n) for all n ≥ n0. (Nov/Dec2009). where c and n0 are constants > 0 • Ω (g (n)): class of functions f (n) that grow at least as fast as g (n). What is graph coloring and write some applications? (4) 22. Apply the backtracking technique to solve the following instance of the subset sum problem. Explain in detail about BFS and DFS (14) (Nov/Dec 2007). where c1. May/June 2007) 20. 5. Asymptoic Notations . (Apr/May2008) (2) CLASS TEST-1 –Answer Key 1. 6} 18.17. Solve the following instance of the knapsack problem by branch and bound algorithm. 4. State the properties of NP – Hard problems.n-1]) then algorithm runs very short time.

. Recurrence Relations Homogenous Recurrence Relations • Solve: Try Multiply by x n = a 1 x n −1 + a 2 x n −2 +.n-1]) then algorithm runs very long time.It is time complexity algorithm runs for long time If the key element present at nth location in the list (X [0…. + a d x n −d a0 ≠ 0 xn = rn rd rn a0 ≠ 0 • Get characteristic equation: r d − a 1 r d -1 − a 2 r d ..2 −. Average case time complexity • It gives behavior of an algorithm on specific random input • C avg (n)=Probability of unsuccessful search + Probability of successful search • • CLASS TEST-2–Answer Key 3. r2.. rd ⇒ x n = ∑ c i rin i =1 d x n ~ c i rin • Where ri is dominant root ri >rj ∀j≠ i Inhomogeneous Recurrence Equations x n = a 1 x n −1 + a 2 x n −2 +. − a d = 0 Case of Distinct Roots • Distinct Roots r1. ….. + a d x n+a0 −d • Nonzero constant term • Solution Method a0 ≠ 0 (1) Solve homogenous equation Yn = a1 Yn −1 + a 2 Yn − 2 + . + a n Yn −d Case ∑a i =1 .....

• We have n kinds of items. • The most common formulation of the problem is the 0-1 knapsack problem.. Where b>=2. and solve for constants 4. and places them in separate arrays. find the most valuable set of items that fit in a knapsack of fixed volume. We usually assume that all values and weights are nonnegative. Each kind of item j has a value pj and a weight wj.This means that the time needed to perform insertion and deletion and many others Operations can be O (N) in the worst case a binary tree with N node has height at least (log N) 8. 5. The maximum weight that we can carry in the bag is W. Algorithm Refer Book CLASS TEST-4–Answer Key 9. Smoothness of Recurrence relation Let T (n) be an eventually non decreasing function and f (n) be a smooth function. If T (n) £ Θ (f (n)) for values of n that are powers of b. Mathematically the 0-1-knapsack problem can be formulated as: minimize . add particular solution  a x n = cn=  0  ∑ ia i   n   3) Add particular and homogeneous solutions. Knapsack problem Given items of different values and volumes.indicates how much extra memory the algorithm needs. Algorithm Refer Book 7. This sorting method is an example of the DIVIDE-AND-CONQUER paradigm  The merge sort is a comparison sort and has an algorithmic complexity of O (n log n). which restricts the number xj of copies of each kind of item to zero or one. Merge Sort  The merge sort splits the list to be sorted into two equal halves.indicates how fast the algorithm runs • Space complexity . Drawbacks of Binary Search The disadvantage of a binary search tree is that its height can be as large as N-1 . Efficiency of an algorithm • Time complexity . then T (n) £ Θ (f (n)) for any n. CLASS TEST-3–Answer Key 6. 1 through n.

s-{j}} Hence from vertex 2 to 1. 1) =d (4.4)+Cost(4. {2. each container is loaded.S)=min { d[I. {2. {4}. 1) =15 Cost (2. 1) =25 Cost (4.1 (Length is 35) . 4}. {2. Container loading problem • In this problem. 1) =d (3. 1) =18 Cost (3.3. CLASS TEST-5–Answer Key 11. Φ. Optimum Tour Distance matrix 1 1 2 3 4 0 5 6 8 2 10 0 13 8 3 15 ∞ 0 9 4 20 ∞ 12 0 Algorithm Refer Book Step 1: Select any arbitrary node say select 1 Let S=Φ then. Φ.{3}. {2}.1)=min{ [d(2. the ship is loaded attach stage.Subject to Algorithm Refer Book 10. 1) =18 Cost (4. {2}. 1) =6 Cost (4.4. 1) =13 Cost (4.1)]. Vertex 3 to 1 and vertex 4 to 1 calculate cost Cost (2. 1) =d (2.{3. 3}. 3. 1) =8 Step 2: Candidate(S) =1 Apply formula Cost(i. • • The total weight of all the containers must be less than or equal to the capacity. The Greedy approach is applied to the problem for solving.3) +Cost (s. 2. {3}. 1) =5 Cost (3.1)]=25 Cost (3. 1) =35 Optimum tour is: 1. 1) =23 Step 4: Candidate(S) =3 Cost (1.j] +Cost (j. Φ. {3}.[d(2.4}. Cost (2.{4}. 4}. 1) =15 Step 3: Candidate(S) =2 Cost(2.

.These sub problems are solved In dynamic programming many decision independly. CLASS TEST-6–Answer Key 13.Distance Matrix 0 2 ∞ 6 0 2 ∞ 6 ∞ 0 7 ∞ D(2) = ∞ 0 7 ∞ 3 ∞ 0 ∞ D(1) = ∞ ∞ 1 0 0 2 9 6 ∞ 0 7 ∞ 3 5 0 9 ∞ ∞ 1 0 D(0) = 3 ∞ 0 9 ∞ ∞ 1 0 0 2 9 6 10 0 7 16 3 5 0 9 4 6 1 0 0 2 7 6 10 0 7 16 3 5 0 9 4 6 1 0 .Finally all the solutions of sub sequences are generated and all the overlapping problems are collected together to get the subinstances are considered. Because of rework on solutions and conquer strategy The divide and conquer uses top down Dynamic programming approach of problem 4.12. solving(recursive approach of the problem solving(iterative methods) method) Divide and conquer splits its input at Dynamic programming splits its input at every 5.i. specific deterministic points usually in the possible split pints it determines which split middle point is optimal. Comparison b/w Divide and Conquer and Dynamic Programming Sl.e. Dynamic programming is efficient than divide uses bottom up 3.No The 1. Divide and Conquer problem is divided into Dynamic Programming small subproblems. are neglected.duplicate sub solutions may be obtained Divide and conquer is less efficient In dynamic computing duplications in solutions is avoided totally. solution to the given problem In this method duplications in sub solutions 2.

6. or all the queens must be in different columns All solutions are permutations of the 8-tuple (1. . 8) • • . 5. • Place eight queens on an 8 × 8 chessboard so that no queen attacks another queen. where xi is the column for ith queen • Identify explicit constraints Explicit constraints using 8-tuple formulation are Si = {1. Identify data structures to solve the problem First pass: Define the chessboard to be an 8 × 8 array . define the chessboard solution to be an 8-tuple (x1.D(3) = D(4) = 14. Traveling Salesperson problem The goal of the Travelling Salesman Problem (TSP) is to find the “cheapest” tour of a select number of “cities” with the following restrictions: ● ● You must visit each city once and only once You must return to the original starting point CLASS TEST-7–Answer Key 15. 4. 2. 6. 3. .8-tuples Identify implicit constraints o No two xi can be the same.Second pass: Since each queen is in a different row. 3. 1≤i≤8 o Solution space of 88 . . 8 Queens Problem. . 7. x8). 4. 7. 5. 2. 8}.

7. 6} & d=13. Algorithm Refer Book 16. 4. A Hamiltonian cycle is a round trip path along ‘n’ edges of G that visits every vertex once and returns to its starting position.x3. 2. 6. 4.• • Reduces the size of solution space from 88 to 8! tuples o No two queens can be on the same diagonal. E) be a connected graph with n vertices. 1.…xn) can either minimizes or maximizes or satisfies the criteria function.e. using backtrack formulation.x4. The solution obtained i.Backtracking technique to solve subset sum problem S= {3. 0 W/O 3 With 3 3 With 4 W/O 4 7 With 5 1 2 With 6 1 3 W/O 5 With 5 7 W/O 6 8 With 4 W/ O 4 0 3 W/ O 5 3 4 With 5 W/O 5 0 9 4 Solution = {3.x2.…xn )which is chosen from solution space. (x1. The solution above is expressed as an 8-tuple as 4.x2. 3. CLASS TEST-8–Answer Key 17.x3. 6} 7 .x4. 5 Algortihm Refer Book Hamiltonian circuit problem using Backtracking • • Let G = (V. 5. 8. Backtracking Backtracking is a method in which the desired solution is expressed as n tuple (x1.

Applications of DFS are. Graph Coloring and applications • Graph coloring is the problem of coloring each vertex in a graph such that no two adjacent vertices are the same color • Some direct examples: – Map coloring – Register assignment CLASS TEST-9–Answer Key 19. • Compute the connected components • Compute a spanning forest Find shortest path.BFS implemented using Queue . CLASS TEST-10–Answer Key 21. Articulation point A vertex v in a connected graph G is an articulation point if the deletion of vertex v together with all edges incident to v disconnects the graph into two or more non empty components.Knapsack Problem-Solution Item 1 2 3 4 W=16 With 1 ub = v+ (W-w) * v(i+1)/ w (i+1) Weight 10 7 8 4 Values vi/wi \$100 \$63 \$56 \$12 10 9 7 3 w=0 v=0 ub=100 W/O 1 w=4 v=40 ub=76 w=0 v=0 ub=60 .Applications of BFS are.DFS implemented using stack . • Topological Sort • Strongly Connected Components. BFS and DFS DFS (Depth First Search) .18. • Spanning Forest BFS (Breadth First Search) . Diagram Refer Book 20.

w=4 v=40 ub=64 .With 2 W/O 2 w=11 w=4 v=40 ub=70 W/O 3 With 3 w=9 v=65 ub=69 With 4 W/O 4 w=12 w=9 v=65 ub=65 22. Propertites of NP – Hard Problems A problem H is NP-hard if and only if there is an NP-complete problem L that is Polynomial time.