Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

12 Problems on Asymptotic Notation - Assignment 1 | CSCI 212, Assignments of Computer Science

Material Type: Assignment; Professor: Choi; Class: Design & Analysis of Algorithm; Subject: Computer Science; University: George Washington University; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-ona
koofers-user-ona 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download 12 Problems on Asymptotic Notation - Assignment 1 | CSCI 212 and more Assignments Computer Science in PDF only on Docsity! George Washington University Department of Computer Science Csci 212 - Homework 1 Due date: February 17, 2009 1.Asymptotic notations. (a) Solve the recurrence: T (n) = 1/n + T (n− 1), where T (0) = 0. (b) Prove or disprove: (b1) 2n+1 = O(2n), (b2) 22n = O(2n) (c) Let P be a problem. The worst-case time complexity of P is O(n2). The worst-case time complexity of P is also Ω(n log n). Let A be an algorithm that solves P . Which statements of the following statements are consistent with this information about the complexity of P? (c1) A has worst-case time complexity O(n2). (c2) A has worst-case time complexity O(n3/2). (c3) A has worst-case time complexity O(n). (c4) A has worst-case time complexity θ(n2). (c5) A has worst-case time complexity θ(n3). 2. What value is returned by each of the following functions? Express your answer as a function of n. Give the worst-case running time using big-Oh notation. function f(n) 1. r := 0 2. for i := 1 to n do 3. for j := 1 to n do 4. for k := 1 to n do 5. r := r + 1 6. return(r) function g(n) 1. r := 0 2. for i := 1 to n do 3. for j := 1 to i do 4. for k := j to i + j do 5. for l := 1 to i + j − k do 6. r := r + 1 7. return(r) 3. Suppose you are given an array of n element in increasing order. (a) Describe an O(n) time algorithm to find two numbers from the list that add up to zero, or report there is no such pair. (b) Describe an O(n2) time algorithm to find three numbers from the list that add up to zero, or report that there is no such triple. 4. Let A and B be sets of positive integers. We want to compute the set C of elements that appear more than once in the union (i.e., a ∈ C if a appears either in A and B, or more than once in A or B). (a) Assume that A and B are unsorted. Give an O(n log n) time algorithm for the problem. (b) Assume that A and B are sorted. Give an O(n) time algorithm for the problem. 5. Suppose you are given a set S of n positive integers in the range 1 to k where k is much smaller than n. (a) Assume that the value of k is known. Give an algorithm to sort the numbers in S that runs in O(n log k) time. (b) Assume that the value of k is not known. Give an algorithm to sort the numbers in S that runs in O(n log k) time. 6. Suppose you are given a sorted sequence of distinct integers {aa, a2, · · · , an}. Give an O(log n) time algorithm to determine whether there exists an index i such that ai = i. For example, in {−10,−3, 3.5.7}, a3 = 3. In {2, 3, 4, 7, 8, 9}, there is no such i. 7. Let X = {x1, x2, · · · , xn} be a sequence of arbitrary numbers. Give an O(n) time algorithm to find the subsequence of consecutive elements xi, xi+1, · · · , xj whose sum is maximum over all consecutive subsequences. 8. Consider the O(n) time Select algorithm discussed in class and modify it in such a way that the group size is 9 instead of 5. Prove that the time complexity of this algorithm is also O(n). 9. You are given a list of n integers in the range 1 to 999. Design an O(n) time algorithm to rearrange elements of the array “in place” so that all 1-digit numbers precede all 2-digit numbers, and all 2-digit numbers precede all 3-digit numbers. 10. We are given an array A with n elements and a number C. Assume that the sum of the elements in A is larger than C. We would like to compute the smallest number of elements from A whose sum is at least C. (For example, if A = [8, 3, 9, 2, 7, 1, 5] and C = 18 then the answer is 3.) Give an O(n) time algorithm for this problem. (HINT: use the linear-time SELECT algorithm) 11. You are given a list A = [a1, a2, · · · , an] of n distinct numbers in an arbitrary order. You are supposed to find two numbers ai and aj from A such that i < j and aj − ai is maximum over all possible such pairs. Give an O(n) time algorithm.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved