Assignment 1 (concept): Solutions

Similar documents
PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Outline and Reading. Analysis of Algorithms 1

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

Analysis of Algorithms

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

Algorithms and Data Structures

Data Structures Lecture 8

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

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

UNIT 1 ANALYSIS OF ALGORITHMS

Comparison of x with an entry in the array

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

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

CS:3330 (22c:31) Algorithms

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

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

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

Scientific Computing. Algorithm Analysis

CS126 Final Exam Review

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

Elementary maths for GMT. Algorithm analysis Part I

Programming II (CS300)

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Analysis of Algorithms Part I: Analyzing a pseudo-code

Complexity of Algorithms

Complexity Analysis of an Algorithm

What is an algorithm?

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Data Structures and Algorithms. Part 2

Data Structures and Algorithms Key to Homework 1

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

Data Structures and Algorithms Chapter 2

Chapter 2: Complexity Analysis

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Data Structures and Algorithms CSE 465

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Lecture 2: Algorithm Analysis

ANALYSIS OF ALGORITHMS

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe Sandra Batista

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

Data structure and algorithm in Python

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

CSE Winter 2015 Quiz 1 Solutions

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Introduction to Data Structure

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Computer Algorithms. Introduction to Algorithm

Introduction to Computers & Programming

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Complexity of Algorithms. Andreas Klappenecker

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

UNIT 1 BASICS OF AN ALGORITHM

L6,7: Time & Space Complexity

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

MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala

Analysis of Algorithm. Chapter 2

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Time Analysis of Sorting and Searching Algorithms

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe

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

Analysis of Algorithms

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

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

CSCE f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Algorithm Analysis and Design

Lecture 5: Running Time Evaluation

How fast is an algorithm?

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

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

Analysis of Algorithms. CS 1037a Topic 13

Round 1: Basics of algorithm analysis and data structures

MATH 423 Linear Algebra II Lecture 17: Reduced row echelon form (continued). Determinant of a matrix.

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

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.

Review for Midterm Exam

Algorithmic Analysis. Go go Big O(h)!

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

Choice of C++ as Language

CS 161 Summer 2009 Homework #1 Sample Solutions

Asymptotic Analysis of Algorithms

Introduction to the Analysis of Algorithms. Algorithm

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Theory and Algorithms Introduction: insertion sort, merge sort

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

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

Algorithm Analysis. October 12, CMPE 250 Algorithm Analysis October 12, / 66

CPS 616 TRANSFORM-AND-CONQUER 7-1

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

Data Structures and Algorithms

Lecture 7. Transform-and-Conquer

CS240 Fall Mike Lam, Professor. Algorithm Analysis

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

CS S-02 Algorithm Analysis 1

Transcription:

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 by asymptotic growth rate in non-decreasing order: ( 3 )n, 64 1, n 3, 0.0001n, 10000n, logn, logn, nlogn, n n, 1000, n, n logn, n, logn, n 100, 4 n, logn 3, n n, n 3 logn Answer: 64 1, 1000, logn, logn, logn 3, n, logn, 10000n, nlogn, 0.0001n, n logn, n 3, n 3 logn, n 100, ( 3 )n, n, n n, 4 n, n n. Remark: In the above ranking, if a function f(n) preceeds another function g(n) then f(n) O(g(n)) holds. Consequently, there are several valid answers. Indeed, several of the above ranked functions f(n), g(n) satisfy f(n) Θ(g(n)), which implies that both f(n) O(g(n)) and g(n) O(f(n)) hold. Note: (1) logn = logn, logn 3 = 3logn () Grouping these functions into the following classes can help to clarify their orders. (a) Constant: 64 1, 1000 (b) Logarithmic: logn, logn, logn 3 (c) Linear: n, logn, 10000n (d) nlogn (e) Polynomial: 0.0001n, n 3, n 100 (f) n logn (g) n 3 logn (h) Exponential: ( 3 )n, n, n n, 4 n (i) n n Observe that the functions in the class (e) are not equivalent for the f(n) Θ(g(n)) relation. The same observation is true for the class (h).. (15%) Use the definition of Big-Oh to prove that 0.001n 3 1000n logn 100n+5 is O(n 3 ). To show that 0.001n 3 1000n logn 100n+5 is O(n 3 ), we must find constants c > 0 and n 0 1 such that 0.001n 3 1000n logn 100n+5 cn 3, n n 0. (1) 1

