Download presentation

Presentation is loading. Please wait.

Published byIvan Whitting Modified over 2 years ago

1
Fundamentals of Python: From First Programs Through Data Structures Chapter 19 Unordered Collections: Sets and Dictionaries

2
Fundamentals of Python: From First Programs Through Data Structures2 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

3
Fundamentals of Python: From First Programs Through Data Structures3 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

4
Fundamentals of Python: From First Programs Through Data Structures4 A set is a collection of items in no particular order Most typical operations: –Return the number of items in the set –Test for the empty set (a set that contains no items) –Add an item to the set –Remove an item from the set –Test for set membership –Obtain the union of two sets –Obtain the intersection of two sets –Obtain the difference of two sets Using Sets

5
Fundamentals of Python: From First Programs Through Data Structures5 Using Sets (continued)

6
Fundamentals of Python: From First Programs Through Data Structures6 The Python set Class

7
Fundamentals of Python: From First Programs Through Data Structures7 The Python set Class (continued)

8
Fundamentals of Python: From First Programs Through Data Structures8 A Sample Session with Sets

9
Fundamentals of Python: From First Programs Through Data Structures9 A Sample Session with Sets (continued)

10
Fundamentals of Python: From First Programs Through Data Structures10 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

11
Fundamentals of Python: From First Programs Through Data Structures11 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 Hashing attempts to approximate random access into an array for insertions, removals, and searches

12
Fundamentals of Python: From First Programs Through Data Structures12 Relationship Between Sets and Dictionaries A dictionary is an unordered collection of elements called entries –Each entry consists of a key and an associated value –A dictionarys keys must be unique, but its values may be duplicated One can think of a dictionary as having a set of keys

13
Fundamentals of Python: From First Programs Through Data Structures13 List Implementations of Sets and Dictionaries The simplest implementations of sets and dictionaries use lists This section presents these implementations and assesses their run-time performance

14
Sets List implementation of a set Fundamentals of Python: From First Programs Through Data Structures14

15
Fundamentals of Python: From First Programs Through Data Structures15 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 A list implementation of a dictionary behaves in many ways like a list implementation of a set

16
Fundamentals of Python: From First Programs Through Data Structures16 Dictionaries (continued)

17
Fundamentals of Python: From First Programs Through Data Structures17 Dictionaries (continued)

18
Fundamentals of Python: From First Programs Through Data Structures18 Dictionaries (continued)

19
Fundamentals of Python: From First Programs Through Data Structures19 Complexity Analysis of the List Implementations of Sets and Dictionaries The list implementations of sets and dictionaries require little programmer effort –Unfortunately, they do not perform well Basic accessing methods must perform a linear search of the underlying list –Each basic accessing method is O(n)

20
Fundamentals of Python: From First Programs Through Data Structures20 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

21
Fundamentals of Python: From First Programs Through Data Structures21 Hashing Strategies (continued)

22
Fundamentals of Python: From First Programs Through Data Structures22 Hashing Strategies (continued)

23
Fundamentals of Python: From First Programs Through Data Structures23 The Relationship of Collisions to Density Density –The number of keys relative to the length of an array As the density decreases, so does the probability of collisions Keeping a low load factor even (say, below.2) seems like a good way to avoid collisions –Cost of memory incurred by load factors below.5 is probably prohibitive for data sets of millions of items –Even load factors below.5 cannot prevent many collisions from occurring for some data sets

24
Fundamentals of Python: From First Programs Through Data Structures24 Hashing with Non-Numeric Keys Try returning the sum of the ASCII values in the string This method has effect of producing same keys for anagrams –Strings that contain same characters, but in different order First letters of many words in English are unevenly distributed –This might have the effect of weighting or biasing the sums generated

25
Fundamentals of Python: From First Programs Through Data Structures25 Hashing with Non-Numeric Keys (continued) One solution: –If length of string is greater than a certain threshold Drop first character from string before computing sum Can also subtract the ASCII value of the last character Python also includes a standard hash function for use in hashing applications –Function can receive any Python object as an argument and returns a unique integer

26
Fundamentals of Python: From First Programs Through Data Structures26 Hashing with Non-Numeric Keys (continued)

