ENGI 4892: Data Structures Assignment 2 SOLUTIONS

Size: px
Start display at page:

Download "ENGI 4892: Data Structures Assignment 2 SOLUTIONS"

Transcription

1 ENGI 4892: Data Structures Assignment 2 SOLUTIONS Due May 30 in class at 1:00 Total marks: 50 Notes: You will lose marks for giving only the final answer for any question on this assignment. Some steps of justification are required for all questions. Marks will be deducted for lack of neatness and for lack of clarity in your explanations. 1. [6] Determine the asymptotic complexity of the following functions. You must provide justification for each step. Valid justifications include: The definition of big-o The 6 big-o facts, including fact 2a (summation of a constant number of terms) and the observation that an order k polynomial is O(n k ). Ordering of the common complexity classes (e.g. if f(n) = O(n) then f(n) is also O(2 n )). You should provide answers in the smallest and simplest possible terms. (a) f(n) = 103(n 3 2) = 103n = O(n 3 ) (Complexity of polynomial) (b) f(n) = log 2 n 2 + ln n + 21 = 2 log 2 n + ln n + 21 = O(lg n) + O(lg n) + 21 (Fact 6) = O(lg n) + O(lg n) + O(1) (Fact 5) = O(lg n) + O(1) (Fact 2) = O(lg n) + O(lg n) (O(1) is O(lg n)) = O(lg n) (Fact 2) (c) f(n) = 4n 2 + n lg n + n = 4n 2 + n lg n + n = O(n 2 ) + O(n lg n) + O(n) (Fact 5) = O(n 2 ) + O(n 2 ) + O(n 2 ) (n lg n and n are both O(n 2 )) = O(n 2 ) (Fact 2a) 1

2 2. [15] Prove the following statements. You must provide justification for each step. You may utilize the technique described on slide 10 of the first set of notes on complexity, or any other mathematically valid argument. Note that k is a constant. (a) n ik is O(n k+1 ) Since i n for this summation, we can say the following, We can simplify the RHS, Thus, n k = n k i k n n k 1 = n k n = n k+1 (b) i k n k+1 This satisfies the definition of big-o notation for constants c = N = 1. n k (n+1) 2k is O(n k ) We must find constants c and N such that, Divide both sides by n k n k (n + 1) 2k cnk for n N 1 (n + 1) 2k c Since (n + 1) 2k is monotonically increasing with n we know the LHS will always be less than 1. Thus, the definition of big-o is satisfied for all n by choosing c = 1 and N = k 2 2k (c) 2 (lg n)0.3 is O(n 2 ) 2 (lg n)0.3 2 lg n = n Therefore 2 (lg n)0.3 is O(n). Since n is O(n 2 ) we can say that 2 (lg n)0.3 is also O(n 2 ) by Fact 1. (d) n! is O(n n ) n! = n n n n n = n n In the expression above we have replaced all terms with n. This creates a function g(n) such that n! g(n). Since we have n such terms this function g(n) = n n. Thus, the definition of big-o is satisfied directly for c = 1 and N = 1. 2

3 (e) If f 1 (n) is O(g 1 (n)) and f 2 (n) is O(g 2 (n)) then f 1 (n)f 2 (n) is O(g 1 (n)g 2 (n)). To prove this statement we may assume the following (the antecedent): f 1 (n) c 1 g 1 (n) for n N 1 f 2 (n) c 2 g 2 (n) for n N 2 We can unify the conditions under which these two statements are true by choosing the largest value of N i for both. f 1 (n) c 1 g 1 (n) for n max(n 1, N 2 ) f 2 (n) c 2 g 2 (n) for n max(n 1, N 2 ) Since the above two inequalities hold for all the same values of n we can combine them. f 1 (n)f 2 (n) c 1 c 2 g 1 g 2 (n) for n max(n 1, N 2 ) This satisfies the definition of big-o allowing us to state that f 1 (n)f 2 (n) is O(g 1 (n)g 2 (n)). 3

4 3. [9] Determine the exact number of assignment statements executed for the following three code snippets as a function of n. Also, give the complexity of each in big-o notation. (a) for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i][j] = b[i][j] + c[i][j]; assignments: 2n 2 + 2n + 1, asymptotic complexity: O(n 2 ) (b) for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = a[i][j] = 0; k < n; k++) a[i][j] += b[i][k] * c[k][j]; assignments: 2n 3 + 3n 2 + 2n + 1, asymptotic complexity: O(n 3 ) (c) for (i = 0; i < n - 1; i++) for (j = i+1; j < n; j++) { tmp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = tmp; } assignments: 2n 2 1, asymptotic complexity: O(n 2 ) 4

