1 / 22

Practical Guide to Algorithms and Data Structures for Programs

Dive into the world of algorithms and data structures to master the art of problem-solving and information storage. This course covers the fundamentals and applications of these key concepts. Learn from the best to excel in your programming journey!

julieward
Download Presentation

Practical Guide to Algorithms and Data Structures for Programs

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 1, 8/30/11 CMP 338

  2. The Course Instructor: Bowen Alpern Email: alpern@acm.org Office hour: GI 137-I Tuesday 4-5pm (and by appointment) (poor speller, nominal aphasia, limited teaching experience) Texts: Algorithms, fourth edition, Sedgewick and Wayne Booksite: algs4.cs.princeton.edu Format: T, Th 6-7:40pm, room 333 Gillet Lecture (100 min) Grade policy (tentative) Program assignments 40% Exams 40% (4 in class, 5% each, final 20%) Homework 20%

  3. CMP 338 “Algorithms + Data Structures = Programs” - Niklaus Wirth Algorithm – a method for solving a problem Data Structure – a method for storing information Why study algorithms (and data structures)?

  4. I'm thinking of a number ...

  5. I'm thinking of a number ... 0 ≤ x < 1024 = 210

  6. I'm thinking of a number ... 0 ≤ x < 1024 = 210 Queries: is x ≤ y ? (for any integer y)

  7. I'm thinking of a number ... 0 ≤ x < 1024 = 210 Queries: is x ≤ y ? (for any integer y) Algorithm: lo = 0; hi =1023 while (lo < hi) if (x ≤ lo + (lo+hi)/2) then hi = lo + (lo+hi)/2 else lo = lo + (lo+hi)/2 + 1 return lo // or hi

  8. Analysis If 0 ≤ x < N = 2n(n = log2 N ≡ lg N) x has n bits 695 = 1010110111b Each query resolves 1 bit Why? Each query consists of a single comparison Therefore: Guessing one of N consecutive integers can be done with ~ lg N comparisons

  9. So What?

  10. So What? Same algorithm tests membership in a sorted list

  11. So What? Same algorithm tests membership in a sorted list boolean member(Item i, List<Item> l) lo = 0; hi = l.length -1 while (lo < hi) int m = lo + (lo+hi)/2 if (0 ≤ i.compareTo(l.get(m))) then hi = m else lo = m + 1 return i.equals(l.get(lo))

  12. Search Binary search Work to look up 1 of N keys ~ lg N comparisons Can we do better? Kabbalah Assign a numeric value to each letter of an alphabet Tries Work to look up key ~ ||key|| Hashing Work to look up key ~ c (average case)

  13. Polynomials Polynomials over x with integer coefficients Operations: +, -, ●, /, %, evaluate, … First implementation: array of ints p[i] – coefficient of xi Good for dense polynomials Second implementation: linked list of nodes int coefficient Nomial link Good for sparse polynomials

  14. 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

  15. Selection Select the kth smallest entry in a collection Application: sort the √N smallest entries Select the √Nth smallest Partition on k = √N Sort the partition with smallest entries Analysis: Select ??? Partition ~ N (next slide) Sort ~ c √N log N << N Can this be done in linear time?

  16. Partitioning int partition(Item[] a, Item p, int lo, int hi) int i = lo-1, j = hi while (true) while (less(a[++i],p) if (i == hi) break; while (less(p, a[--j]) if (j == lo) break; swap(a, i, j) swap(a, lo, j) return j; // a[lo..j] < p & p < a[j+1..hi]

  17. Analysis Partitioning N Items ~ 2N compares Average case: Two partitions are approximately equal in size Worst case: One partition is empty (or has only 1 Item)

  18. Selection Algorithm Item select(Item[] a, int k, int lo, int hi) if (k == 0 && lo == hi) return a[lo] Item p = choosePivot(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)

  19. Selection Analysis Average case: T(N) < 2N + T(N/2 + c√N) ~ c' N Worst case: T(N) < 2N + T(N-1) ~ c N2 ~ c k N How can worst case performance be improved? Answer: choose a better pivot!

  20. Choosing a Good Pivot Item pivot(Item[] a, int lo, int hi) Item[] b = new Item[N/5] for (int j=0; j<N/5; j++) b[j] = median5(a, j*5) Item p = select(b, N/10, 0, (N/5)-1) return p Item median5(Item[a], int lo) sort(a, lo, lo+4) return a[lo+2]

  21. Analysis Computing b ~ c N Pivot : Tp(N) < c' N + Ts(N/5) Select: Ts(N) < c'' N + Ts(N/5) + Ts(7N/10) ~ c''' N Why Ts(7N/10)? Largest partition size <= 70% N Pivot is less than half of b Each element of b is less than 2 elements of a Pivot is less than 30% of a Similarly, pivot is greater than 30% of a

  22. Homework 1) Create a Matrix library Constructor create a random N x M matrix void mutiplyAdd does c += a*b for conformant matrices a, b, and c 2) Code partition() Get the details right 3) Code select() Get the details right 4) Code pivot() Get the details right

More Related