27
Fundamentals of Python: From First Programs Through Data Structures27 Linear Probing Linear probing –Simplest way to resolve a collision –Search array, starting from collision spot, for the first available position At the start of an insertion, the hashing function is run to compute the home index of the item –If cell at home index is not available, move index to the right to probe for an available cell –When search reaches last position of array, probing wraps around to continue from the first position

28
Linear Probing (continued) For retrievals, stop probing process when current array cell is empty or it contains the target item –If target item is found, its cell is set to DELETED Fundamentals of Python: From First Programs Through Data Structures28

29
Fundamentals of Python: From First Programs Through Data Structures29 Linear Probing (continued) Problem: After several insertions/removals, item is farther away from its home index than needs to be –Increasing the average overall access time Two ways to deal with this problem: –After a removal, shift items on the cells right over to the cells left until an empty cell, a currently occupied cell, or the home indexes for each item are reached –Regularly rehash the table (e.g., if load factor is.5) Clustering: Occurs when items causing a collision are relocated to the same region within the array

30
Fundamentals of Python: From First Programs Through Data Structures30 Linear Probing (continued)

31
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 Problem: By jumping over some cells, one or more of them might be missed –Can lead to some wasted space Fundamentals of Python: From First Programs Through Data Structures31

32
Quadratic Probing (continued) Here is the code for insertions, updated to use quadratic probing: Fundamentals of Python: From First Programs Through Data Structures32

33
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 Retrieval and removal each perform these steps: –Compute the items home index in the array –Search the linked list at that index for the item 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 Structures33

34
Fundamentals of Python: From First Programs Through Data Structures34 Chaining (continued)

35
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 ] Quadratic probing: Tends to mitigate clustering –Average search complexity is 1 – log e (1 – D) – (D / 2) for the successful case and 1 / (1 – D) – D – log e (1 – D) for the unsuccessful case Fundamentals of Python: From First Programs Through Data Structures35

36
Complexity Analysis (continued) Chaining: –Locating an item consists of two parts: Computing home index constant time behavior Searching linked list upon a collision linear –Worst case (all items that have collided with each other are in one chain, which is a linked list): O(n) –If lists are evenly distributed in array and array is fairly large, the second part can be close to constant –Best case (a chain of length 1 occupies each array cell): O(1) Fundamentals of Python: From First Programs Through Data Structures36

37
Case Study: Profiling Hashing Strategies Request: –Write a program that allows a programmer to profile different hashing strategies 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 Structures37

38
Fundamentals of Python: From First Programs Through Data Structures38 Case Study: Profiling Hashing Strategies (continued)

39
Fundamentals of Python: From First Programs Through Data Structures39 Case Study: Profiling Hashing Strategies (continued)

40
Analysis (continued): –Here are the profilers results: 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 Structures40

41
Case Study: Profiling Hashing Strategies (continued) Implementation: Fundamentals of Python: From First Programs Through Data Structures41

42
Fundamentals of Python: From First Programs Through Data Structures42 Case Study: Profiling Hashing Strategies (continued)

43
Hashing Implementation of Dictionaries HashDict uses the bucket/chaining strategy –To manage the array, declare three instance variables: _table, _size, and _capacity Fundamentals of Python: From First Programs Through Data Structures43

44
Hashing Implementation of Sets The design of the methods for HashSet is also the same as the methods in HashDict, except: –__contains__ searches for an item (not key) –add inserts item only if it is not already in the set –A single iterator method is included instead of separate methods that return keys and values Fundamentals of Python: From First Programs Through Data Structures44

45
Sorted Sets and Dictionaries Each item added to a sorted set must be comparable with its other items –Same applies for keys added to a sorted dictionary The iterator for each type of collection guarantees its users access to items or keys in sorted order Implementation alternatives: –List-based: must maintain a sorted list of the items –Hashing implementation: not feasible –Binary search tree implementation: generally provide logarithmic access to data items Fundamentals of Python: From First Programs Through Data Structures45

46
Sorted Sets and Dictionaries (continued) Fundamentals of Python: From First Programs Through Data Structures46

47
Summary A set is an unordered collection of items –Each item is unique –List-based implementation linear-time access –Hashing implementation constant-time access Items in a sorted set can be visited in sorted order –A tree-based implementation of a sorted set supports logarithmic-time access A dictionary is an unordered collection of entries, where each entry consists of a key and a value –Each key is unique; its values may be duplicated Fundamentals of Python: From First Programs Through Data Structures47

48
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 Structures48

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google