Chapter 11 - Direct Addressing


Document last modified: 

OVERVIEW

Scenario:
• Maintain a dynamic set.
• Each element has a key drawn from a universe U = {0, 1, . . . ,m − 1} where m isn’t too large.
• Unique keys, no two elements have the same key.

Represent by a direct-address table, or array, T [0 . . .m − 1]:
• Each slot, or position, corresponds to a key in U.
• If there’s an element x with key k, then T [k] contains a pointer to x.
• Otherwise, T [k] is empty, represented by NIL.

Specialized Universe of Keys

Example implementing a dynamic set by a direct-address table

T = direct-address table

U = { 0, 1, .., 9 } each key corresponds to an index of the table (1-to-1 mapping)

K = { 2, 3, 5, 8 } Í U actual keys determines the slots in the table that contain pointers to elements.

Slots with /  contain NIL

Question 11.1

The load factor a of a direct-addressed data structure is often given as: a = n / m

n = number of keys used, K = { 2, 3, 5, 8 }

m = number of keys in universe, U = { 0, 1, .., 9 }

What is the load (utilization) of the example?

What is the minimum load factor?

What is the maximum load factor?

 

Implementation

Direct-Address-Search (T: Direct_Address_Table, k: U) : Node_Ptr 

 --  post: if k is defined in T, then return the Node_Ptr address to the node 
 --                containing the key and the satellite data
 --          if k is not defined in T, then return NIL

           return T[k]

Direct-Address-Insert (T: Direct_Address_Table, x: Node_Ptr)
  --  pre:  T[ x.key ] = NIL
  --  post: T[ x.key ] = x

           T[ x.key ] ¬ x

Direct-Address-Delete (T: Direct_Address_Table, x: Node_Ptr)
 --  post: T[ x.key ] = NIL

           T[ x.key ] ¬ NIL

Question 11.2

Diagram a data structure for direct-addressing to implement a dictionary of dates translating from historical dates to English representation, for example:

1492 ® "fourteen ninety two"

Which of above is the key? satellite data?

What does T, direct address table, hold?

Give a call to search for 1492 ® "fourteen ninety two".

Give a call to insert 1492 ® "fourteen ninety two".

Give a call to delete 1492 ® "fourteen ninety two".

Java Implementation

class Year {
     public String english;
     public int key; 
     Year( int k, String e ) { english=e; key = k; }
}
 
public class Direct {

    static void INSERT( Year T[], Year x ) {
         T[ x.key ] = x;
    }
 
    static void DELETE( Year T[], Year x ) {
         T[ x.key ] = null;
    }
 
    static Year SEARCH( Year T[], int k ) {
         return T[ k ];
    }

     public static void main(String a[]) {
          Year T[] = new Year[10000];
          Year x;
 
          INSERT( T, new Year( 1492, "fourteen ninety two") );
          INSERT( T, new Year( 2011, "two oh one one") );
 
          x = SEARCH( T, 2011 );
 
          System.out.println( x.english );
 
          DELETE( T, x );                    
    } 
}

Question 11.2

From above Java, what is stored at each index of T?

The natural numbers are totally ordered by the relation. Are the keys totally ordered?

Is the key field of Year redundant?

 

Performance

Question 11.3

Describe a procedure to find the maximum key used by a dynamic set S represented by a direct-address table T of length m.

What is the best and worst case performance?

Describe a procedure to find the maximum element (satellite data) of dynamic set S represented by a direct-address table T of length m.

What is the best and worst case performance?
 

When Direct Addressing Does Not Work (Well)

Question 11.4

Give one other situation when direct addressing as implemented here does not work well. Suggest a solution.

What is U for the Java program below?

What is set K of keys actually stored for the Java program below?

What is the maximum load factor?

What are the minimum storage requirements for the direct-addressing?

class Year {
     public String english;
     public int key; 
     Year( int k, String e ) { english=e; key = k; }
}
 
public class Direct {

    static void INSERT( Year T[], Year x ) {
         T[ x.key ] = x;
    }
 
    static void DELETE( Year T[], Year x ) {
         T[ x.key ] = null;
    }
 
    static Year SEARCH( Year T[], int k ) {
         return T[ k ];
    }

     public static void main(String a[]) {
          Year T[] = new Year[10000];
          Year x;
 
          INSERT( T, new Year( 1492, "fourteen ninety two") );
          INSERT( T, new Year( 2011, "two oh one one") );
 
          x = SEARCH( T, 2011 );
 
          System.out.println( x.english );
 
          DELETE( T, x );                    
    } 
}