Inequality (1) is equivalent to the inequality below. cn 3 0.001n 3 +1000n logn+100n 5 0 () Inequality () must be true if the following inequality holds (where 1000n logn and 100n are dropped since they are positive, and 5 is multiplied by n 3 ). cn 3 0.001n 3 5n 3 0 (3) Inequality (3) is equivalent to (c 0.001 5)n 3 0, which is true n n 0 when c = 5.001 and n 0 = 1. Now, we have found c = 5.001 and n 0 = 1 such that 0.001n 3 1000n logn 100n+5 cn 3 for all n n 0. Therefore, we have proved that 0.001n 3 1000n logn 100n+5 is O(n 3 ). Remark: The pair of c and n 0 is not unique. For instance, when c = 1 and n 0 =, Inequality (1) always holds for n n 0. 3. (15%) Use the definition of Big-Oh to prove that n 1+0.001 is not O(n). We prove that n 1+0.001 is not O(n) by contradiction. Suppose that n 1+0.001 is O(n), which means that we can find c > 0 and n 0 1 such that n 1+0.001 cn, n n 0. (4) By dividing both sides of the inequality of (4) by n (n 1) we obtain the following: c n 0.001 (5) Inequality (5) can not be true since c must be a constant but n 0.001 is unbounded. In fact, as soon as n > e 1000log(c) we have c < n 0.001. This is a contradiction with the assumption that we can find such a constant c. Therefore, n 1+0.001 is not O(n). 4. (10%)UsethedefinitionofBig-Ohtoprovethatiff(n)isO(g(n)),thenaf(n)isO(g(n)), for any positive constant a. From the fact that f(n) is O(g(n)) we can find constants c f > 0 and n 0f 1 such that f(n) c f g(n), n n 0f. (6) By multiplying both sides of the inequality of (7) by a (a > 0), we get af(n) ac f g(n), n n 0f. (7) Let c = ac f and n 0 = n 0f. We show that we can get constants c > 0 and n 0 1 such that af(n) cg(n) for all n n 0. By the definition of Big-Oh, this proves that if f(n) is O(g(n)), then af(n) is O(g(n)), for any positive constant a.

5. (5%) We want to know how many students are taking both CS10 and CS11 this term. Let A and B be the class lists of CS10 and CS11. Each of A and B consists of unique student IDs of the corresponding class. To keep it simple, we assume that the two classes have the same number of students, denoted by n. 5.1 Write an algorithm in pseudocode to count the number of students who are taking both CS10 and CS11 this term. 5. Compute the worst case running time T(n) of your algorithm with respect to the class size n. 5.3 Give the best Big-Oh complexity characterization of T(n). Solution 1: 5.1 Algorithm countcommon(a, B, n) Input: Two integer arrays A and B with both size of n Output: Number of common elements in A and B e 0 //number of common elements for i 0 to n 1 do for j 1 to n 1 do if B[j] = A[i] then e = e+1 break return e 5. The worst case occurs when there are no common elements in A and B. In such case, every element in A needs to be compared with every element in B. This algorithm involves a nested for loop. We analyze the inner-most for loop first. In each iteration of the inner for loop, only a number of constant c operations are performed (mainly one comparison). The number of iterations of the inner for loop is n. Thus, the total number of operations performed in this loop is cn. As for the outer (or first) for loop, the number of iterations is again n. In each iteration of the outer for loop, it performs the work of the inner loop. Therefore, the total work done by the outer for loop is n cn = cn. Consequently T(n) = cn +c where c is the number of operations for initializing e and returning e at the end. 5.3 T(n) = cn +c is O(n ). (Proof is straightforward.) 3

Solution : 5.1 Algorithm countcommon(a, B, n) Input: Two integer arrays A and B with both size of n Output: Number of common elements in A and B Sort B by merge-sort or quick-sort e 0 //number of common elements for i 0 to n 1 do b binarysearch(a[i], B) if b null then e = e+1 return e 5. The worst case happens when there are no common elements in A and B. First, sorting the elements in B can be done in cnlogn by either merge-sort or quick-sort. Then, we can search in B for each element of A via binary-search, which takes n c logn = c nlogn since the cost for each binary search is c logn. In total T(n) = cnlogn+c nlogn. 5.3 T(n) = cnlogn+c nlogn is O(nlogn). Remark: If both of the input arrays A and B are already sorted, one can achieve O(n) for Algorithm countcommon by comparing the elements of A and B in sequence, akin to the merge operation in merge-sort. 6. (5%) In the mathematical discipline of linear algebra, a matrix of the form l 1,1 0 l,1 l,. L = l 3,1 l 3,.......... l n,1 l n,... l n,n 1 l n,n (8) is called lower triangular matrix. For example, the following matrix is lower triangular. 1 0 0 8 0 (9) 4 9 7 Provided that l i,i 0 for 1 i n, a matrix equation in the form Lx = b, where x = [x 1,...,x n ] T and b = [b 1,...,b n ] T, is very easy to solve by an iterative process 4

