1 / 15

Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I

Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I. Table Lookup. You can speed up search if you store items in a table. Assign 1 item per key, for integer keys ranging from 1 to n. To find an item, simply access the index given by its key.

Download Presentation

Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I

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 StructuresCSCI 132, Spring 2014Lecture 32Tables I

  2. Table Lookup • You can speed up search if you store items in a table. • Assign 1 item per key, for integer keys ranging from 1 to n. • To find an item, simply access the index given by its key. no it by if at we in of an 0 1 2 3 4 5 6 7 8 • Table lookup has running time of O(1)

  3. Example: Color Tables

  4. Rectangular Tables Tables are very often in rectangular form with rows and columns. Most programming languages accommodate rectangular tables as 2-D arrays. (0, 0) c s c i f u n ! v e r y (2, 3)

  5. Rectangular Tables in Memory (0, 0) c s c i f u n ! v e r y (2, 3) Row-major order: c s c i f u n ! v e r y row 0 row 1 row 2 Column-major order: c f v s u e c n r i ! y col 0 col 1 col 2 col 3

  6. Computing the index in memory The compiler must be able to convert the index (i, j) of a rectangular table to the correct position in the sequential array in memory. For an m x n array (m rows, n columns): Each row is indexed from 0 to m-1 Each column is indexed from 0 to n - 1 c . . . i f . . . ! v . . . y 0 n-1 n 2n-1 2n row 0 row 1 row 2 Item at (i, j) is at sequential position i * n + j

  7. Access Arrays • Multiplication can be slow. To speed things up, we can compute the beginning index of each row and store it in an auxiliary array, called an access array. access_array 0 n 2n 3n ... 0 1 2 3 ... • To find the position of (i, j), take the value at index i from the access array and add j. (access_array[i] + j) • In general, an access array is an auxiliary array used to find data that is stored elsewhere.

  8. Triangular Tables Some rectangular tables have positions where the entry is always zero. To save memory, we omit these positions from our table. Definitions: A matrix is a table of numbers. A lower triangular matrix is a matrix in which non zero entries are only in positions for which i >= j:

  9. Leaving out the zeros

  10. Calculating Access Array values Number of cells preceding the start of row i: 0 + 1 + 2 + .... + i = i (i + 1) /2 Each entry (i, j) is located at position: j + i (i + 1)/2 One can compute the values of the access array once and then not again for the entire program. You do not need multiplication to compute these values: 0, 0 + 1, 1 + 2, (1 + 2) + 3, ....

  11. Jagged Tables

  12. Inverted Tables

  13. Functions and Tables • A function starts with an argument and computes a value. • A table starts with an index and looks up a value. • A function assigns to each element in set A (the domain) a single element from set B (the codomain). • A table assigns to each index in set A (the index set) a single value from set B (the base type).

  14. The Table ADT A table with index set I and base type T is a function from I into T together with the following operations. Standard operations: 1. Table access: Evaluate the function at any index in I. 2. Table assignment: Modify the function by changing its value at a specified index in I to the new value specified in the assignment. Additional operations: 3. Creation: Set up a new function from I to T . 4. Clearing: Remove all elements from the index set I , so the remaining domain is empty. 5. Insertion: Adjoin a new element x to the index set I and define a corresponding value of the function at x. 6. Deletion: Delete an element x from the index set I and restrict the function to the resulting smaller domain.

  15. Lists vs. Tables Lists are inherently sequential; Tables are not. List retrieval requires search O(lgn); Tables do not: O(1) Traversal is natural for a list, not for a table.

More Related