

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1 CS1110 23 April 2009 Ragged arrays Reading for today : sec. 9.3. Reading for next time : sec. 2.5 pp. 83-86 and sec. 4.5 pp. 156-162. We posted the skeleton for class BigInt from prelim III to the course website; you can use it to try re-doing P3 on a computer for study purposes. (Again: we recommend testing your sample-exam solutions at a computer; this includes the ādraw foldersā questions.)
r 5 7 7
r r
b holds the name of a one-dimensional array object of b.length elements; its elements are 1D arrays. b[i] holds the name of a 1D array of int s of length b[i].length.
java.util.Arrays.deepToString recursively creates an appropriate String. 9 6 4 5 7 7 4 Ragged arrays: rows have different lengths b a0 a r 17 13 19
r 28 95
r r
int [][] b; Declare variable b of type int [][] b= new int [2][] Create a 1-D array of length 2 and store its name in b. Its elements have type int [] (and start as null ). b[0]= new int [] {17, 13, 19}; Create int array, store its name in b[0]. b[1]= new int [] {28, 95}; Create int array, store its name in b[1]. 1 2 1
5 Application: ātriangularā data One example: array dist in which dist[i][j] would be the same as dist[j][i]. Another: Pascalās triangle (represents a function with interesting symmetries) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 The first and last entries of each row are 1. Each other entry is the sum of the two entries above it. Row r has r+1 values. (Coloring the odd numbers starts to look like Sierpinskiās triangleā¦)
Pascalās Triangle (^1) 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Entry p[i][j], entry j of row i, is the number of ways j elements can be chosen from a set of size i! p[i][j] = āi choose jā =
i j recursive formula (computed via dynamic programming ): for 0 < i < j, p[i][j] = p[iā1][jā1] + p[iā1][j] 7 Pascalās Triangle (^1) 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Binomial theorem: Row r gives the coefficients of (x + y) r (x + y)^2 = 1 x^2 + 2 xy + 1 y^2 (x + y)^3 = 1 x^3 + 3 x^2 y + 3 xy^2 + 1 y^3 (x + y)r^ = ā (k choose r) xkyr-k 0 ⤠k ⤠r
Large collections of association data abound, but often, many possible associations have the default value. Netflix data : (movie, rater, score): 480K Ć 18K = 8.6B possible scores to track, but there are only (!) 100M actual scores. GroupLens data (freely distributed by U. Minn): the small set has 943 Ć1682= 1.5M possibilities, but only 100K actual scores. Main idea: DONāT store an int array of length 1682 for each movie; store a rater-sorted array of score objects corresponding to just the raters who scored that movie (avg. length: 59!). Another very useful technique (among many more substantive ones; take more CS courses!): map the movie/rater names to ints, b/c they can be meaningful array indices. Application: representation of (irregular) sparse data