5 4. [8] Determine the final value of count as a function of n for the following three code snippets. (a) [2] for (count = 0, i = 1; i <= n; i++) for (j = 1; j <= n; j++) count++; (b) [2] We need only count the number of times that count++ is executed. The outer loop executes n times. For each execution of the outer loop, the inner is also executed n times. Therefore, count++ is executed n 2 times. for (count = 0, i = 1; i <= n; i++) for (j = 1; j <= i; j++) count++; Here the outer loop executes n times, but the inner loop executes i times. We need to sum up the number of executions of the inner loop (which is also the number of times count++ is executed). n(n + 1) i = = n 2 /2 + n/2 2 (c) [4] To analyze the code below you may assume that n is a power of 2. Also, you may find it useful to utilize the formula for a geometric series: k=m for (count = 0, i = 1; i <= n; i *= 2) for (j = 1; j <= i; j++) count++; ar k = a(rm r n+1 ) 1 r The straightforward approach used in part (b) will not work here because i doubles for each execution of the outer loop. The progression of i is 1, 2, 4, 8,... n. We can also describe this progression as 2 0, 2 1, 2 2, 2 3,... 2 m = n. We introduce a quantity called k which represents the increasing exponent values in this progression (i.e. k follows the sequence 0, 1, 2,...m). The point of introducing k is to have a quantity that increases by one at each step. This will allow us to use the familiar summation identities. Just like in part (b) we need to sum the number of executions of the inner loop, which is i. In this case i = 2 k and we sum over all values of k. The only remaining question is the final value of k. This is defined as m. Since 2 m = n, we know that m = log 2 n. log 2 n k=0 This is a geometric series with a = 1 and r = 2. We utilize the given formula, simplify and obtain 2n 1. 2 k 5

6 5. [10] Recall the code snippet discussed in class for finding the length of the longest increasing sequence within an array. The following is a variation on this code which differs in several ways. Firstly, the variable i will be used to select different possibilities for the end of an increasing subsequence rather than the beginning (k is used to indicate the beginning). Also, both i and k decrease within their respective loops, as opposed to increasing. In particular, i will decrease so as to jump one past the beginning of the increasing subsequence just found. This makes this code more efficient than the version presented in class. 1. for (i = n-1, length = 1; i >= 0;) { 2. for (k = i; k >= 1 && a[k-1] < a[k]; k--) 3. ; 4. int curlength = i - k + 1; 5. if (length < curlength) 6. length = curlength; 7. i = k - 1; 8. } In answering the following questions you may assume that n > 1. (a) [4] Determine the exact number of assignment statements executed by this code as a function of n when the input array is sorted in increasing order. The first two initialization statements in line 1 will always be executed. We then enter the body of the outer loop where we have an additional assignment in the initialization of the inner for loop. The condition a[k-1] < a[k] will always be true. Hence, the inner loop will decrement k until k >= 1 is no longer true. This will require i decrement operations on the first (and only) execution of the outer loop when i = n 1. Considering only those assignment statements mentioned so far the total count would be 3 + n 1 = n + 2. We have two additional assignments in the body of the outer loop that will be executed (lines 4 and 7). The assignment statement on line 6 will also be executed since the if condition is satisfied on the first pass. Thus our total becomes as follows: n = n + 5 Since the inner loop has been completed, we know that k = 0. On line 7 i will be set to 1. Thus, the outer loop condition will not be satisfied and the program will terminate. Thus, the total number of assignments is n + 5. (b) [4] Determine the exact number of assignment statements executed by this code as a function of n when the input array is sorted in decreasing order (or if all entries are the same). In this case there are no increasing subsequences greater than length 1. Thus, the condition a[k-1] < a[k] will always be false. This means that the inner loop contributes only one assignment statement (the initialization) for every execution of the outer loop. Also, the condition on line 5 will never be satisfied since we will never find a subsequence with length greater than 1. Therefore we have two initial assignment statements on line 1 and then the body of the loop is entered. The loop will be entered n times and on each iteration there will be three assignment statements executed: 6

