binary relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...a binary relation over a set...

174
Binary Relations

Upload: others

Post on 04-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Binary Relations

Page 2: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Outline for Today

Binary Relations

• Reasoning about connections between objects.

Equivalence Relations

• Reasoning about clusters.

Strict Orders

• Reasoning about prerequisites.

Page 3: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Relationships

In CS103, you've seen examples of relationships

• between sets:

A ⊆ B

• between numbers:

x < y x ≡ₖ y x ≤ y

• between people:

p loves q

Since these relations focus on connections between two objects, they are called binary relations.

• The “binary” here means “pertaining to two things,” not “made of zeros and ones.”

Page 4: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

What exactly is a binary relation?

Page 5: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 6: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 7: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 8: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 9: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 10: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 11: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

<

Page 12: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

10 12

<

Page 13: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

10 12

<

10 < 12

Page 14: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

<

Page 15: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

<

5 -2

Page 16: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

<

5 -2

5 ≮ -2

Page 17: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

≡₃

Page 18: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

≡₃

7 10

Page 19: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

7 10

≡₃

7 ≡₃ 10

Page 20: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

≡₃

6 11

Page 21: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

≡₃

6 11

6 ≢₃ 11

Page 22: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 23: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

R

Page 24: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

R

Page 25: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

aRb

R

Page 26: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

R

Page 27: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

aR̸b

R

Page 28: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Binary Relations

A binary relation over a set A is a predicate R that can be applied to ordered pairs of elements drawn from A.

If R is a binary relation over A and it holds for the pair (a, b), we write aRb.

3 = 3 5 < 7 Ø ⊆ ℕ

If R is a binary relation over A and it does not hold for the pair (a, b), we write aR̸b.

4 ≠ 3 4 ≮ 3 ℕ ⊈ Ø

Page 29: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Properties of Relations

Generally speaking, if R is a binary relation over a set A, the order of the operands is significant.

• For example, 3 < 5, but 5 ≮ 3.

In some relations order is irrelevant; more on that later.

Relations are always defined relative to some underlying set.

It's not meaningful to ask whether ☺ ⊆ 15, for example, since ⊆ is defined over sets, not arbitrary objects.

Page 30: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Visualizing Relations

We can visualize a binary relation R over a set A by drawing the elements of A and drawing an arrow between an element a and an element b if aRb is true.

Example: the relation a | b (meaning “a divides b”) over the set {1, 2, 3, 4} looks like this:

1

2

4

3

Page 31: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Visualizing Relations

We can visualize a binary relation R over a set A by drawing the elements of A and drawing an arrow between an element a and an element b if aRb is true.

Example: the relation a ≠ b over the set {1, 2, 3, 4} looks like this:

1

2

4

3

Page 32: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Visualizing Relations

We can visualize a binary relation R over a set A by drawing the elements of A and drawing an arrow between an element a and an element b if aRb is true.

Example: the relation a = b over the set {1, 2, 3, 4} looks like this:

1

2

4

3

Page 33: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Visualizing Relations

We can visualize a binary relation R over a set A by drawing the elements of A and drawing an arrow between an element a and an element b if aRb is true.

Example: below is some relation over {1, 2, 3, 4} that's a totally valid relation even though there doesn't appear to be a simple unifying rule.

1

2

4

3

Page 34: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Capturing Structure

Page 35: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Capturing Structure

• Binary relations are an excellent way for capturing certain structures that appear in computer science.

• Today, we'll look at two examples (partitions and prerequisites).

• Then on Friday, we'll explore how to write proofs about definitions given in first-order logic.

Page 36: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Partitions

Page 37: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 38: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 39: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 40: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 41: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 42: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 43: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 44: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 45: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 46: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 47: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Partitions

• A partition of a set is a way of splitting the set into disjoint, nonempty subsets so that every element belongs to exactly one subset.

• Two sets are disjoint if their intersection is the empty set; formally, sets S and T are disjoint if S ∩ T = Ø.

• Intuitively, a partition of a set breaks the set apart into smaller pieces.

• There doesn't have to be any rhyme or reason to what those pieces are, though often there is one.

Page 48: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Partitions and Clustering

• If you have a set of data, you can often learn something from the data by finding a “good” partition of that data and inspecting the partitions.

