Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 68 · Sets, Relations and Functions

Fuzzy Sets and Multisets

Go beyond ordinary sets: multisets that count repeats and fuzzy sets with degrees of membership, with union, intersection and complement in Python.

An ordinary set answers one question about an element: in or out. That works for "the primes below 20", but real data is messier. A shopping basket contains three apples, not "apples: yes". And is a 28-degree day "hot"? It is somewhat hot, more than 20 degrees and less than 38. Two extensions of set theory handle these cases. Multisets allow an element to appear several times. Fuzzy sets allow an element to belong to a degree between 0 and 1.

This lesson builds on Sets and shows both extensions with the operations you already know: union, intersection and complement.

Multisets

A multiset (or bag) is a collection where each element has a multiplicity: the number of times it occurs. Order still does not matter. {a, a, b} and {a, b, a} are the same multiset, but both differ from the set {a, b}. We write the multiplicity of x in A as m_A(x).

Operations use the multiplicities:

  • Union: m(x) = max(m_A(x), m_B(x)).
  • Intersection: m(x) = min(m_A(x), m_B(x)).
  • Sum: m(x) = m_A(x) + m_B(x): pour two bags together.
  • Difference: m(x) = max(0, m_A(x) - m_B(x)).
  • Cardinality: the sum of all multiplicities.

These generalize the set operations: when every multiplicity is 0 or 1, union and intersection reduce to the usual ones. Multisets describe the prime factorization of a number (12 = {2, 2, 3}), the letters in a word, the roots of a polynomial counted with multiplicity and the contents of a database table without a unique key.

Python's collections.Counter is a multiset, and its operators |, &, + and - match the definitions above.

Python Multisets with Counter

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last two lines reveal a lovely fact: the multiset intersection of prime factorizations gives the gcd, and the union gives the lcm. Compare GCD and the Euclidean Algorithm.

Fuzzy sets

A fuzzy set A over a universe U is described by a membership function μ_A : U → [0, 1]. The value μ_A(x) is the degree to which x belongs to A. A value of 1 means full membership, 0 means none, and 0.7 means "mostly". An ordinary (crisp) set is the special case where μ only takes values 0 and 1.

Fuzzy sets model vague concepts: "tall", "warm", "fast". A thermostat controller does not switch abruptly at 25 degrees; it blends rules based on how warm the room is, which is the core idea of fuzzy logic in appliances and control systems.

The standard operations (due to Zadeh) work pointwise:

  • Union: μ(x) = max(μ_A(x), μ_B(x)).
  • Intersection: μ(x) = min(μ_A(x), μ_B(x)).
  • Complement: μ(x) = 1 - μ_A(x).
  • Subset: A ⊆ B if μ_A(x) ≤ μ_B(x) for all x.

Notice that min and max are the same formulas used for multiset intersection and union. All three theories share a pattern: sets use 0/1, multisets use counts, fuzzy sets use degrees.

Three kinds of "belonging"

Set In or out, 1 or 0.
Multiset In k times, k = 0, 1, 2 ...
Fuzzy set In to a degree between 0 and 1.

Union uses max and intersection uses min in all three.

1 / 2
Python Fuzzy sets of temperatures

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two things stand out. At 30 degrees the temperature is partly warm (0.5) and partly hot (0.33), so a controller would blend both rules. And the last line shows a break from classical logic: A ∩ complement(A) is not empty for a fuzzy set, since min(0.5, 0.5) = 0.5. The laws of excluded middle and non-contradiction fail, though De Morgan's laws, commutativity and associativity still hold.

An α-cut takes all elements with membership at least α and returns an ordinary set. It is the bridge that lets you use crisp tools on fuzzy data.

When to use which

  • Use a set when membership is a clear yes or no.
  • Use a multiset when duplicates carry meaning: counting words, inventory, factorizations, bags of items.
  • Use a fuzzy set when a category is vague and degrees matter: sensor data, control systems, approximate matching, ranking.

Do not confuse fuzziness with probability. "This tea is 0.7 hot" describes how well the tea fits a vague word. "70% chance the tea is hot" describes uncertainty about a crisp fact. See Probability Basics for the latter.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is the intersection of the multisets {a, a, b} and {a, b, b}?

    Intersection takes the minimum multiplicity of each element: a appears min(2, 1) = 1 time and b min(1, 2) = 1 time.
  2. 2. In a fuzzy set, mu(x) = 0.3 means what?

    A membership function value gives the degree of membership between 0 (not at all) and 1 (completely).
  3. 3. Given mu_A(x) = 0.8 and mu_B(x) = 0.5, what is the union membership of x?

    Fuzzy union takes the maximum of the two degrees.
  4. 4. Which classical law can fail for fuzzy sets?

    With mu(x) = 0.5, min(mu, 1 - mu) is 0.5, so the intersection of A and its complement is not empty.

Summary

  • A multiset assigns each element a multiplicity; union takes the maximum, intersection the minimum, sum adds.
  • Prime factorizations are multisets: intersection gives the gcd and union gives the lcm.
  • A fuzzy set uses a membership function into [0, 1]; union is max, intersection is min, complement is 1 minus the degree.
  • Fuzzy sets model vague concepts and break the law that A and its complement are disjoint.
  • Sets, multisets and fuzzy sets all use the same min/max pattern with different kinds of "belonging".

Continue with Power Sets and Cartesian Products.