ordered sets - university of california, berkeleycs61a/fa18/assets/... · sets as unordered...
TRANSCRIPT
![Page 1: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/1.jpg)
Ordered Sets
![Page 2: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/2.jpg)
Announcements
![Page 3: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/3.jpg)
Sets
![Page 4: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/4.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets have arbitrary order, just like dictionary entries
>>> s = {'one', 'two', 'three', 'four', 'four'} >>> s {'three', 'one', 'four', 'two'} >>> 'three' in s True >>> len(s) 4 >>> s.union({'one', 'five'}) {'three', 'five', 'one', 'four', 'two'} >>> s.intersection({'six', 'five', 'four', 'three'}) {'three', 'four'} >>> s {'three', 'one', 'four', 'two'}
4
![Page 5: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/5.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
• Intersection: Return a set with any elements in set1 and set2
• Adjoin: Return a set with all elements in s and a value v
Union
1
34
2
35
1
34
2
5
Intersection
1
34
2
35
3
Adjoin
1
34
2
1
34
2
5
![Page 6: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/6.jpg)
Sets as Linked Lists
![Page 7: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/7.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
(Demo)
7
Time order of growth
⇥(1)
⇥(n)
Time depends on whether & where v appears in s.
In the worst case: v does not appear in s
or In the average case: appears in a uniformly distributed
random location
def empty(s): return s is Link.empty
def contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(3, Link(2))) >>> contains(s, 2) True """
![Page 8: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/8.jpg)
�(n)
Sets as Unordered Sequences
�(n2)
Time order of worst-case growth
The size of the set
If sets are the same size
8
def adjoin(s, v): if contains(s, v): return s else: return Link(v, s)
def intersect(s, t):
if s is Link.empty:
return Link.empty
rest = __________________________
if contains(t, s.first):
return ______________________
else:
return rest
intersect(s.rest, t)
Link(s.first, rest)
![Page 9: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/9.jpg)
Sets as Ordered Linked Lists
![Page 10: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/10.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, contains, adjoin, intersect, union
Implement set operations Ordered linked lists first, rest, <, >, ==
Different parts of a program may make different assumptions about data
![Page 11: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/11.jpg)
Searching an Ordered List
11
first: 1
rest:
Link instance
first: 2
rest:
Link instance
�(n)contains
Time order of growthOperation
�(n)adjoin
first: 1
rest:
Link instance
first: 3
rest:
Link instance
first: 5
rest:
Link instance
s:
>>> s = Link(1, Link(3, Link(5))) >>> contains(s, 1) True >>> contains(s, 2) False >>> t = adjoin(s, 2)
t: (Demo)
![Page 12: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/12.jpg)
Set Operations
![Page 13: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/13.jpg)
Intersecting Ordered Linked Lists
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
13
def intersect(s, t): if empty(s) or empty(t): return Link.empty else: e1, e2 = s.first, t.first if e1 == e2: return Link(e1, intersect(s.rest, t.rest)) elif e1 < e2: return intersect(s.rest, t) elif e2 < e1: return intersect(s, t.rest)
�(n)Order of growth? (Demo)If s and t are sets of size n, then
![Page 14: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/14.jpg)
Set Mutation
![Page 15: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/15.jpg)
Adding to an Ordered List
15
first: 1
rest:
Link instance
first: 3
rest:
Link instance
first: 5
rest:
Link instance
add(s, 0)
s:
Try to return the same object as input
![Page 16: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/16.jpg)
Adding to an Ordered List
16
first: 1 0
rest:
Link instance
first: 3
rest:
Link instance
first: 1
rest:
Link instance
first: 5
rest:
Link instance
s:
add(s, 4)
add(s, 3)
![Page 17: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/17.jpg)
Adding to an Ordered List
17
first: 1 0
rest:
Link instance
first: 3
rest:
Link instance
first: 5 4
rest:
Link instance
first: 1
rest:
Link instance
first: 5
rest:
Link instance
add(s, 6)
s:
![Page 18: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/18.jpg)
Adding to an Ordered List
18
first: 1 0
rest:
Link instance
first: 3
rest:
Link instance
first: 5 4
rest:
Link instance
first: 1
rest:
Link instance
first: 5
rest:
Link instance
first: 6
rest:
Link instance
s:
![Page 19: Ordered Sets - University of California, Berkeleycs61a/fa18/assets/... · Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate](https://reader033.vdocuments.mx/reader033/viewer/2022060415/5f13878c980f787f44061fb0/html5/thumbnails/19.jpg)
Adding to a Set Represented as an Ordered List
19
def add(s, v): """Add v to a set s, returning modified s.”””
>>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ if empty(s): return Link(v) if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s
v Link(s.first, s.rest)
add(s.rest, v)
Link(v, s.rest)
s:
.