• Usually, the term clustering is used in data analysis rather than partitioning.

• Interested to learn more? Take CS161 or CS246!

Page 49: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

What's the connection between partitions and binary relations?

Page 50: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 51: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a a

Page 52: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a a

Page 53: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 54: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 55: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 56: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 57: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ab

Page 58: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 59: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 60: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 61: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a bc

Page 62: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

b c

Page 63: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ab c

Page 64: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ca

Page 65: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ba cb ca

ba ab

aa

∧ →

Page 66: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

→∧

aRa

aRb bRa

aRb bRc aRc

Page 67: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 68: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 69: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Reflexivity

Some relations always hold from any element to itself.

Examples:

• x = x for any x.

• A ⊆ A for any set A.

• x ≡ₖ x for any x.

Relations of this sort are called reflexive.

Formally speaking, a binary relation R over a set A is reflexive if the following first-order statement is true:

∀a ∈ A. aRa

(“Every element is related to itself.”)

Page 70: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Reflexivity Visualized

∀a ∈ A. aRa(“Every element is related to itself.”)

Page 71: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

Let R be therelation drawnto the left. Is R

reflexive?

Page 72: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

Page 73: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

a

Page 74: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

a

Page 75: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

a

This means that R is not reflexive, since the first-order logic

statement given below is not true.

Page 76: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is reflexive?

∀a ∈ A. aRa(“Every element is related to itself.”)

Page 77: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is reflexive?

∀a ∈ ??. a a

Page 78: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is reflexive?

∀a ∈ ??. a a

Reflexivity is a property of relations, not

individual objects.

Page 79: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 80: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 81: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Symmetry

In some relations, the relative order of the objects doesn't matter.

Examples:

• If x = y, then y = x.

• If x ≡ₖ y, then y ≡ₖ x.

These relations are called symmetric.

Formally: a binary relation R over a set A is called symmetric if the following first-order statement is true about R:

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

(“If a is related to b, then b is related to a.”)

Page 82: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Symmetry Visualized

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 83: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 84: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

b

Page 85: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

b

Page 86: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Is This Relation Symmetric?

Page 87: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 88: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

b

Page 89: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

b

Page 90: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

b

Page 91: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 92: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 93: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

Page 94: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

Page 95: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

Page 96: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Symmetric?

∀a ∈ A. ∀b ∈ A. (aRb → bRa)(“If a is related to b, then b is related to a.”)

a

Page 97: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 98: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

Page 99: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Transitivity

Many relations can be chained together.

Examples:

• If x = y and y = z, then x = z.

• If R ⊆ S and S ⊆ T, then R ⊆ T.

• If x ≡ₖ y and y ≡ₖ z, then x ≡ₖ z.

These relations are called transitive.

A binary relation R over a set A is called transitive if the following first-order statement is true about R:

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

(“Whenever a is related to b and b isrelated to c, we know a is related to c.)

Page 100: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Transitivity Visualized

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)(“Whenever a is related to b and b is

related to c, we know a is related to c.)

Page 101: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Transitive?

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)(“Whenever a is related to b and b is

related to c, we know a is related to c.)

Page 102: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Transitive?

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)(“Whenever a is related to b and b is

related to c, we know a is related to c.)

a

c

b

Page 103: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Transitive?

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)(“Whenever a is related to b and b is

related to c, we know a is related to c.)

a

c

b

Page 104: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is This Relation Transitive?

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)(“Whenever a is related to b and b is

related to c, we know a is related to c.)

a

c

b

Page 105: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations

An equivalence relation is a relation that is reflexive, symmetric and transitive.

Some examples:

• x = y

• x ≡ₖ y

• x has the same color as y

• x has the same shape as y.

Page 106: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Time-Out for Announcements!

Page 107: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Problem Set One Grades

• You need to read over the feedbackon your pset 1 as soon as possible.

• Easiest way to improve in this course is attempting problems and incorporating feedback.

• Almost the entire point of psets is to give practice.

Page 108: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Problem Set Two

• The problem set is due Thursday at 11:59PM.

• Have questions?

• Stop by office hours!

• Ask on Campuswire!

• General problem set policy reminders:

• Please tag your pages on Gradescope

• All solutions must be typed in LaTeX.

