1 / 17

CMP 338

Data Structures and Algorithms I Day 2, 9/1/11. CMP 338. Programming Homework 1. Create a matrix library with the following API Class: Matrix Matrix(int r, int c) Creates a matrix with r rows and c columns Individual entries are random doubles multAdd(Matrix C, Matrix A, Matrix B)

pnoble
Télécharger la présentation

CMP 338

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and Algorithms I Day 2, 9/1/11 CMP 338

  2. Programming Homework 1 Create a matrix library with the following API Class: Matrix Matrix(int r, int c) Creates a matrix with r rows and c columns Individual entries are random doubles multAdd(Matrix C, Matrix A, Matrix B) Throw an exception if matrices are not conformant Otherwise, set C += A*B Note: Real library would have more methods e.g. getters and setters for individual matrix elements

  3. Matrices Implementation: 2-D array of double Multiply-add: for (int i=0; i<M; i++) for (int j=0; j<N; j++) for (int k=0; k<L, k++) c[i][j] += a[i][k]*b[k][j] Analysis: ~ MNL double multiply-adds ~ N3 for square matrices Data structures for sparse matrices

  4. Partition, take 1 int partition(double a[], double p, int lo, int hi) int i = lo; int j = hi while (true) while (a[i++] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

  5. Partition, take 2 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) while (a[++i] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

  6. Partition, take 3 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) while (a[++i] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

  7. Partition, take 4 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi < a.length int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) {a[lo..i-1]<=p<a[i]} while (p < a[--j]) {a[j]<=p<a[j+1..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  8. Partition, take 5 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) if (i == hi) break {a[lo..i-1]<=p<a[i]} while (p < a[--j]) if (j == lo) break {a[j]<=p<a[j+1..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  9. Partition, take 6 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) if (i == hi) break {a[lo..i-1]<=p<a[i] || i==hi && a[lo..hi]<p} while (p < a[--j]) if (j == lo) break {a[j]<=p<a[j+1..hi] || j==lo && p<a[lo..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  10. Partition, take 7 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  11. Partition, take 8 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i] <= p <a[j..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  12. Partition, take 9 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i-1] <= p <a[j+1..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j{a[lo..j] <= p < a[j+1..hi]}

  13. Partition, take 10 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i-1] <= p <a[j+1..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i++, j--) else return j{a[lo..j] <= p < a[j+1..hi]}

  14. Partition, take 10 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) while (a[i] <= p) if (i++ == hi) break while (p < a[j]) if (lo == j++) break if (i<j) swap(a, i++, j--) else return j

  15. Partition, take 1 int partition(double a[], double p, int lo, int hi) int i = lo; int j = hi while (true) while (a[i++] <= p) while (p < a[j++]) if (i<j) swap(a, i, j) else return j

  16. Programming Homework 2 Implement the following API double pivot(double[] a, int lo, int hi) Assert that 0 <= lo <= hi+1 < ||a||+1 Return p where a[i]<=p<=a[j] for some lo<=j,i<=hi int partition(double[] a, double p, int lo, int hi) Assert that 0 <= lo <= hi+1 < ||a||+1 Return j, lo-1 <= j <= hi,such that a[lo..j] <= p < a[j+1..hi] double select(double[] a, int lo, int hi, int k) Assert that 0<= lo <= hi+1 <= ||a||+1 and 0 <= k <= hi-lo Return the smallest double d such that k elements of a[lo..hi] are less than or equal to d

  17. Selection Algorithm Item select(double[] a, int k, int lo, int hi) if (k == 0 && lo == hi) return a[lo] Item p = pivot(a, lo, hi) // a[lo+(lo+hi)/2] int j =partition(a, p, lo, hi) if (k <= j) return select(a, k, lo, j) else return select(a, k-(j+1), j+1, hi)

More Related