23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Describe, <strong>in</strong> pseudo-code a method for multiply<strong>in</strong>g an n × m matrix A <strong>and</strong> an m<br />

× p matrix B. Recall that the product C = AB is def<strong>in</strong>ed so that C[i] [j] =<br />

C-4.21<br />

What is the runn<strong>in</strong>g time of your method?<br />

Suppose each row of an n × n array A consists of 1's <strong>and</strong> 0's such that, <strong>in</strong> any<br />

row i of A all the 1's come before any 0's. Also suppose that the number of 1's <strong>in</strong><br />

row i is at least the number <strong>in</strong> row i + 1, for i = 0,1,…, n − 2. Assum<strong>in</strong>g A is<br />

already <strong>in</strong> memory, describe a method runn<strong>in</strong>g <strong>in</strong> O(n) time (not O(n 2 )) for<br />

count<strong>in</strong>g the number of 1's <strong>in</strong> A.<br />

C-4.22<br />

Describe a recursive method for comput<strong>in</strong>g the nth Harmonic number,<br />

Projects<br />

P-4.1<br />

Implement prefixAverages1 <strong>and</strong> prefixAverages2 from Section<br />

4.2.5, <strong>and</strong> perform an experimental analysis of their runn<strong>in</strong>g times. Visualize<br />

their runn<strong>in</strong>g times as a function of the <strong>in</strong>put size with a log-log chart.<br />

P-4.2<br />

Perform a careful experimental analysis that compares the relative runn<strong>in</strong>g<br />

times of the methods shown <strong>in</strong> Code Fragments 4.5.<br />

Chapter Notes<br />

The big-Oh notation has prompted several comments about its proper use [16, 47,<br />

61]. Knuth [62, 61] def<strong>in</strong>es it us<strong>in</strong>g the notation f(n) = O(g(n)), but says this<br />

"equality" is only "one way." We have chosen to take a more st<strong>and</strong>ard view of<br />

equality <strong>and</strong> view the big-Oh notation as a set, follow<strong>in</strong>g Brassard [16]. The reader<br />

<strong>in</strong>terested <strong>in</strong> study<strong>in</strong>g average-case analysis is referred to the book chapter by Vitter<br />

<strong>and</strong> Flajolet [97]. We found the story about Archimedes <strong>in</strong> [77]. For some additional<br />

mathematical tools, please refer to Appendix A.<br />

257

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!