• Partners should only make one submission – put both partners names on the PDF and tag both partners on Gradescope

• Working in partners is encouraged!

Page 109: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Back to CS103!

Page 110: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

What's the connection between partitions and binary relations?

Page 111: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

xRy if x and y have the same shape

Page 112: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

xTy if x and y have the same color

Page 113: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Classes

Given an equivalence relation R over a set A, for any x ∈ A, the equivalence class of x is the set

[x]R = { y ∈ A | xRy }

Intuitively, the set [x]R contains all elements of A that are related to x by relation R.

Page 114: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

xRy if x and y have the same shape

[ ]R [ ]R[ ]R

Page 115: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

The Fundamental Theorem of Equivalence Relations: Let R be an equivalence relation over a set A. Then every element a ∈ A belongs to exactly one equivalence class of R.

Page 116: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

xRy if x and y have the same shape

[ ]R [ ]R[ ]R

Page 117: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

How’d We Get Here?

We discovered equivalence relations by thinking about partitions of a set of elements.

We saw that if we had a binary relation that tells us whether two elements are in the same group, it had to be reflexive, symmetric, and transitive.

The FToER says that, in some sense, these rules precisely capture what it means to be a partition.

Page 118: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Binary relations give us a common language to describe common

structures.

Page 119: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations IRL

Most modern programming languages include some sort of hash table data structure.

• Java: HashMap

• C++: std::unordered_map

• Python: dict

If you insert a key/value pair and then try to look up a key, the implementation has to be able to tell whether two keys are equal.

Although each language has a different mechanism for specifying this, many languages describe them in similar ways...

Page 120: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations IRL

“The equals method implements an equivalence relation on non-null object references:

• It is reflexive: for any non-null reference value x, x.equals(x) should return true.

• It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

• It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z)returns true, then x.equals(z) should return true.”

Java 8 Documentation

Page 121: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations IRL

“The equals method implements an equivalence relation on non-null object references:

• It is reflexive: for any non-null reference value x, .equals(x) should return true.

• It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

• It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z)returns true, then x.equals(z) should return true.”

Java 8 Documentation

Page 122: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations IRL

“Each unordered associative container is parameterized by Key, by a function object type Hash that meets the Hash requirements (17.6.3.4) and acts as a hash function for argument values of type Key, and by a binary predicate Pred that induces an equivalence relation on values of type Key. Additionally, unordered_map and unordered_multimap associate an arbitrary mapped type T with the Key.”

C++14 ISO Spec, §23.2.5/3

Page 123: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Equivalence Relations IRL

“Each unordered associative container is parameterized by Key, by a function object type Hash that meets the Hash requirements (17.6.3.4) and acts as a hash function for argument values of type Key, and by a binary predicate Pred that induces an equivalence relation on values of type Key. Additionally, unordered_map and unordered_multimap associate an arbitrary mapped type T with the Key.”

C++14 ISO Spec, §23.2.5/3

Page 124: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Prerequisite Structures

Page 125: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

CS106B

ProgrammingAbstractions

CS107Computer

Organization andSystems

CS110

Principles ofComputer Systems

CS103

CS109Intro to Probability

for ComputerScientists

CS161

Design and Analysis

of Algorithms

The CS CoreT

heory

MathematicalFoundations of

Computing

Page 126: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 127: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Pancakes

Everyone's got a pancake recipe. This one comes from Food Wishes (http://foodwishes.blogspot.com/2011/08/grandma-kellys-good-old-fashioned.html).

Ingredients

· 1 1/2 cups all-purpose flour

· 3 1/2 tsp baking powder

· 1 tsp salt

· 1 tbsp sugar

· 1 1/4 cup milk

· 1 egg

· 3 tbsp butter, melted

Directions

1. Sift the dry ingredients together.

2. Stir in the butter, egg, and milk. Whisk together to form the batter.

3. Heat a large pan or griddle on medium-high heat. Add some oil.

4. Make pancakes one at a time using 1/4 cup batter each. They're readyto flip when the centers of the pancakes start to bubble.

Page 128: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

MeasureFlour

MeasureBaking Pwdr

MeasureSalt

MeasureSugar

MeasureMilk

MeltButter

Beat Egg

Combine DryIngredients

HeatGriddle

OilGriddle

Add WetIngredients

MakePancakes

