AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 2014

Similar documents
CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

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

DESIGN AND ANALYSIS OF ALGORITHMS

Lecture 1. Introduction

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CS-6402 DESIGN AND ANALYSIS OF ALGORITHMS

Computer Science Foundation Exam

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Data Structures Brett Bernstein

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS JUN 2015

Lecture 3. Brute Force

Module 2: Classical Algorithm Design Techniques

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 14 Component-I (A) - Personal Details

Backtracking. Chapter 5

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

1 Introduction 2. 2 A Simple Algorithm 2. 3 A Fast Algorithm 2

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

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

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

A loose end: binary search

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

Solutions to Exam Data structures (X and NV)

Lecture 7. Transform-and-Conquer

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

COMP 251 Winter 2017 Online quizzes with answers

15CS43: DESIGN AND ANALYSIS OF ALGORITHMS

SELF-BALANCING SEARCH TREES. Chapter 11

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CSCE 411 Design and Analysis of Algorithms

Algorithm Design (8) Graph Algorithms 1/2

CPS 616 TRANSFORM-AND-CONQUER 7-1

Assertions & Verification & Example Loop Invariants Example Exam Questions

Sorting. Bubble Sort. Selection Sort

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Decrease and Conquer

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

CS Data Structures and Algorithm Analysis

CSci 231 Final Review

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

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

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

COMP3121/3821/9101/ s1 Assignment 1

CS521 \ Notes for the Final Exam

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

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

Algorithms 4. Odd or even Algorithm 5. Greatest among three numbers Algorithm 6. Simple Calculator Algorithm

UNIT 1 ANALYSIS OF ALGORITHMS

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

DESIGN AND ANALYSIS OF ALGORITHMS

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

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

DATA STRUCTURES/UNIT 3

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

DATA STRUCTURES AND ALGORITHMS. Sorting algorithms (insertion sort, bubble sort, selection sort)

DC54 DATA STRUCTURES DEC 2014

COMP Data Structures

Assertions & Verification Example Exam Questions

Chapter 9 Graph Algorithms

INSTITUTE OF AERONAUTICAL ENGINEERING

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

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

Chapter 9 Graph Algorithms

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Introduction to Algorithms I

Introduction to Algorithms

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

Introduction to Algorithms: Brute-Force Algorithms

Final Examination CSE 100 UCSD (Practice)

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

DESIGN AND ANALYSIS OF ALGORITHMS

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

Q1 Q2 Q3 Q4 Q5 Q6 Total

Brute Force: Selection Sort

LECTURE 3 ALGORITHM DESIGN PARADIGMS

Exam Sample Questions CS3212: Algorithms and Data Structures

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Student number: Datenstrukturen & Algorithmen page 1

Data Structures and Algorithms Chapter 2

Theorem 2.9: nearest addition algorithm

CS 440 Theory of Algorithms /

