14

I was given as homework the Introduction to Algorithms exercise 11.1-3 which goes as follows:

Suggest how to implement a direct-access table in which the keys of stored elements do not need to be distinct and the elements can have satellite data. All three dictionary operations (Insert, Delete and Search) should run in O(1) time. Don't forget that Delete takes as an argument a pointer to an object to be deleted, not a key.

Well, Insert is no problem, as it simply means creating a linked list at the appropriate place in the table (if it doesn't already exist) and adding the element to it. Search, which is given a key, can return any of the elements that match the key, so it simply means we need to return the head of the matching list in the table.

My problem is with Delete operation. If I modify the object to add to it a pointer to its node in the linked list, then I can delete in O(1), but I'm not sure I'm allowed to change the object. Is there a way for doing this without changing the given object?

roottraveller
  • 7,942
  • 7
  • 60
  • 65
CS n00b
  • 149
  • 1
  • 3

5 Answers5

6

Is this a question from the Cormen book? As I understand it, from reading the previous paragraphs in that book, the object that you store in the direct access table is 'your' object. So you can, as you suggest, store pointers to doubly-linked lists in the table with each list element having a pointer to the user's object. Then, the dictionary SEARCH operation returns a list element and the user must use a further step to get at his object. Likewise the DELETE operation takes a pointer to a list element.

Does that make sense? I don't want to spoil your homework!

Peter Hull
  • 6,683
  • 4
  • 39
  • 48
3

We can use a double linked -list at every indices of direct-address table. Slot j will contain a pointer to the head of the list, which contain all the elements with key-value j and if there are no such element slot j contains NIL. INSERT-inserting x at the head of the list will take just O(1) time. SEARCH- It can return any element that matches the given key and thus returning the head of the list will just take O(1) time. DELETE- As we have used double linked-list, we can delete an element in O(1) time using pointer to previous and next nodes.

1

I think you can make use of the satellite data to be mapped as a secondary key. Then it's a kind of 2-level hash table. Concerned with the DELETE operation, at first we use the primary key. And when there are duplicate primary keys, we use secondary keys to get the object. Therefore the total time is still O(1).

Romi
  • 11
  • 1
0

The most important part.."implement a direct-access table in which the keys of stored elements do not need to be distinct" and " dictionary operation in O(1) time.

Insertion is also not possible in O(1) time if elements are equal(as Q says elements need not to be distinct).

For delete part you have to take keys as well as objects to reach to a particular object and assume an key out of satellite data too, to reach over a particular object.

I think only above 2 ideas make sense for O(1) time.

Atiq
  • 490
  • 11
  • 28
0

Hash tables will work for you up to a certain point. Once you start having collsions, then it becomes O(1+k/n) where k is the number of keys and n is your table size. If you do a scheduled hash resize and re-hash, then you might be able to get away with this. Don't know if that would affect your efficiency rating since it doesn't happen during insertion, deletion or search.

Deletion by not changing the object can be achieved by simply setting the hash reference pointer to null. No direct change to the object is made, but changes to the object container are made.

I think for most of the restrictions that you are giving, a hash table can be implemented in certain ways to get around them. There is more information at http://en.wikipedia.org/wiki/Hash_table#Performance_analysis on how to implement a hash table.

Sakamoto Kazuma
  • 2,573
  • 7
  • 34
  • 75