Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sets and Multimaps 5/9/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,

Similar presentations


Presentation on theme: "Sets and Multimaps 5/9/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,"— Presentation transcript:

1 Sets and Multimaps 5/9/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Sets and Multimaps Sets and Multimaps

2 Definitions Set an unordered collection of unique elements
Elements of a set are like keys of a map, no auxiliary values. Multiset (also known as a bag) a set-like container that allows duplicates. Multimap the same key can be mapped to multiple values. For example, the index of a book maps a given term to one or more locations at which the term occurs. Sets and Multimaps

3 Set ADT Sets and Multimaps

4 Implementing a Set Similar techniques from Map Sets and Multimaps

5 Nodes storing set elements in order
Storing a Set in a List We can implement a set with a list Elements are stored sorted according to some canonical ordering The space used is O(n) Nodes storing set elements in order List Set elements Sets and Multimaps

6 Union as merging two sorted lists
A, D, J, M, N B, C, D, N, P Sets and Multimaps

7 Intersection and Subtraction
Similarly… Sets and Multimaps

8 Generic Merging Generalized merge of two sorted lists A and B
Template method genericMerge Auxiliary methods aIsLess bIsLess bothAreEqual Runs in O(nA + nB) time provided the auxiliary methods run in O(1) time Algorithm genericMerge(A, B) S  empty sequence while A.isEmpty()  B.isEmpty() a  A.first().element(); b  B.first().element() if a < b aIsLess(a, S); A.remove(A.first()) else if b < a bIsLess(b, S); B.remove(B.first()) else { b = a } bothAreEqual(a, b, S) A.remove(A.first()); B.remove(B.first()) while A.isEmpty() while B.isEmpty() return S Sets and Multimaps

9 Using Generic Merge for Set Operations
Any of the set operations can be implemented using a generic merge For example: For intersection: only copy elements that are duplicated in both list For union: copy every element from both lists except for the duplicates All methods run in linear time Sets and Multimaps

10 Algorithm genericMerge(A, B) S  empty sequence
while A.isEmpty()  B.isEmpty() a  A.first().element(); b  B.first().element() if a < b aIsLess(a, S); A.remove(A.first()) else if b < a bIsLess(b, S); B.remove(B.first()) else { b = a } bothAreEqual(a, b, S) A.remove(A.first()); B.remove(B.first()) while A.isEmpty() while B.isEmpty() return S aIsLess(a,S) bIsLess(b,S) bothAreEqual(a,b,S) Union(A,B) Intersection(A,B) subtraction(A,B) Sets and Multimaps

11 Algorithm genericMerge(A, B) S  empty sequence
while A.isEmpty()  B.isEmpty() a  A.first().element(); b  B.first().element() if a < b aIsLess(a, S); A.remove(A.first()) else if b < a bIsLess(b, S); B.remove(B.first()) else { b = a } bothAreEqual(a, b, S) A.remove(A.first()); B.remove(B.first()) while A.isEmpty() while B.isEmpty() return S aIsLess(a,S) bIsLess(b,S) bothAreEqual(a,b,S) Union(A,B) Append a to S Append b to S Intersection(A,B) nothing Subtraction(A,B) Sets and Multimaps

12 Mulitset Set-like with duplicates Can use a map Key = element
Value = count Sets and Multimaps

13 Multimap store multiple entries with the same key
implement a multimap M by means of a map M’ let E(k) be the list of entries of M with key k The entries of M’ are the pairs (k, E(k)) Ie, each “entry” is a list of entries Sets and Multimaps

14 Mulitmaps Sets and Multimaps


Download ppt "Sets and Multimaps 5/9/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,"

Similar presentations


Ads by Google