User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,532 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1394 | Replies: 5
Reply
Join Date: Apr 2004
Location: Dhaka, Bangladesh
Posts: 344
Reputation: Asif_NSU is on a distinguished road 
Rep Power: 5
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

What does it mean?

  #1  
Nov 25th, 2004
This is probably a stupid question, but i dont get the answer. Plz help me understand.

Question:
How could a linked list and a hash table be combined to allow someone to run through the list from item to item while still maintaining the ability to access an individual element in O(1) time?

Solution:
Create a hash table where the elements of the array are pointers to linked list objects. You have your linked list as normal, but when you add an element to the linked list, "you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element".

I dont understand what you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element. means.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: What does it mean?

  #2  
Nov 25th, 2004
OK, I assume you know what a linked list is else it gets cumbersome

A hashtable is a special type of map where the items are indexed on the hashcode of the key.
The hashcode is an integer calculated over the item being stored, in this case your data.
The hashtable contract specifies that items with identical value have identical hashcodes as well.
In other words if you have 2 objects a and b where a == b, then a.hashCode() == b.hashCode().
This facilitates fast lookup. Internally the items are indexed on hashcode, when you try to find an object by the key the hashcode of that key is calculated. That provides an index into the datastorage where a group of items stored with that hashcode are kept.
If the hashing algorithm is perfect there will always be only 1 item there. If not you will at least have a far smaller number of items to compare with your key than the entire collection.

What you're said to do is store a key into your stored data into a hashtable.
As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list.

So you get both a List (fast sequential lookup) and a Map (fast random lookup) of the same data for practically the price of one.
List Data Hashtable
1 *O1 O1.hashCode()
2 *O2 O2.hashCode()
3 *O3 O2.hashCode()
4 *O4 O2.hashCode()
5 *O5 O2.hashCode()

Now if you want to look up an item and you know the key (from which you can calculate the hashcode) you can do it through the hashtable.
If on the other hand you want to look up elements by index or iteration you can use the list.
Reply With Quote  
Join Date: Apr 2004
Location: Dhaka, Bangladesh
Posts: 344
Reputation: Asif_NSU is on a distinguished road 
Rep Power: 5
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: What does it mean?

  #3  
Nov 26th, 2004
Suppose i have a linked list of say integers. So whenever i add a new integer to that linkedlist i also:
1. calculate the hash code for that integer
2. add a pointer to that integer into the hashtable(the position of the pointer is determined by the hash code)
Is that what it means?
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: What does it mean?

  #4  
Nov 26th, 2004
correct.
In the case of an integer it's rather trivial as the hashcode will be the integer itself but the concept is sound.
Reply With Quote  
Join Date: Oct 2005
Posts: 1
Reputation: diogeneb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
diogeneb diogeneb is offline Offline
Newbie Poster

Re: What does it mean?

  #5  
Oct 14th, 2005
I still do not understand. Could you please elucidate what you mean by:-

"What you're said to do is store a key into your stored data into a hashtable. As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list"

Assuming the hashtable is represented by the array Arr. If data items X, Y & Z hash to the same value (say I), then Arr[i] would contain a pointer to the linked list containing X,Y,Z. Now, suppose someone wants to search for Z in the hashtable. Z hashes to I. Then a sequential lookup of the linked list contained in Arr[i] is done, and Z is found. It takes O(3). How can we make it O(1)?

Thanks
Dio
Reply With Quote  
Join Date: Sep 2004
Posts: 6,059
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: What does it mean?

  #6  
Oct 14th, 2005
>Then a sequential lookup of the linked list contained in Arr[i] is done, and Z is found. It takes O(3).
That's still O(1) because it's a constant time. But if the hash function is good, the extra cost of a sequential search is negligible since the lists will all be very short. The only way to get guaranteed and exact O(1) performance as you're thinking of it is to use a perfect hash, which isn't always possible.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 7:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC