Discrete Structures & Algorithms More about sets EECE 320 — UBC.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

More Set Definitions and Proofs 1.6, 1.7. Ordered n-tuple The ordered n-tuple (a1,a2,…an) is the ordered collection that has a1 as its first element,
Recursive Definitions and Structural Induction
Analysis of Algorithms
Recursively Defined Functions
Prof. Johnnie Baker Module Basic Structures: Sets
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Discrete Structures & Algorithms Basics of Set Theory EECE 320 — UBC.
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Induction and recursion
Discrete Structures & Algorithms More on Methods of Proof / Mathematical Induction EECE 320 — UBC.
1 Section 1.7 Set Operations. 2 Union The union of 2 sets A and B is the set containing elements found either in A, or in B, or in both The denotation.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
Induction and recursion
Sets Set Operations Functions. 1. Sets 1.1 Introduction and Notation 1.2 Cardinality 1.3 Power Set 1.4 Cartesian Products.
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Induction and recursion
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Lecture 2 Computational Complexity
CS 3343: Analysis of Algorithms
Chapter 4: Induction and Recursion
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
Analysis of Algorithms
Sequences – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Sequences Reading: Kolman, Section 1.3.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
CSC 211 Data Structures Lecture 13
Complexity 20-1 Complexity Andrei Bulatov Parallel Arithmetic.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
Chapter 5 With Question/Answer Animations. Section 5.1.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
CompSci 102 Discrete Math for Computer Science
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Counting Discrete Mathematics. Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?”
Lecture 2.1: Sets and Set Operations CS 250, Discrete Structures, Fall 2014 Nitesh Saxena Adopted from previous lectures by Cinda Heeren.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Sets Definition: A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. We write a.
Discrete Mathematics. Set Theory - Definitions and notation A set is an unordered collection of elements. Some examples: {1, 2, 3} is the set containing.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Module #10: Proof Strategies Rosen 5 th ed., §3.1 (already covered)
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Chapter 4: Induction and Recursion
Discrete Mathematical The Set Theory
Modeling with Recurrence Relations
COMP108 Algorithmic Foundations Algorithm efficiency
CS 3343: Analysis of Algorithms
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Discrete Structures for Computer Science
Discrete Structures for Computer Science
Applied Discrete Mathematics Week 9: Integer Properties
Divide and Conquer Algorithms Part I
Solving Recurrence Relations
Induction and recursion
Presentation transcript:

Discrete Structures & Algorithms More about sets EECE 320 — UBC

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 Generalized Union Example: Let U = N, and define: Then

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 Generalized Intersection Example: Let U = N, and define: Then

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 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 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 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 Inclusion/Exclusion Example: There are 250 ECE students. 100 are taking ECE are taking ECE are taking both. How many people are taking ECE320 or ECE315? – 50 = 170 ECE320 ECE315 |A  B| = |A| + |B| - |A  B|

11 Inclusion/Exclusion Example: There are 250 ECE students. 100 are taking ECE are taking ECE are taking both. How many are taking neither ECE320 nor ECE315? ECE320 ECE ( ) = 80

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 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 Set representations

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 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. A B A  B A  B Bit-wise ORBit-wise AND

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