7 k=i int curlength = i - k + 1; i = k - 1; Hence the total is, 2 + 3n (c) [1] What is the worst-case value of the input array for large values of n. The number of assignment statements executed for a decreasing order of input array elements (or all elements being equal) is greatest. As shown above, this number 3n+2 is clearly greater than for an increasing order where the total is n + 5 for n 2. If there are some increasing subsequences (e.g. [10, 1, 2, 3, 2, 1]) then the performance will be better (i.e. fewer statements executed) than for a pure decreasing order since line 7 essentially allows us to skip over a subsequence after finding it. 6. [3] One algorithm able to solve the subset-sum problem works as follows. Each possible combination of entries from the input set (treated as a list) is denoted as a binary number. For example, for the input set {1, 2, 3, 4, 2} we would utilize 5-digit binary numbers and the number would indicate a combination involving the 2 and the 2 (upon finding such a combination the algorithm would stop and return a positive answer). For an input set of size n, give the asymptotic complexity of this algorithm and justify your answer. This algorithm will test all combinations of these numbers. We will assume that testing whether the sum of n numbers is 0 has a cost of O(n) (see note below). So the question then becomes how many combinations are there? Since there is a unique binary number for every combination, we simply have to count the number of binary numbers. With n bits we can specify 2 n numbers. Thus, the overall complexity of this algorithm is O(n2 n ). Note: In the analysis above we have not considered the precision of these numbers. A more careful analysis would introduce another variable giving the number of bits required to represent each number of the input set. 7

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer. Test 1 SOLUTIONS June 10, 2010 Total marks: 34 Name: Student #: Answer each question in the space provided or on the back of a page with an indication of where to find the answer. There are 4 questions

More information

CS126 Final Exam Review

CS126 Final Exam Review CS126 Final Exam Review Fall 2007 1 Asymptotic Analysis (Big-O) Definition. f(n) is O(g(n)) if there exists constants c, n 0 > 0 such that f(n) c g(n) n n 0 We have not formed any theorems dealing with

More information

CS S-02 Algorithm Analysis 1

CS S-02 Algorithm Analysis 1 CS245-2008S-02 Algorithm Analysis 1 02-0: Algorithm Analysis When is algorithm A better than algorithm B? 02-1: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster 02-2:

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

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

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

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-02 Algorithm Analysis David Galles Department of Computer Science University of San Francisco 02-0: Algorithm Analysis When is algorithm A better than algorithm

More information

Introduction to the Analysis of Algorithms. Algorithm

Introduction to the Analysis of Algorithms. Algorithm Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational

More information

Math 501 Solutions to Homework Assignment 10