called forward substitution, reported as follows. The matrix equation Lx = b can be written as a system of linear equations l 1,1 x 1 = b 1 l,1 x 1 + l, x = b...... l n,1 x 1 + l n, x + + l n,n x n = b n (10) Observe that the first equation (l 1,1 x 1 = b 1 ) only involves x 1, and thus one can solve for x 1 directly. The second equation only involves x 1 and x, and thus can be solved once one substitutes in the already solved value for x 1. Continuing in this way, the k-th equation only involves x 1,...,x k, and one can solve for x k using the previously solved values for x 1,...,x k 1. The resulting formulas are: x 1 = b 1, x = b l,1 x 1,, x n = b n n 1 i=1 l n,ix i (11) l 1,1 l n,n l, Please refer to http://en.wikipedia.org/wiki/triangular matrix for a general description about triangular matrices and the forward substitution process. The following algorithm, ForwardSubstitution 1, is a straight-forward realization of the forward substitution process reported above. Algorithm ForwardSubstitution 1(L, b) Input: -dimensional array L[1,...,n][1,...,n] encoding the triangular matrix in Expression (1) such that L[i][j] = l i,j ; An array b[1,...,n] where b[i] = b i. Output: An array x[1,...,n] such that x[i] = x i as given in Expressions (11). x[1] b[1]/l[1][1] for i to n do v 0 for j 1 to i 1 do v v +L[i][j] x[j] x[i] (b[i] v)/l[i][i] 6.1 Prove that the time complexity of Algorithm ForwardSubstitution 1 is O(n ). For this algorithm, there are no difference between best case and worst case. It uses a nested for loop. We analyze the inner-most for loop first. In every iteration of this loop a constant number c 1 of operations is performed and the loop is repeated for i 1 times. Thus, the total number of operations performed by this loop is c 1 (i 1). As for the first for loop, in every iteration it performs a constant number of operations c to reset v to 0, c 1 (i 1) operations by the second for block, and a constant number of operations c 3 for calculating the 5

value of x i. The first loop changes the values of i from to n, thus the total number of operations performed in this loop is n (c +c 1 (i 1)+c 3 ). i= In total, the running time T(n) of Algorithm ForwardSubstitution 1 is n (c +c 1 (i 1)+c 3 )+c 0, i= where c 0 is the number of operations for calculating x 1. T(n) can be further simplified as follows. T(n) = c (n 1)+c 1 (1++3+ +n 1)+c 3 (n 1)+c 0 = c (n 1)+c 1 ( n(n+1) 1)+c 3 (n 1)+c 0 = c 1 n +( c 1 +c +c 3 )n+c 0 c 1 c c 3 Therefore, Algorithm ForwardSubstitution 1 runs in O(n ). 6. (optional, 5% bonus) Can Algorithm ForwardSubstitution 1 be improved to achieve O(n) time complexity? Adjust your reasoning regarding your answer. Answer: If none of the coefficients of the triangular matrix L is zero, then the cost for computing x 1,x,...,x n lies in Θ(n ). Indeed, under this assumption, the account (performed in the solution of the previous question) for the number of additions, subtractions, multiplications and divisions is not over-estimated. In fact, this amount of arithmetic operations is a sharp estimate. Therefore, under the hypothesis that L[i][j] 0 for 1 i n, 1 j i, Algorithm ForwardSubstitution 1 runs in Θ(n ) arithmetic operations. Another argument is to observe that the number of coefficients in the triangular matrix L is n(n+1). Therefore, we need Θ(n ) read operations for accessing all the entries of the input data set. Considering the amount of data that a given algorithm needs to read and write is often used as an argument for proving that this algorithm requires at least a given number of elementary operations. 6