1 / 19

Discrete Structures & Algorithms More about sets

Explore different methods of specifying sets, including explicit enumeration, predicates, and recursive definitions. Learn about cardinality and set operations like inclusion/exclusion. Discover different representations of sets and their complexity in search operations.

Download Presentation

Discrete Structures & Algorithms More about sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete Structures & AlgorithmsMore about sets EECE 320 — UBC

  2. 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 S3 • Recursive step: If x, y S3 then x+y S3

  3. 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*

  4. 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}, |S| = 1. If S = , |S| = 0. |S| = 3. If S = { , {}, {,{}} }, If S = {0,1,2,3,…}, |S| is infinite. (more on this later)

  5. ECE320 ECE315 100 + 120 – 50 = 170 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? |A  B| = |A| + |B| - |A  B|

  6. ECE320 ECE315 250 - (100+120-50) = 80 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?

  7. Generalized Inclusion/Exclusion Suppose we have: B A C And we want to know |A U B U C| |A U B U C| = |A| + |B| + |C| We have counted these intersections twice here - |A  B| - |A  C| - |B  C| + |A  B  C| Therefore, add it back once to ensure it is correctly counted. This intersection has been counted thrice here But has been subtracted thrice here!

  8. 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.

  9. Set representations

  10. (101011) Sets as bit strings Let U = {x1, x2,…, xn}, and let A  U. Then the characteristic vector of A is the n-vector whose elements, xi, are 1 if xi A, and 0 otherwise. Example: If U = {x1, x2, x3, x4, x5, x6}, and A = {x1, x3, x5, x6}, then the characteristic vector of A is

  11. Bit-wise OR Bit-wise AND Sets as bit strings Example: If U = {x1, x2, x3, x4, x5, x6}, A = {x1, x3, x5, x6}, and B = {x2, x3, x6}, then we have a quick way of finding the characteristic vectors of A  B and A  B.

  12. 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, …”

  13. 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.

  14. Sets as arrays Smith Rogers Paul Brown … 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?

  15. Complexity of search Smith Rogers Paul Brown … 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.

  16. Binary search What if the array or the list were kept ordered? Suppose we were searching for “Brown”. Brown Campbell Dali Rogers … Scott • 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.

  17. How many comparisons? (binary search) • For simplicity, assume N, the size of the array, is 2k. • 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: 2k, 2k-1, …, 20 • k+1 = log2N+1. Brown Campbell Dali Rogers … Scott

  18. 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)  (log2n+1)t, • where t is the time needed per comparison/iteration. • g(n) = O(log2n), is what we say. Brown Campbell Dali Rogers … Scott

  19. 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.)

More Related