150 likes | 274 Views
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.
E N D
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)
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)
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
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
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.
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:
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, ....
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).
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.
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.