ServePancakes

Page 129: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Relations and Prerequisites

Let's imagine that we have a prerequisite structure with no circular dependencies.

We can think about a binary relation Rwhere aRb means

“a must happen before b”

What properties of R could we deduce just from this?

Page 130: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 131: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a a

Page 132: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

aa

Page 133: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 134: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 135: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 136: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a bc

Page 137: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

b c

Page 138: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ab c

Page 139: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ca

Page 140: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 141: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 142: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 143: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

a b

Page 144: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

ab

Page 145: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

aa

ba cb ca

∧ →

ba ab

Page 146: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

aRa

aRb ∧ bRc → aRc

aRb → bRa

Page 147: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

∀a ∈ A. ∀b ∈ A. ∀c ∈ A. (aRb ∧ bRc → aRc)

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 148: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 149: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 150: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity

Some relations never hold from any element to itself.

As an example, x ≮ x for any x.

Relations of this sort are called irreflexive.

Formally speaking, a binary relation R over a set A is irreflexive if the following first-order logic statement is true about R:

∀a ∈ A. aR̸a

(“No element is related to itself.”)

Page 151: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity Visualized

∀a ∈ A. aR̸a(“No element is related to itself.”)

Page 152: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 153: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is this relation reflexive?

Page 154: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

Is this relation reflexive?

Page 155: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

Is this relation reflexive?

Page 156: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa(“Every element is related to itself.”)

Is this relation reflexive?

Nope!

Page 157: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If
Page 158: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Is this relation irreflexive?

Page 159: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aR̸a(“No element is related to itself.”)

Is this relation irreflexive?

Page 160: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aR̸a(“No element is related to itself.”)

Is this relation irreflexive?

Page 161: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aR̸a(“No element is related to itself.”)

Is this relation irreflexive?

Nope!

Page 162: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Reflexivity and Irreflexivity

Reflexivity and irreflexivity are not negations of one another!

Here's the definition of reflexivity:

∀a ∈ A. aRa

What is the negation of the above statement?

∃a ∈ A. aRa̸

What is the definition of irreflexivity?

∀a ∈ A. aR̸a

Page 163: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

∀a ∈ A. aRa

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 164: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 165: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 166: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Asymmetry

In some relations, the relative order of the objects can never be reversed.

As an example, if x < y, then y ≮ x.

These relations are called asymmetric.

Formally: a binary relation R over a set A is called asymmetric if the following first-order logic statement is true about R:

∀a ∈ A. ∀b ∈ A. (aRb → bR̸a)

(“If a relates to b, then b does not relate to a.”)

Page 167: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Asymmetry Visualized

∀a ∈ A. ∀b ∈ A. (aRb → bR̸a)(“If a relates to b, then b does not relate to a.”)

Page 168: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Question to Ponder: Are symmetry and asymmetry negations of one another?

Page 169: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity

Transitivity

∀a ∈ A. ∀b ∈ A. (aRb → bRa)

Page 170: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Irreflexivity

Transitivity

Asymmetry

Page 171: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Strict Orders

A strict order is a relation that is irreflexive, asymmetric and transitive.

Some examples:

x < y.

a can run faster than b.

A ⊊ B (that is, A ⊆ B and A ≠ B).

Strict orders are useful for

• representing prerequisite structures,

• modeling dependencies,

• listing preferences,

• and so much more!

Page 172: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Strict Orders IRL

• In C++, many STL containers rely on strict orders to define the relative position of elements in terms of precedence of one item over other.

• Eg. the std::set which is implemented with a binary search tree.

• If you want to use std::sort, you have to provide a comparator function or overload the < operator.

• If you overload the < operator, C++ requires that the < relation be a strict order over the underlying type!

Page 173: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Recap

Binary Relations

• Reasoning about connections between objects.

Equivalence Relations

• Reasoning about clusters.

Strict Orders

• Reasoning about prerequisites.

Page 174: Binary Relationsweb.stanford.edu/class/archive/cs/cs103/cs103.1208...A binary relation over a set Ais a predicate Rthat can be applied to ordered pairs of elements drawn from A. If

Next Time

Proofs involving Binary Relations

• Equivalence Relation Proofs

• Alternative Perspectives on Partitions

• Proofs Involving Multiple Relations