Transcription:

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 Q.2 a. Design an algorithm for computing gcd (m,n) using Euclid s algorithm. Apply the algorithm to find gcd (31415, 14142). ALGORITHM Euclid(m, n) //Computes gcd(m, n) by Euclid s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n= do r m mod n m n n r return m gcd(31415,14142) = gcd(14142,3131) = gcd(3131,1618) = gcd(1618,1513) = gcd(1513,15) = gcd(1513,15) = gcd(15,43) = gcd(43,19) = gcd(19,5) = gcd(5,4) = gcd(4,1) = gcd(1,) = 1. b. Let A be the adjacency matrix of an undirected graph. Explain what property of the matrix indicates that: (i) the graph is complete. (ii) the graph has a loop, i.e. an edge connecting a vertex to itself. (iii) the graph has an isolated vertex, i.e. a vertex with no edges incident to it. For the adjacency matrix representation: i. A graph is complete if and only if all the elements of its adjacency matrix except those on the main diagonal are equal to 1, i.e., A[i,j]=1 for every 1 i,j n, i = j. ii. A graph has a loop if and only if its adjacency matrix has an ele- ment equal to 1 on its main diagonal, i.e., A[i,i]=1for some 1 i n. iii. An (undirected, without loops) graph has an isolated vertex if and only if its adjacency matrix has an all-zero row. Q.3 a. Write general outlines for analysing time efficiency of recursive algorithms. General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms 1. Decide on a parameter (or parameters) indicating an input s size. 2. Identify the algorithm s basic operation. (As a rule, it is located in the innermost loop.) IETE 1

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 3. Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencies have to be investigated separately. 4. Set up a sum expressing the number of times the algorithm s basic operation is executed. 5. Using standard formulas and rules of sum manipulation, either find a closedform formula for the count or, at the very least, establish its order of growth. b. Design a recursive algorithm for computing 2 n for any non negative integer n that is based on the formula: 2 n =2 n 1 +2 n 1. Draw a tree of recursive calls for this algorithm for 2 4. Algorithm Power(n) //Computes 2 n recursively by the formula 2 n =2 n 1 +2 n 1 //Input: A nonnegative integer n //Output: Returns 2 n if n = return 1 else return Power(n 1) + Power(n 1) Q.4 a. Write the algorithm for Bubble sort and derive its time complexity. void bubblesort(int* array,int size) { int swapped; int i; for(i =1; i < size; i++) { swapped =;//this flag is to check if the array is already sorted int j; for(j =; j < size - i; j++) { if(array[j]> array[j+1]) { int temp = array[j]; array[j]= array[j+1]; array[j+1]= temp; swapped =1; } } if(!swapped){ break;//if it is sorted then stop IETE 2

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 } } } Complexity of bubble sort For an array of size n, in the worst case: 1st passage through the inner loop: n-1 comparisons and n-1 swaps (n-1)st passage through the inner loop: one comparison and one swap. All together: c ((n-1) + (n-2) +... + 1), where c is the time required to do one comparison, one swap, check the inner loop condition and increment j. We also spend constant time k declaring i,j,temp and initialising i. Outer loop is executed n-1 times, suppose the cost of checking the loop condition and decrementing i is c1 c ((n-1) + (n-2) +... + 1) + k + c1(n-1) (n-1) + (n-2) +... + 1 = n(n-1)/2 so our function equals c n*(n-1)/2 + k + c1(n-1) = 1/2c (n2-n) + c(n-1) + k complexity O(n2). b. Explain how Binary Search method fails to find 43 in the given sorted array: 8, 12, 25, 26, 35, 48, 57, 78, 86, 93, 97, 18, 135, 168, 21 (8) The value to be searched is 43. As explained earlier, in the first iteration low = 1, high = 15 and mid = 8 As 43 < A[8] = 78, therefore, as in part (i) low = 1, high = 8 1 = 7 and mid = 4 As 43 > A[4] = 26, the algorithm makes another iteration in which low = mid + 1 = 5 high = 7 and (new) mid = (5 + 7)/2 = 6 Next, as 43 < A[6] = 48, the algorithm makes another iteration, in which low = 5 high = 6 1 = 5 hence mid = 5, and A[5] = 35 As 43 > A[5], hence value A[5]. But, at this stage, low is not less than high and hence the algorithm returns 1, indicating failure to find the given value in the array. Q.5 a. Apply the DFS-based algorithm to solve the topological sorting problem for the following diagram: (8) IETE 3

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 Fig.2 The digraph below is not a dag. Its DFS traversal that starts at aencounters a back edge from e to a: b. Explain Lexicographic Permute algorithm, which generates permutations in lexicographical order. Also generate all permutations of {1, 2, 3, 4} by this algorithm. (8) ALGORITHM LexicographicPermute(n) //Generates permutations in lexicographic order //Input: A positive integer n //Output: A list of all permutations of {1,..., n} in lexicographic order initialize the first permutation with 12... n while last permutation has two consecutive elements in increasing order do let i be its largest index such that ai < ai+1 //ai+1> ai+2 >... > an find the largest index j such that ai < aj //j i + 1 since ai < ai+1 swap ai with aj //ai+1ai+2... an will remain in decreasing order reverse the order of the elements from ai+1 to an inclusiveadd the new permutation to the list The permutations of {1, 2, 3, 4} generated in lexicographic order. (Readhorizontally.) 1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 IETE 4

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 Q.6 a. Define AVL tree. Construct an AVL tree for the list 3, 6, 5, 1, 2, 4. An AVL tree is a binary search tree in which the balance factor ofevery node, which is defined as the difference between the heights of the node sleft and right subtrees, is either or +1 or 1. b. Explain the idea behind using Gaussian Elimination method to solve a system of linear equation. Solve the following system by Gaussian elimination. x1 + x2 + x3 = 2 2x1 + x2 + x3 = 3 x1 x2 + 3x3 = 8 (8) IETE 5

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 IETE 6 Q.7 a. Apply Warshall s algorithm to find the transitive closure of the digraph defined by the following adjacency matrix: 1 1 1

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 b. Differentiate between Spanning tree and minimum spanning tree (MST). Apply Prim s algorithm to find MST for the following graph. IETE 7

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 Q.8 a. Write algorithm for Counting Sort. Calculate the time efficiency of this algorithm. (8) ComparisonCountingSort(A[..n 1]) //Sorts an array by comparison counting //Input: An array A[..n 1] of orderable elements //Output: Array S[..n 1] of A s elements sorted in nondecreasing order for i to n 1 do Count[i] for i to n 2 do for j i +1 to n 1 do if A[i]<A [j] Count[j] Count[j]+1 else Count[i] Count[i]+1 for i to n 1 do S[Count[i]] A[i] return S What is the time efficiency of this algorithm? It should be quadratic because the algorithm considers all the different pairs of an n-elementarray. More formally, the number of times its basic operation, the comparison A[i]<A [j], is executed is equal to the sum we have encountered several times already: b. Write notes on the following: (4 2) IETE 8

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 (i) P and NP problems (ii) CNF-satisfiability problem Q.9 a. Write Pseudocode for the Bisection method. Explain the strength and weaknesses of this method. (4+2+2) Bisection(f (x), a, b, eps, N) //Implements the bisection method for finding a root of f (x) = //Input: Two real numbers a and b, a < b, // a continuous function f (x) on [a, b], f (a)f (b) <, // an upper bound on the absolute error eps >, // an upper bound on the number of iterations N //Output: An approximate (or exact) value x of a root in (a, b) //or an interval bracketing the root if the iteration number limit is reached n 1 //iteration count while n N do x (a + b)/2 if x a <eps return x fval f (x) if fval = return x if fval f (a)< b x else a x n n + 1 return iteration limit, a, b It always converges to a root whenever we start with an interval whose properties are very easy to check. And it does not use derivatives of the function f (x) as some faster methods do. The principal weakness of the bisection method as a general algorithm for solving equations is its slow rate of convergence compared with other known methods. It is for this reason that the method is rarely used. Also, it cannot be extended to solving more general equations and systems of equations. b. Solve the following instance of the Knapsack problem by the branch and bound algorithm: (8) Item Weight Value Value Weight 1 4 $4 1 2 7 $42 6 3 5 $25 5 4 3 $12 4 IETE 9

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 The Knapsack s capacity w is 1. At the root of the state-space tree, no items have been selected as yet. Hence, both the total weight of the items already selected w and their total value v are equal to. The value of the upper bound computed is $1. Node 1, the left child of the root, represents the subsets that include item 1. The total weight and value of the items already included are 4 and $4, respectively; the value of the upper bound is 4 + (1 4) 6 = $76. Node 2 represents the subsets that do not include item 1. Accordingly, w =, v = $, and ub = + (1 ) 6 = $6. Since node 1 has a larger upper bound thanthe upper bound of node 2, it is more promising for this maximization problem,and we branch from node 1 first. Its children nodes 3 and 4 represent subsetswith item 1 and with and without item 2, respectively. Since the total weight w ofevery subset represented by node 3 exceeds the knapsack s capacity, node 3 canbe terminated immediately. Node 4 has the same values of w and v as its parent;the upper bound ub is equal to 4 + (1 4) 5 = $7. Selecting node 4 over node2 for the next branching, we get nodes 5 and 6 by respectively includingand excluding item 3. The total weights and values as well as the upper bounds forthese nodes are computed in the same way as for the preceding nodes. Branchingfrom node 5 yields node 7, which represents no feasible solutions, and node 8, which represents just a single subset {1, 3} of value $65. The remaining live nodes 2 and 6 have smaller upper-bound values than the value of the solution represented by node 8. Hence, both can be terminated making the subset {1, 3} of node 8 the optimal solution to the problem. IETE 1

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 214 TEXT BOOK I. Introduction to The Design & Analysis of Algorithms, Anany Levitin, Second Edition, Pearson Education, 27 IETE 11