31
CSCI 115 Chapter 4 Relations and Digraphs

CSCI 115 Chapter 4 Relations and Digraphs. CSCI 115 §4.1 Product Sets and Partitions

Embed Size (px)

Citation preview

CSCI 115

Chapter 4

Relations and Digraphs

CSCI 115

§4.1

Product Sets and Partitions

§4.1 – Product Sets and Partitions

• Product Set– Ordered pair– Cartesian Product

• Theorem 4.1.1– For any 2 finite non-empty sets A and B,

|A x B| = |A||B|

§4.1 – Product Sets and Partitions

• Partitions

– A partition (quotient set) of A is a set P of nonempty subsets of A such that:

1. Each element of A belongs to a set in P

2. If A1 and A2 are elements of P, then A1A2 = {}

– Each element of P is called a cell or a block

CSCI 115

§4.2

Relations and Digraphs

§4.2 – Relations and Digraphs

• Relations– Relation from A to B

• Subset of A x B

– Relation on A• Subset of A x A

§4.2 – Relations and Digraphs

• Sets arising from relations– Domains

• Domain of relation

– Ranges• Range of relation• Range of an element• Range of a subset

§4.2 – Relations and Digraphs

• Theorem 4.2.1– Let R be a relation from A to B, and let A1 and A2 be

subsets of A. Then:i. If A1 A2, then R(A1) R(A2)

ii. R(A1 A2) = R(A1) R(A2)

iii. R(A1 A2) R(A1) R(A2)

• Theorem 4.2.2– Let R and S be Relations from A to B. If R(a) = S(a)

a A, then R = S.

§4.2 – Relations and Digraphs

• The matrix of a relation

– If A = {a1, a2, …, am} and B = {b1, b2, …, bn}, then a relation R from A to B can be represented by an m x n boolean matrix (MR) constructed as follows:

