13 views

Uploaded by Andrew Adkins

ses

- Questions on Artificial Intelligence
- Visual Prolog Version 5.x
- Data Structure
- Recursive Median Sieve
- Java Collections Framework
- MELJUN CORTES JAVA_Collections
- big-o
- BCA 403 (File & Data Structure).docx
- 1.2Array String Recursion
- Tutorial 6 Sol
- Markov Chains
- Daa Ppt 28th Jan
- [7] robustH2
- 80c5eab4f5a54214ec14084cbbce2b3a 4 Dynprog 1 Changeproblem (1)
- Python Advanced_ Towers of Hanoi
- Important Doc
- Decidability_Complexity.theory_Syntactic.sugars .pdf
- algo8
- Wide Area Computing
- Zacharov - KdV_A Completely Integrable Hamiltonian System

You are on page 1of 4

Exercise 3.1-3 (page 50) from CLRS.

Exercise 3.1-4 (page 50) from CLRS.

1. (11 points) Asymptotic Growth

Rank the following functions by increasing order of growth; that is, nd an arrangement

g

1

, g

2

, . . . , g

11

of the functions satisfying g

1

= O(g

2

), g

2

= O(g

3

), . . . , g

10

= O(g

11

).

Partition your list into equivalence classes such that f(n) and g(n) are in the same

class if and only if f(n) = (g(n)). All the logs are in base 2.

n

100

, 3

n

, n

100

,

1/n, 2

2n

, 10

100

n,

3

n

, 1/5, 4

n

,

nlog n, log(n!).

2. (19 points) Suppose you are given an array A[1..n] A[1..n] of sorted integers that has

been circularly shifted k positions to the right. For example, [35, 42, 5, 15, 27, 29]

is a sorted array that has been circularly shifted k = 2 positions, while [27, 29, 35,

42, 5, 15] has been shifted k = 4 positions. We can obviously nd the largest element

in A in O(n) time. Describe an O(log n) algorithm based on the divide-and-conquer

strategy to nd the largest element in A. (You need to show that your algorithms time

complexity is O(log n))

Let n = len(alist).

(a) (6 points) What is the runtime of the iterative version in terms of n, and why?

Be sure to state a recurrence relation and solve it.

(b) (8 points) What is the runtime of the recursive version in terms of n, and why?

Be sure to state a recurrence relation and solve it.

(c) (5 points) Explain how you might x the recursive version so that it has the

same asymptotic running time as the iterative version (but is still recursive).

1

3. (30 points) Set Intersection

Python has a built in set data structure. A set is a collection of elements without

repetition.

In an interactive Python session, type the following to create an empty set:

s = set()

To nd out what operations are available on sets, type:

dir(s)

Some fundamental operations include add, remove, and contains and len .

Note that contains and len are more commonly called with the syntax

element in set and len(set). All four of these operations run in constant time i.e.

O(1) time.

For this problem, we will be analyzing the runtime of s.intersection(t) that takes

two sets, s and t, and returns a new set with all the elements that occur in both s and

t. We will then use intersection in a new version of the Document Distance code

from the rst two lectures.

(a) (5 points) Using notation, make a conjecture for the asymptotic running time

of s.intersection(t) in terms of the sizes of the sets: |s| and |t|. Justify your

conjecture.

HINT: Think about the fundamental operations above.

(b) (10 points) Determine experimentally the running time of s.intersection(t),

by running it with dierent sized sets. Fill in the following chart. Include in your

PDF submission a snippet of code that determines one of the entries in the chart.

Note: there are a number of ways to time code. You can use the timeit mod-

ule (see http://www.diveintopython.org/performance tuning/timeit.html

for a good description of how to use it). Alternatively, if you have ipython in-

