Download presentation
Presentation is loading. Please wait.
1
Discrete Structures & Algorithms More about sets EECE 320 — UBC
2
2 Generalized Union Example: Let U = N, and define: A 1 = {2,3,4,…} A 2 = {4,6,8,…} A 3 = {6,9,12,…}
3
3 Generalized Union Example: Let U = N, and define: Then
4
4 Generalized Intersection Example: Let U = N, and define: A 1 = {1,2,3,4,…} A 2 = {2,4,6,…} A 3 = {3,6,9,…}
5
5 Generalized Intersection Example: Let U = N, and define: Then
6
6 Inclusion/Exclusion Example: How many people are wearing a watch? How many people are wearing sneakers? How many people are wearing a watch OR sneakers? What’s wrong? A B Wrong. |A B| = |A| + |B| - |A B| |A|+|B|?
7
7 Reminder: How to specify sets? Explicit enumeration: {John, Paul, George, Ringo} Implicitly: {1,2,3,…}, or {2,3,5,7,11,13,17,…} Using predicates: { x : P(x) is true }, –where P(x) is some predicate that is true for all elements of the set –Example: { x : x is prime } Recursive definitions –Example: The set of integers divisible by 3 can be specified as. Basis step: 3 S 3 Recursive step: If x, y S 3 then x+y S 3
8
8 Recursive set definitions Assume S is a an alphabet. (for example S={0,1}) What set does this definition specifies? Basis step: S* (where is the notation for the null string) Recursive step: If w S* and x S then wx S*
9
9 Cardinality of sets If S is finite, then the cardinality of S, |S|, is the number of distinct elements in S. If S = {1,2,3}, |S| = 3. If S = {3,3,3,3,3}, If S = , If S = { , { }, { ,{ }} }, |S| = 1. |S| = 0. |S| = 3. If S = {0,1,2,3,…}, |S| is infinite. (more on this later)
10
10 Inclusion/Exclusion Example: There are 250 ECE students. 100 are taking ECE 320. 120 are taking ECE 315. 50 are taking both. How many people are taking ECE320 or ECE315? 100 + 120 – 50 = 170 ECE320 ECE315 |A B| = |A| + |B| - |A B|
11
11 Inclusion/Exclusion Example: There are 250 ECE students. 100 are taking ECE 320. 150 are taking ECE 315. 50 are taking both. How many are taking neither ECE320 nor ECE315? ECE320 ECE315 250 - (100+120-50) = 80
12
12 Generalized Inclusion/Exclusion Suppose we have: And we want to know |A U B U C| A B C |A U B U C| = |A| + |B| + |C| + |A B C| - |A B| - |A C| - |B C| We have counted these intersections twice here This intersection has been counted thrice here But has been subtracted thrice here! Therefore, add it back once to ensure it is correctly counted.
13
13 Wrap up The principle of inclusion/exclusion allows us to compose sets for smaller sets and to reason about the size of the new set. Solve examples in the textbook to become comfortable with inclusion/exclusion.
14
14 Set representations
15
15 Sets as bit strings Let U = {x 1, x 2,…, x n }, and let A U. Then the characteristic vector of A is the n-vector whose elements, x i, are 1 if x i A, and 0 otherwise. Example: If U = {x 1, x 2, x 3, x 4, x 5, x 6 }, and A = {x 1, x 3, x 5, x 6 }, then the characteristic vector of A is (101011)
16
16 Sets as bit strings Example: If U = {x 1, x 2, x 3, x 4, x 5, x 6 }, A = {x 1, x 3, x 5, x 6 }, and B = {x 2, x 3, x 6 }, then we have a quick way of finding the characteristic vectors of A B and A B. A101011 B011001 A B A B 111011 001001 Bit-wise ORBit-wise AND
17
17 Representing sets Are bit strings always sufficient? What assumptions do we make when we adopt the bit string representation? –That we know the universe of possible elements. –We also need to keep a list of all possible elements. –If we are storing a set of names using a bit string, we may need to also store information such as “Smith is bit 0, Rogers is bit 2, …”
18
18 Representing sets Alternative representations of sets? –Arrays –Linked lists –… What is the primary operation to be supported? –Set membership –And, how do we implement such an operation? Search.
19
19 Sets as arrays SmithRogersPaulBrown…Campbell What is the primary operation? Set membership. How do we test if “Scott” is in the set? Elements are in arbitrary positions. What is the maximum number of comparisons needed? If n is the cardinality (size) of the set? Maximum comparisons: n What is the minimum number of comparisons needed? What is the average number of comparisons needed? n/2. Why?
20
20 Complexity of search SmithRogersPaulBrown…Campbell If the set is represented as an unordered list, we have to search through all elements. Let f(n) be the time required to search for an element in a list of size n. Let t be the time taken per comparison. Then: f(n) (t)(n), and this is expressed as f(n)=O(n), or the time complexity of sequential search is O(n). More about the big-O notation in another lecture.
21
21 Binary search BrownCampbellDaliRogers…Scott What if the array or the list were kept ordered? Suppose we were searching for “Brown”. Compare with the middle element of the array. If “Brown” is smaller (lexicographically) then we need to search only in the left half of the list, else we need to search only in the right half of the list. We then proceed, iteratively, to make a comparison with the middle element of the search region.
22
22 How many comparisons? (binary search) For simplicity, assume N, the size of the array, is 2 k. After each comparison, we reduce the size of our search to a smaller array that is half the size of the initial array. Therefore: the maximum number of steps needed is … –At worst, we need to bring the size of the search region to 1. –How many steps would that take? –Diminishing array sizes: 2 k, 2 k-1, …, 2 0 k+1 = log 2 N+1. BrownCampbellDaliRogers…Scott
23
23 How many comparisons? (binary search) If g(n) denotes the time required to search for an element using binary search in an array of size n, then –g(n) (log 2 n+1)t, –where t is the time needed per comparison/iteration. g(n) = O(log 2 n), is what we say. BrownCampbellDaliRogers…Scott
24
24 What do you need to know? (as good engineers) To understand how to perform computations efficiently, we need to know –How to count! –How to sum a sequence of steps. –Reason about the complexity/efficiency of an algorithm. –Prove the correctness of our analysis. Logic Methods of inference Proof strategies Induction … –(and eventually, how to create better algorithms.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.