DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 23

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 23Friday, October 21SetsOne more built-in Python container type•Set literals are enclosed in braces•Duplicate elements are removed on construction•Sets are unordered, just like dictionary entries2>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}>>> 3 in sTrue>>> len(s)4>>> s.union({1, 5}){1, 2, 3, 4, 5}>>> s.intersection({6, 5, 4, 3}){3, 4}Implementing SetsThe interface for sets•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•Adjunction: Return a set with all elements in s and a value v3Union13423513425Intersection1342353Adjunction13421342Sets as Unordered SequencesProposal 1: A set is represented by a recursive list that contains no duplicate items4 def empty(s): return s is Rlist.empty def set_contains(s, v): if empty(s): return False elif s.first == v: return True return set_contains(s.rest, v)DemoR(n)=Θ(n)k1· n ≤ R(n) ≤ k2· nReview: Order of GrowthFor a set operation that takes "linear" time, we say that5n: size of the setR(n): number of steps required to perform the operationwhich means that there are constants k1 and k2 such thatfor sufficiently large values of n.DemoΘ(n)Θ(n2)Sets as Unordered Sequences6 def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v) set1_not_set2 = filter_rlist(set1, f) return extend_rlist(set1_not_set2, set2)Θ(n2)Time order of growthThe size of the setThe size of the larger setSets as Ordered SequencesProposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest7 def set_contains2(s, v): if empty(s) or s.first > v: return False elif s.first == v: return True return set_contains2(s.rest, v)Θ(n)Order of growth?Set Intersection Using Ordered SequencesThis algorithm assumes that elements are in order.8def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1: return intersect_set2(set1, set2.rest)DemoΘ(n)Order of growth?Tree SetsProposal 3: A set is represented as a Tree. Each entry is:•Larger than all entries in its left branch and•Smaller than all entries in its right branch9731 59117315 911531 7911Membership in Tree SetsSet membership tests traverse the tree•The element is either in the left or right sub-branch•By focusing on one branch, we reduce the set by about half10531 7911 def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)9If 9 is in the set, it is in this branchAdjoining to a Tree Set11531 791187911878Right! Left! Right!None None8NoneStop!87879118531 79118DemoWhat Did I Leave Out?Sets as ordered sequences:•Adjoining an element to a set•Union of two setsSets as binary trees:•Intersection of two sets•Union of two setsThat's homework 8!12No lecture on MondayMidterm 2 on Monday, 7pm-9pmGood


View Full Document

Berkeley COMPSCI 61A - Lecture 23

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 23
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 23 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 23 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?