stalled (see http://ipython.scipy.org), you can use their builtin timeit com-

mand which is more user friendly.

time in s |s| = 10

3

|s| = 10

4

|s| = 10

5

|s| = 10

6

|t| = 10

3

|t| = 10

4

|t| = 10

5

|t| = 10

6

(c) (5 points) Give an approximate formula for asymptotic running time of

s.intersection(t) based on your experiments. How does this compare with

your conjecture in part (a)? If the results dier from your conjecture, make a new

conjecture about the algorithm used.

(d) (10 points) In the Document Distance problem from the rst two lectures, we

compared two documents by counting the words in each, treating theses counts

2

as vectors, and computing the angle between these two vectors. For this problem,

we will change the Document Distance code to use a new metric. Now, we will

only care about words that show up in both documents, and we will ignore the

contributions of words that only show up in one document.

Download ps1.py, docdist7.py, and test-ps1.py from the class website.

docdist7.py is mostly the same as docdist6.py seen in class, however it does not

implement vector angle or inner product; instead, it imports those functions

from ps1.py. Currently, ps1.py contains code copied straight from docdist6.py,

but you will need to modify this code to implement the new metric.

Modify inner product to take a third argument, domain, which will be a set

containing the words in both texts. Modify the code so that it only increases

sum if the word is in domain.

Dont forget to change the documentation string at the top.

Modify vector angle so that it creates sets of the words in both L1 and L2,

takes their intersection, and uses that intersection when calling inner product.

Again, dont forget to change the docstring at the top.

Run test-ps1.py to make sure your modied code works. The same test suite

will be run when you submit ps1.py to the class website.

Does your code take signicantly longer with the new metric? Why or why not?

Submit ps1.py on the class website. All code submitted for this class will be

checked for accuracy, asymptotic eciency, and clarity.

3

Iterative Version:

def binarySearch(alist, item):

first = 0

last = len(alist)-1

found = False

while first<=last and not found:

midpoint = (first + last)/2

if alist[midpoint] == item:

found = True

else:

if item < alist[midpoint]:

last = midpoint-1

else:

first = midpoint+1

return found

Recursive Version:

def binarySearch(alist, item):

if len(alist) == 0:

return False

else:

midpoint = len(alist)/2

if alist[midpoint]==item:

return True

else:

if item<alist[midpoint]:

return binarySearch(alist[:midpoint],item)

else:

return binarySearch(alist[midpoint+1:],item)

4

- Questions on Artificial IntelligenceUploaded byUtkarsh Bajpai
- Visual Prolog Version 5.xUploaded byeditali
- Data StructureUploaded byProf.Manikandan
- Recursive Median SieveUploaded bysat_rins
- Java Collections FrameworkUploaded byJorge Taquito Valdivia
- MELJUN CORTES JAVA_CollectionsUploaded byMELJUN CORTES, MBA,MPA
- big-oUploaded byprasadkvss
- BCA 403 (File & Data Structure).docxUploaded byneerukumari1
- Tutorial 6 SolUploaded byAli Alabid
- 1.2Array String RecursionUploaded byhimanshuchawla654
- Markov ChainsUploaded byindhu
- Daa Ppt 28th JanUploaded byccatty
- [7] robustH2Uploaded byHieu LE
- 80c5eab4f5a54214ec14084cbbce2b3a 4 Dynprog 1 Changeproblem (1)Uploaded byPriyank Paliwal
- Python Advanced_ Towers of HanoiUploaded byPedro Elias Romero Nieto
- Important DocUploaded byLalitSisodia
- Decidability_Complexity.theory_Syntactic.sugars .pdfUploaded byVlad-Andrei Ursu
- algo8Uploaded byBijal Patel
- Wide Area ComputingUploaded byAlexandros Valavanis
- Zacharov - KdV_A Completely Integrable Hamiltonian SystemUploaded bycorrain_matteo
- Response to the Reviewer 1.docUploaded byjudehemanth
- Lecture-9-CS210-2012 (1).pptxUploaded byMoazzam Hussain
- AOA Intractable ProblemsUploaded by9chand3
- NP SectionUploaded byspy_crash
- Larsson+SjobergUploaded bymlh_4040
- NP CompleteUploaded bypreethi
- d Slab ManualUploaded bybabigfchfbtr4e53w
- mergesortUploaded bynelly
- ArraysUploaded byPhilip Joseph
- Branch and Redeuce (Lecture 02)Uploaded byTamim Hossain

- final-ProjectUploaded byRavi Joshi
- Project Management Bo Tonnquist ENGUploaded byIgor Brodin
- GOOGLE SketchUp - Film and Stage InstructionsUploaded byXaverius Nanda
- Authentication and Key Agreement in 3GPP NetworksUploaded byCS & IT
- Dalta Wayz AirlineUploaded bybilal
- Moment ConnectionUploaded bynitin400
- Lich v. US RubberUploaded byjoycebaylon
- McKeldin v. Reliance Standard Life, 4th Cir. (2007)Uploaded byScribd Government Docs
- IEEE Conference A4 Format MSwordUploaded byLarissa Nunes da Costa
- How to become a mobile testerUploaded byAlmir Mehanovic
- SSI Basic System Notes (PDF)Uploaded byBambang Soegeng
- description: tags: 2007-217Uploaded byanon-787011
- Advantages of Zinc-free Hydraulic OilsUploaded byAnonymous F2TxS3Yoa
- Alcatel U3 - Alcatel U3 User GuideUploaded byoswaldomarangoni5295
- Aspen Tutorial Unit 1Uploaded byvsraochemical1979
- Border Client Agreement Form.pdfUploaded bya
- Anderson Kathleen 1976 ThailandUploaded bythe missions network
- Nanofabrication TechnologiesUploaded byedgar17286
- SPD Brochure Masters in Transportation and Car DesignUploaded byArpitPatel
- USD vs INRUploaded bySunny Khandelwal
- -comprehensive-mathematics-jee-advanced-2014.pdfUploaded byMohit Mude
- German Guide for Silva Schneider DVD 710PUploaded byMari Pop
- Ptmail m0617 Sms Smart Money Secrets a Quick Start GuideUploaded bypkj009
- ACFrOgAcx1pkGnafFUi1FKrXI7yzvxZH6LV0lIyF6cw0J3FbrsHP2b-NYO73e2LaBhOmFxGvxzaFNvXbPgDGJF5UBeurFmt30P_X0jcQwe9DTG_JvlHo_azRk5U6jik=Uploaded byKUNAL
- PearlExpertTitan Man v9.0Uploaded byvanderhenst_michiel
- MaglevUploaded byPrathamesh Birwatkar
- GBREB Boston Massachusetts Standard Lease Fixed Term RH201Uploaded byAnonymous T4ZbwqS2A
- 1. IR meaning, scope and importance.pptxUploaded byUsman Maqsood.
- Weblogic Iapp DgUploaded byAbery Au
- "Cooling Down" - air conditioning technology in the US vs. global warming - for IET Eng & Tech Sept 2008Uploaded byLaurie Wiegler