mij = {1 if (ai, bj) R0 if (ai, bj) R

§4.2 – Relations and Digraphs

• The Digraph of a relation– Let R be a relation on A. The Digraph of R is

constructed as follows:

1. Draw a circle for each element in A, and label the circles accordingly (these are called vertices)

2. Draw an arrow from ai to aj iff aiRaj (these are called edges)

– In-degrees and out-degrees

§4.2 – Relations and Digraphs

• Relation Restriction– Let R be a relation on a set A, with B A. The

restriction of R to B is R (B x B).

CSCI 115

§4.3

Paths in Relations and Digraphs

§4.3 – Paths in Relations and Digraphs

• Path of length n from a to b:– aRx1, x1Rx2, x2Rx3, …, xn-1Rb

– Geometric path in digraph

• Cycle: A path that begins and ends at the same vertex

§4.3 – Paths in Relations and Digraphs

• New relations from paths– Rn: xRny iff there is a path of length n from

x to y– R: xRy iff there is any path from x to y

• Connectivity Relation

– R*: xR*y iff there is a path from x to y, or x = y• Reachability Relation

§4.3 – Paths in Relations and Digraphs

• Theorem 4.3.1– If R is a relation on A = {a1, a2, …, an}, then

MR2 = MR MR = ((MR))2

• Theorem 4.3.2– For n 2 and R a relation on a finite set A, we

have MRn = MR MR... MR (n factors)

§4.3 – Paths in Relations and Digraphs

• Path composition– is used as a variable for paths– Assume we have:

• 1: a, x1, x2, …, xn-1, b• 2: b, y1, y2, …, ym-1, c

– Then, we can compose 1 and 2 as follows:• 21: a, x1, x2, …, xn-1, b, y1, y2, …, ym-1, c• 21 is a path of length n + m

– 12 does not make sense

CSCI 115

§4.4

Properties of Relations

§4.4 – Properties of Relations

• A relation R on a set A is:– Reflexive iff (a,a) R a A– Irreflexive iff (a,a) R a A– Symmetric iff (a,b) R (b,a) R– Asymmetric iff (a,b) R (b,a) R– Antisymmetric iff (a,b) R and (b,a) R a = b– Transitive iff (a,b) R and (b,c) R (a,c) R

§4.4 – Properties of Relations

• Theorem 4.4.1– A relation R is transitive iff it satisfies the following

property: If there is a path of length greater than 1 from vertex a to vertex b, there is a path of length 1 from a to b (i.e. aRb). In other words, R is transitive iff Rn R n > 1.

§4.4 – Properties of Relations

• Theorem 4.4.2– Let R be a relation on a set A. Then:

i. Reflexivity of R means a R(a) a Aii. Symmetry of R means a R(b) iff b R(a)

iii. Transitivity of R means b R(a) and c R(b) implies c R(a)

§4.4 – Properties of Relations• Other properties:

• The digraph of a reflexive relation has a cycle of length 1 at every vertex• The digraph of an irreflexive relation has no cycles of length 1• If R is reflexive then the Domain(R) = Range(R) = A• The matrix of a reflexive relation has all 1s on the diagonal• The matrix of an irreflexive relation has all 0s on the diagonal• The matrix of a symmetric relation has mij = mji i,j (MR = MR

T)

• The matrix of an asymmetric relation meets the following 2 criteria:• mij = 1 mji = 0 AND the diagonal must be all 0s

• The matrix of an antisymmetric relation meets the following criteria:• If ij, then mij = 0 or mji = 0

• The Matrix of a transitive relation meets the following criteria:• MR must have a 1 everywhere ((MR))2 has a 1

§4.4 – Properties of Relations

• Graph of a symmetric relation– Undirected edge– Adjacent vertices

• Connected graphs• Disconnected graphs

CSCI 115

§4.5

Equivalence Relations

§4.5 – Equivalence Relations

• Equivalence Relation– A relation R on a set A is an equivalence

relation if it is reflexive, symmetric, and transitive

§4.5 – Equivalence Relations

• Theorem 4.5.1– Let P be a partition of a set A such that

P = {A1, A2, …, An}. Define a relation R on A as follows:aRb iff aAi and bAi (i {1, 2, …, n}). Then R is an equivalence relation on A.

– Here R is called the equivalence relation determined by P.

§4.5 – Equivalence Relations

• Lemma 4.5.1– Let R be an equivalence relation on A. Let

a, b A. Then aRb iff R(a) = R(b).• Theorem 4.5.2

– Let R be an equivalence relation on A, and let P be the collection of all distinct relative sets R(a) for a in A. Then P is a partition of A, and R is the equivalence relation determined by P.

• Note: When R is an equivalence relation on A, the sets R(a) are called equivalence classes. The partition constructed in Theorem 4.5.2 is denoted A/R.

§4.5 – Equivalence Relations

Procedure to determine A/R (A finite or countable):1. Choose any aA and find R(a).

2. If R(a) A, choose bA, bR(a) and find R(b).

3. If A is not the union of the computed equivalence classes, choose xA such that x is not in any of the equivalence classes, and find R(x).

4. Repeat step 3 until A is accounted for, or a pattern emerges which describes the equivalence classes.

5. A/R is the partition formed by using each equivalence class as a cell.

CSCI 115

§4.6

Data Structures for Relations and Digraphs

§4.6 – Data Structures for Relations and Digraphs

• Linked list data structure– Storage cell

• Data• Pointer

• Implementations– Structures– Arrays

• We will use the array implementation

§4.6 – Data Structures for Relations and Digraphs

• Implementation 1 – Start, Tail, Head, Next1. Fill in tail and head arrays

2. Fill in start variable

3. Fill in next array

• The tail, head and next arrays have as many elements as the digraph has edges

• Start is a simple variable (not an array)

§4.6 – Data Structures for Relations and Digraphs

• Implementation 2 – Vert, Tail, Head, Next1. Fill in tail and head arrays

2. Fill in next array (within vertex)

3. Fill in vert array. The data in vert is the first subscript in tail that corresponds to the current vertex

• The tail, head and next arrays have as many elements as the digraph has edges

• The vert array has as many elements as the digraph has vertices