Beruflich Dokumente
Kultur Dokumente
Objectives
After completing this chapter, you will be able to:
Implement a set type and a dictionary type using
lists
Explain how hashing can help a programmer
achieve constant access time to unordered
collections
Explain strategies for resolving collisions during
hashing, such as linear probing, quadratic probing,
and bucket/chaining
Objectives (continued)
After completing this chapter, you will be able to:
(continued)
Use a hashing strategy to implement a set type and
a dictionary type
Use a binary search tree to implement a sorted set
type and a sorted dictionary type
Using Sets
A set is a collection of items in no particular order
Most typical operations:
Applications of Sets
Sets have many applications in the area of data
processing
Example: In database management, answer to
query that contains conjunction of two keys could be
constructed from intersection of sets of items
associated with those keys
10
Implementations of Sets
Arrays and lists may be used to contain the data
items of a set
A linked list has the advantage of supporting
constant-time removals of items
Once they are located in the structure
11
12
13
Sets
List implementation of a set
14
Dictionaries
Our list-based implementation of a dictionary is
called ListDict
The entries in a dictionary consist of two parts, a key
and a value
15
Dictionaries (continued)
16
Dictionaries (continued)
17
Dictionaries (continued)
18
19
Hashing Strategies
Key-to-address transformation or a hashing
function
Acts on a given key by returning its relative position in
an array
Hash table
An array used with a hashing strategy
Collision
Placement of different keys at the same array index
20
21
22
23
24
25
26
Linear Probing
Linear probing
Simplest way to resolve a collision
Search array, starting from collision spot, for the first
available position
27
28
29
30
Quadratic Probing
To avoid clustering associated with linear probing,
we can advance the search for an empty position a
considerable distance from the collision point
Quadratic probing: Increments the home index by
the square of a distance on each attempt
31
32
Chaining
Items are stored in an array of linked lists (chains)
Each items key locates the bucket (index) of the
chain in which the item resides or is to be inserted
To insert an item:
Compute the items home index in the array
If cell is empty, create a node with item and assign
the node to cell; else (collision), insert item in chain
Fundamentals of Python: From First Programs Through Data Structures
33
Chaining (continued)
34
Complexity Analysis
Linear probing: Complexity depends on load factor
(D) and tendency of items to cluster
Worst case (method traverses entire array before
locating items position): behavior is linear
Average behavior in searching for an item that
cannot be found is (1/2) [1 + 1/(1 D)2]
35
36
Analysis:
Should allow to gather statistics on number of
collisions caused by the hashing strategies
Other useful information:
Hash tables load factor
Number of probes needed to resolve collisions during
linear or quadratic probing
Fundamentals of Python: From First Programs Through Data Structures
37
38
39
Design:
Profiler class requires instance variables to track
a table, number of collisions, and number of probes
Fundamentals of Python: From First Programs Through Data Structures
40
41
42
Hashing Implementation of
Dictionaries
HashDict uses the bucket/chaining strategy
To manage the array, declare three instance
variables: _table, _size, and _capacity
43
44
45
46
Summary
A set is an unordered collection of items
Each item is unique
List-based implementation linear-time access
Hashing implementation constant-time access
47
Summary (continued)
A sorted dictionary imposes an ordering by
comparison on its keys
Implementations of both types of dictionaries are
similar to those of sets
Hashing: Technique for locating an item in constant
time
Techniques to resolve collisions: linear collision
processing, quadratic collision processing, chaining
The run-time and memory aspects involve the load
factor of the array
Fundamentals of Python: From First Programs Through Data Structures
48