Sets

Jed Rembold

November 14, 2022

Announcements

  • Enigma Project is due on Friday!
    • Get a start on it today if you haven’t already!!
  • Winner of the Graphics Contest was Dominik, with his great implementation of Pacman!
    • Only enough submissions for 1 winner
    • The last contest of the semester will be a Game Contest and instructions will be posted by the end of the week
  • Grade Reports posted!
    • Just got all of the Project 2 scores in last night, so those should be accurately reflected now
    • There are a few things I still need to fix, but otherwise let me know if something seems amiss!
  • Polling: rembold-class.ddns.net

Understanding Check

What is the printed value of the below code?

A = [
    {'name': 'Jill',  'weight':125, 'height':62},
    {'name': 'Sam',   'height':68},
    {'name': 'Bobby', 'height':72},
]
A.append({'weight':204, 'height':70, 'name':'Jim'})
B= A[1]
B['weight'] = 167
print([d['weight'] for d in A if 'weight' in d])
  1. [100,204]
  2. [156,173,204]
  1. [100,167,173,204]
  2. [125,167,204]

Dictionary Records

  • While most commonly used to indicate mappings, dictionaries have seen increased use of late as structures to store records

  • Looks surprisingly close to our original template of:

    boss = {
        'name': 'Scrooge',
        'title': 'founder',
        'salary': 1000
        }
  • Allows easy access of attributes without worrying about ordering

    print(boss['name'])

Mathematical Sets

  • A set is an unordered collection of distinct values.
    • digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    • evens = 0, 2, 4, 6, 8
    • odds = 1, 3, 5, 7, 9
    • primes = 2, 3, 5, 7
    • squares = 0, 1, 4, 9
    • primary = red, green, blue
    • R = x where x is a real number
    • Z = x where x is an integer
    • N = x where x is an integer >=0
  • The set with no elements is call the empty set (∅)

Pythonic Sets

  • Enclosed within squiggly brackets
  • No key-value pairs, just single values separated by commas
digits = { 0, 1, 2, 3, 4, 6, 7, 8, 9 }
squares = { 0, 1, 4, 9 }
primary = { "red", "green", "blue" }
  • Set elements must be immutable
  • Sets themselves are generally mutable
  • Can not create an empty set just using { }!
    • Python assumes this to be an empty dictionary!
    • Must instead use set().

Set Operations

  • The fundamental set operation is membership (∈)
    • 3 ∈ primes
    • 3 ∉ evens
    • red ∈ primary
    • -1 ∉ N
  • The union of the sets \(A\) and \(B\) (\(A \cup B\)) consists of all elements in either \(A\) or \(B\) or both.
  • The intersection of the sets \(A\) and \(B\) (\(A \cap B\)) consists of all elements in both \(A\) and \(B\).
  • The set difference of \(A\) and \(B\) (\(A - B\)) consists of all elements in \(A\) but not in \(B\).
  • The symmetric set difference of \(A\) and \(B\) (\(A\triangle B\)) consists of all elements in \(A\) or \(B\) but not in both.

Python Implementations

  • Python’s built-in implementation of sets supports all these same operations
  • Can either use appropriately named methods on sets or operators between sets
  • Membership 3 in primes
  • Union: A.union(B) A | B
  • Intersection A.intersection(B) A & B
  • Difference A.difference(B) A - B
  • Symmetric difference A.symmetric_difference(B) A ^ B

Venn Diagrams

  • A Venn Diagram is a graphical representation of a set which indicates common elements as overlapping areas
  • The following Venn diagrams illustrate the effect of the 4 primary set operations

image/svg+xml A B A ∪ B
image/svg+xml A B A ∪ B A B A ∩ B B A - B A

image/svg+xml A B A ∪ B A B A ∩ B
image/svg+xml A B A ∪ B A B A ∩ B A B A - B A B A ∆ B

Practice

If we have the following sets from earlier:

digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
evens = { 0, 2, 4, 6, 8 }
odds = { 1, 3, 5, 7, 9 }
primes = { 2, 3, 5, 7 }
squares = { 0, 1, 4, 9 }

What is the value of each of the following:

  • evens ∪ squares
  • odds ∩ squares
  • primes - evens
  • odds ∆ squares

Understanding Check

Looking at the same sets:

digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
evens = { 0, 2, 4, 6, 8 }
odds = { 1, 3, 5, 7, 9 }
primes = { 2, 3, 5, 7 }
squares = { 0, 1, 4, 9 }

What is the set resulting from: \[ (\text{primes} \cap \text{evens}) \cup (\text{odds}\cap\text{squares})\]

  1. { 1, 2, 9 }
  2. { 1, 3, 4, 5}
  3. { 0, 3, 4, 5, 7}

Set Relationships

  • Sets \(A\) and \(B\) are equal (\(A = B\)) if they have the same elements.
    • This would make them the same circles in a Venn diagram
  • Set \(A\) is a subset of \(B\) (\(A\subseteq B\)) if all the elements in \(A\) are also in \(B\).
    • This would mean that the circle for \(A\) would be entirely inside (or equal) to the circle of \(B\)
  • Set \(A\) is a proper subset of \(B\) (\(A\subset B\)) if \(A\) is a subset of \(B\) and the two sets are not equal

Informal Proofs

  • You can use Venn diagrams to justify different set identities
  • Example: Say you wanted to show that: \[ A - (B \cap C) = (A-B) \cup (A-C)\]

image/svg+xml A B C A B C B C A B C A B C A B C A

Python Set Methods

  • Can also use “set comprehension” to generate a set { x for x in range(0,100,2) }
Function Description
len(set) Returns the number of elements in a set
elem in set Returns True if elem is in the set
set.copy() Creates and returns a shallow copy of the set
set.add(elem) Adds the specified elem to the set
set.remove(elem) Removes the element from the set, raising a ValueError if it is missing
set.discard(elem) Removes the element from the set, doing nothing if it is missing

Why use sets?

Sets come up naturally in many situations
Many real-world applications involve unordered collections of unique elements, for which sets are the natural model.
Sets have a well-established mathematical foundation
If you can frame your application in terms of sets, you can rely on the various mathematical properties that apply to sets.
Sets can make it easier to reason about your program
One of the advantages of mathematical abstraction is that using it often makes it easy to think clearly and rigorously about what your program does.
Many important algorithms are described in terms of sets
If you look at web sites that describe some of the most important algorithms in computer science, many of them base those descriptions in terms of set operations.
// reveal.js plugins