Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I

2 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. 0 1 2 3 4 5 6 7 8 no it by if at we in of an Table lookup has running time of O(1)

3 3 Example: Color Tables

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

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

6 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 row 0row 1row 2 Item at (i, j) is at sequential position i * n + j 0n-1n2n2n-1

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

8 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 9 Leaving out the zeros

10 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 11 Jagged Tables

12 12 Inverted Tables

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


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I."

Similar presentations


Ads by Google