Math 501 Solutions to Homework Assignment 10 Department of Mathematics Discrete Mathematics Math 501 Solutions to Homework Assignment 10 Section 7.3 Additional Exercises 1. int CountValuesLessThanT(int a[], int n, int T) { int count = 0; for (int

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

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

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem (Thursday, Jan 29, 1998) Read: Chapter 1 in CLR. Analyzing Algorithms: In order to design good algorithms, we must first agree the criteria for measuring

More information

Introduction to Analysis of Algorithms

Introduction to Analysis of Algorithms Introduction to Analysis of Algorithms Analysis of Algorithms To determine how efficient an algorithm is we compute the amount of time that the algorithm needs to solve a problem. Given two algorithms

More information

4.1 Real-world Measurement Versus Mathematical Models

4.1 Real-world Measurement Versus Mathematical Models Chapter 4 Complexity To analyze the correctness of a computer program, we reasoned about the flow of variable values throughout that program and verified logical properties using this information. In addition

More information

Algorithms and complexity analysis

Algorithms and complexity analysis Chapter 2 Algorithms and complexity analysis 2.1 Relationship between data structures and algorithms The primary objective of programming is to efficiently process the input to generate the desired output.

More information

Analysis of Algorithms Part I: Analyzing a pseudo-code

Analysis of Algorithms Part I: Analyzing a pseudo-code Analysis of Algorithms Part I: Analyzing a pseudo-code Introduction Pseudo-code representation of an algorithm Analyzing algorithms Measuring the running time and memory size of an algorithm Calculating

More information

Elementary maths for GMT. Algorithm analysis Part I

Elementary maths for GMT. Algorithm analysis Part I Elementary maths for GMT Algorithm analysis Part I Algorithms An algorithm is a step-by-step procedure for solving a problem in a finite amount of time Most algorithms transform input objects into output

More information

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity Algorithm Analysis Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity ITCS 2214:Data Structures 1 Mathematical Fundamentals Measuring Algorithm Efficiency Empirical

More information

Choice of C++ as Language

Choice of C++ as Language EECS 281: Data Structures and Algorithms Principles of Algorithm Analysis Choice of C++ as Language All algorithms implemented in this book are in C++, but principles are language independent That is,

More information

Assignment 1 (concept): Solutions

Assignment 1 (concept): Solutions CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions

More information

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation Comp2711 S1 2006 Correctness Oheads 1 Efficiency Issues Comp2711 S1 2006 Correctness Oheads 2 Run Times An implementation may be correct with respect to the Specification Pre- and Post-condition, but nevertheless

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

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

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

Lecture Notes on Sorting

Lecture Notes on Sorting Lecture Notes on Sorting 15-122: Principles of Imperative Computation Frank Pfenning Lecture 4 September 2, 2010 1 Introduction Algorithms and data structures can be evaluated along a number of dimensions,

More information

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept 9/10/2018 Algorithms & Data Structures Analysis of Algorithms Siyuan Jiang, Sept. 2018 1 Email me if the office door is closed Siyuan Jiang, Sept. 2018 2 Grades have been emailed github.com/cosc311/assignment01-userid

More information

Lecture 2: Algorithm Analysis

Lecture 2: Algorithm Analysis ECE4050/CSC5050 Algorithms and Data Structures Lecture 2: Algorithm Analysis 1 Mathematical Background Logarithms Summations Recursion Induction Proofs Recurrence Relations 2 2 Logarithm Definition: 3

More information

csci 210: Data Structures Program Analysis

csci 210: Data Structures Program Analysis csci 210: Data Structures Program Analysis Summary Summary analysis of algorithms asymptotic analysis and notation big-o big-omega big-theta commonly used functions discrete math refresher Analysis of

More information

What is a Computer Algorithm?

What is a Computer Algorithm? What is a Computer Algorithm? A computer algorithm is a detailed step-by-step method for solving a problem by using a computer. Problem-Solving (Science and Engineering) Analysis How does it work? Breaking

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

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and. Big-Oh Notation Formal Definitions A function is in (upper bound) iff there exist positive constants k and n 0 such that for all. A function is in (lower bound) iff there exist positive constants k and

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017 8/3/07 Analysis Introduction to Analysis Model of Analysis Mathematical Preliminaries for Analysis Set Notation Asymptotic Analysis What is an algorithm? An algorithm is any well-defined computational

More information

Final Exam in Algorithms and Data Structures 1 (1DL210)

Final Exam in Algorithms and Data Structures 1 (1DL210) Final Exam in Algorithms and Data Structures 1 (1DL210) Department of Information Technology Uppsala University February 0th, 2012 Lecturers: Parosh Aziz Abdulla, Jonathan Cederberg and Jari Stenman Location:

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 21, 2016 Lecture 8: Sorting Assignment 3: Due Sunday Sept 25 @ 10pm Remember: javac Xlint:all & checkstyle *.java Solutions should be independently written!

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

Comparison of x with an entry in the array

Comparison of x with an entry in the array 1. Basic operations in algorithm An algorithm to solve a particular task employs some set of basic operations. When we estimate the amount of work done by an algorithm we usually do not consider all the

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

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

Introduction to Algorithms and Complexity Theory

Introduction to Algorithms and Complexity Theory Introduction to Algorithms and Complexity Theory Iris Cong UCLA Math Circle - HS II Example 1. Algorithm to find the maximum of a list of N positive integers (call this list L 0, L N-1 ): initialize accumulator

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

CS 261 Data Structures. Big-Oh Analysis: A Review

CS 261 Data Structures. Big-Oh Analysis: A Review CS 261 Data Structures Big-Oh Analysis: A Review Big-Oh: Purpose How can we characterize the runtime or space usage of an algorithm? We want a method that: doesn t depend upon hardware used (e.g., PC,

More information

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs Big-Oh 1 asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs

More information

CMSC Theory of Algorithms Second Midterm

CMSC Theory of Algorithms Second Midterm NAME (please PRINT in large letters): SECTION: 01 02 (circle one) CMSC 27200 Theory of Algorithms Second Midterm 02-26-2015 The exam is closed book. Do not use notes. The use of ELECTRONIC DEVICES is strictly

More information

1 Dynamic Programming

1 Dynamic Programming Recitation 13 Dynamic Programming Parallel and Sequential Data Structures and Algorithms, 15-210 (Spring 2013) April 17, 2013 1 Dynamic Programming Dynamic programming is a technique to avoid needless

More information

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Date of Performance:... Actual Date of Completion:... Expected Date of Completion:... ----------------------------------------------------------------------------------------------------------------

More information

The Running Time of Programs

The Running Time of Programs The Running Time of Programs The 90 10 Rule Many programs exhibit the property that most of their running time is spent in a small fraction of the source code. There is an informal rule that states 90%

More information

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

More information

Analysis of Algorithms. 5-Dec-16

Analysis of Algorithms. 5-Dec-16 Analysis of Algorithms 5-Dec-16 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing

More information

Data Structure and Algorithm Midterm Reference Solution TA

Data Structure and Algorithm Midterm Reference Solution TA Data Structure and Algorithm Midterm Reference Solution TA email: dsa1@csie.ntu.edu.tw Problem 1. To prove log 2 n! = Θ(n log n), it suffices to show N N, c 1, c 2 > 0 such that c 1 n ln n ln n! c 2 n

More information

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II) Why study algorithms? CS 561, Lecture 1 Jared Saia University of New Mexico Seven years of College down the toilet - John Belushi in Animal House Q: Can I get a programming job without knowing something

More information

Analysis of Algorithms

Analysis of Algorithms ITP21 - Foundations of IT 1 Analysis of Algorithms Analysis of algorithms Analysis of algorithms is concerned with quantifying the efficiency of algorithms. The analysis may consider a variety of situations:

More information

Lecture 5 Sorting Arrays

Lecture 5 Sorting Arrays Lecture 5 Sorting Arrays 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons We begin this lecture by discussing how to compare running times of functions in an abstract,

More information

Analysis of algorithms

Analysis of algorithms Analysis of algorithms Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing a

More information

CSE Winter 2015 Quiz 1 Solutions

CSE Winter 2015 Quiz 1 Solutions CSE 101 - Winter 2015 Quiz 1 Solutions January 12, 2015 1. What is the maximum possible number of vertices in a binary tree of height h? The height of a binary tree is the length of the longest path from

More information

https://cs.uiowa.edu/resources/events Searching an array Let R(N) be the running time to search for an integer in an unsorted array. Can we find an f(n) such that R N O(f N )? Searching an array Let R(N)

More information

S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100

S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100 Total No. of Questions 12] [Total No. of Printed Pages 7 [4062]-204 S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100

More information

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS 1 Reference books: The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie Programming in C (3rd Edition) by Stephen G. Kochan. Data

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 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001 CPSC 211, Sections 201 203: Data Structures and Implementations, Honors Final Exam May 4, 2001 Name: Section: Instructions: 1. This is a closed book exam. Do not use any notes or books. Do not confer with

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient? CS706 Intro to Object Oriented Dev II - Spring 04 Announcements Week 4 Program due Two Weeks! Lab 3-4 required completion this week First web-cat submission Material Evaluation of efficiency Time Analysis

More information

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1 CSE 1 Complexity Analysis Program Efficiency [Sections 12.1-12., 12., 12.9] Count number of instructions executed by program on inputs of a given size Express run time as a function of the input size Assume

More information

Computer Algorithms. Introduction to Algorithm

Computer Algorithms. Introduction to Algorithm Computer Algorithms Introduction to Algorithm CISC 4080 Yanjun Li 1 What is Algorithm? An Algorithm is a sequence of well-defined computational steps that transform the input into the output. These steps

More information

Searching, Sorting. Arizona State University 1

Searching, Sorting. Arizona State University 1 Searching, Sorting CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 9 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials. Exam 1 practice problems Remember to also pactice: Homework, quizzes, class examples, slides, reading materials. P1 (MC) For all the questions below (except for the True or False questions), the answer

More information

Part II. Graph Theory. Year

Part II. Graph Theory. Year Part II Year 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005 2017 53 Paper 3, Section II 15H Define the Ramsey numbers R(s, t) for integers s, t 2. Show that R(s, t) exists for all s,

More information

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 2 Solutions Quiz 2 Solutions Do not open this quiz booklet

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

Outline and Reading. Analysis of Algorithms 1

Outline and Reading. Analysis of Algorithms 1 Outline and Reading Algorithms Running time ( 3.1) Pseudo-code ( 3.2) Counting primitive operations ( 3.4) Asymptotic notation ( 3.4.1) Asymptotic analysis ( 3.4.2) Case study ( 3.4.3) Analysis of Algorithms

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

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

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1 Algorithm Analysis Spring Semester 2007 Programming and Data Structure 1 What is an algorithm? A clearly specifiable set of instructions to solve a problem Given a problem decide that the algorithm is

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/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims Lecture 10: Asymptotic Complexity and What Makes a Good Algorithm? Suppose you have two possible algorithms or

More information

Section 1: True / False (2 points each, 30 pts total)

Section 1: True / False (2 points each, 30 pts total) Section 1: True / False (2 points each, 30 pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered

More information

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions U.C. Berkeley CS70 : Algorithms Midterm 2 Solutions Lecturers: Sanjam Garg and Prasad aghavra March 20, 207 Midterm 2 Solutions. (0 points) True/False Clearly put your answers in the answer box in front

More information

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I Algorithm Analysis College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Order Analysis Judging the Efficiency/Speed of an Algorithm Thus far, we ve looked

More information

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal: Assignment 1 Stefano Guerra October 11, 2016 1 Problem 1 Describe a recursive algorithm to reconstruct an arbitrary binary tree, given its preorder and inorder node sequences as input. First, recall that

More information

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r] CSE 101 Final Exam Topics: Order, Recurrence Relations, Analyzing Programs, Divide-and-Conquer, Back-tracking, Dynamic Programming, Greedy Algorithms and Correctness Proofs, Data Structures (Heap, Binary

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

II (Sorting and) Order Statistics

II (Sorting and) Order Statistics II (Sorting and) Order Statistics Heapsort Quicksort Sorting in Linear Time Medians and Order Statistics 8 Sorting in Linear Time The sorting algorithms introduced thus far are comparison sorts Any comparison

More information

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for

More information

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search 10/5/2016 CSE373: Data Structures and Algorithms Asymptotic Analysis (Big O,, and ) Steve Tanimoto Autumn 2016 This lecture material represents the work of multiple instructors at the University of Washington.

More information

Assertions & Verification & Example Loop Invariants Example Exam Questions

Assertions & Verification & Example Loop Invariants Example Exam Questions 2014 November 27 1. Assertions & Verification & Example Loop Invariants Example Exam Questions 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer

More information

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003 Recursion COMS W1007 Introduction to Computer Science Christopher Conway 26 June 2003 The Fibonacci Sequence The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, 21, 34,... We can calculate the nth Fibonacci

More information

Time Complexity for Loops

Time Complexity for Loops Time Complexity for Loops CSE 2320 Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington 1/18/2018 1 Summary Simple runtime problems Counting instructions Detailed Big-Oh briefly

More information

Pseudo code of algorithms are to be read by.

Pseudo code of algorithms are to be read by. Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.

More information

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

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

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

More information

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Sorting. There exist sorting algorithms which have shown to be more efficient in practice. Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics

More information

EECS Sample Midterm Exam

EECS Sample Midterm Exam EECS 477 - Sample Midterm Exam Name - UMich ID # - DO NOT OPEN THE EXAM BOOKLET UNTIL YOU ARE INSTRUCTED TO BEGIN! Honor Code: I have neither given nor received any help on this exam. Signature: You must

More information

Assertions & Verification Example Exam Questions

Assertions & Verification Example Exam Questions 2009 November 23 Assertions & Verification Example Exam Questions 1. 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify

More information

CpSc 421 Final Solutions

CpSc 421 Final Solutions CpSc 421 Final Solutions Do any eight of the ten problems below. If you attempt more than eight problems, please indicate which ones to grade (otherwise we will make a random choice). This allows you to

More information

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2, Exercises Exercises 1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2, a) f(n + 1) = f(n) + 2. b) f(n + 1) = 3f(n). c) f(n + 1) = 2f(n). d) f(n + 1) = f(n)2

More information

Complexity of Algorithms. Andreas Klappenecker

Complexity of Algorithms. Andreas Klappenecker Complexity of Algorithms Andreas Klappenecker Example Fibonacci The sequence of Fibonacci numbers is defined as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... F n 1 + F n 2 if n>1 F n = 1 if n =1 0 if n =0 Fibonacci

More information