1. Relations 关系 (1) | 离散数学

1,731 阅读13分钟

9 Relations 关系

《离散数学及其应用 第八版》(Discrete Mathematics and Its Application 8th Edition)第九章

2022.9.6

这是离散数学下的第一次课,主要介绍了关系的概念、性质、运算、应用、表示。

9.1 Relations and Their Properties 关系及关系的性质

9.1.1 Introductions 引言

Ordered pair 序偶

An ordered pair (a, b) is a listing of the objects a and b in a prescribed order.

  • 序偶是关系中基本且重要的一个概念,两个元素组成一个序偶时需要注意有顺序的区分。

Cartesian roduct 笛卡尔积

If A and B are two nonempty sets, we define the product set or Cartesian product A ×\times B as the set of all ordered pairs (a, b) with a \in A and b \in B

A ×\times B = {(a, b) | a \in A and b \in B}

  • 笛卡尔积的概念稍作复习,需要知道笛卡尔积中元素个数为 NA×NBN_{A}\times N_{B}

Definition of relations 关系的定义

Definition 1 Let A and B be two sets. A binary relation R from A to B is a subset of A ×\times B.

  • 首先明确从 A 到 B 的二元关系是 A 、B 两个集合笛卡尔积的子集。

If R \subseteq A ×\times B and (a, b) \in R, we say that a is related to b by R.

We use the notation a R b to denote that (a, b) \in R and a R b to denote that (a, b) \notin R, then (a, b) belongs to R, a is said to be related to b by R

  • 接着给出关系的定义,需要明确的是关系是 A 、B 内元素分别参与组成序偶的集合。

More generally, let A1, A2, ..., An be n sets. An n-ary relation R on these sets is a subset of A1 ×\times A2 ×\times ... ×\times An.

The sets Ai are known as the domains of the relation, and n as its degree.

  • 可以推广到 n 元关系,其中每一个集合 Ai 称为关系的n 称为关系的

Inverse Relations 逆关系

Any binary relation R from A to B has an inverse relation R-1, defined by
R-1 = ((b, a) | (a, b) \in R \land a \in A \land b \in B}.

  • 接下来给出逆关系的定义,只需要把括号内的 ab 交换即可得到逆关系。

9.1.2 Functions as relations 函数作为关系

A function f : AB is a special case of a relation from A to B

  • It satisfied the property: for each x \in A, there is exactly one pair (x, f(x)) in the relation
  • 函数是一种特殊的关系,但函数本身具有只能一对一或者多对一而不能多对多或一对多。

Relations are a generalization of functions

  • They allow unmapped elements from A
  • They allow one-to-many mappings where an element from A maps to multiple elements from B
  • They also allow many-to-one and many-to-many mappings
  • 关系可以看成函数的推广,因为关系允许未映射的元素、一对多、多对一、多对多。

9.1.3 Relations on Set 集合的关系

Definition 2 A relation R on a set A is a relation from A to A, that is, a subset of A2 = A ×\times A.

  • 集合 A 上的关系就是从 A 到 A 的一个关系,需要明确的是集合的关系是集合的二次幂的子集。

💥 重要问题

Pasted image 20220906203832.png

关于含 n 个元素集合上可以构成的的关系个数:

  • 关系是集合自身二次笛卡尔积的子集,也就是要求这个笛卡尔积的子集个数,很显然是 2n22^{n^{2}}

几种特殊的关系

假设任意非空集合 A ,可以定义集合 A 上的:

  • 空关系 \varnothing
  • 恒等关系 IA={(a,a)aA}I_{A} = \{ (a, a)\,|\,\forall a \in A \}
  • 全域关系 UA=A×A={(a,b)ab(aAbA)}U_{A} = A \times A = \{ (a, b)\, | \, \forall a\forall b (a\in A \land b\in A) \}

9.1.4 Properties of Relations 关系的性质

  • reflexive
  • irreflexive
  • symmetric
  • antisymmetric
  • asymmetric
  • transitive

Reflexive 自反性

Definition 3 A relation R on A is reflexive if \forall a \in A, a R a.

  • 一个在集合 A 上的关系具有自反性是说对于集合中的每个元素 a 都与自己有关系 R

Pasted image 20220907114947.png

看上面这个例子:

  • R1 具有自反性,因为每一个元素都与自身相等,这符合规定的小于等于关系,所以每一个元素都与自身有关系 R1 ,因此我们说它具有自反性。
  • R2 不具有自反性,因为每一个元素都与自身相等,这不符合规定的大于关系,所以每一个元素都与自身没有关系,它不具有自反性。
  • 其他关系同理可以分析其自反性。

Pasted image 20220907115346.png

看上面这个有向图表示关系的实例:

  • 在有向图表示的关系中,自反性体现为每一个元素(结点)都与自己有关系,即都有指向自己的环。

Irreflexive 反自反性

Definition 3 *A relation R on A is irreflexive if \forall a \notin A, a R a.

  • 一个在集合 A 上的关系具有反自反性是说对于集合中的每个元素 a 都与自己没有关系 R

Pasted image 20220907115617.png

看上面这个例子:

  • R1 中每一个元素都与自己相等,没有关系 a < b ,所以 R1 具有反自反性
  • R1 中每一个元素都与自己相等,没有关系 a 不等于 b ,所以 R2 具有反自反性

注意:一个不是自反的关系,不一定就是反自反的。

Pasted image 20220907115913.png

看上面这个例子:

  • R1 中 1 与自身有关系,2 与自身有关系,3 与自身没关系,所以既不是自反,也不是反自反。
  • R2 中 1 与自身有关系,2 与自身有关系,3 与自身有关系,全都与自身有关系,所以具有自反性。
  • R3 中 1 与自身没关系,2 与自身没关系,3 与自身没关系,全都与自身没关系,所以具有反自反性。

Pasted image 20220907120226.png

看上面这个有向图表示关系的实例:

  • 在有向图表示的关系中,反自反性体现为每一个元素(结点)都与自己没有关系,即全都与自身无环。

Symmestric 对称性

Definition 4 A binary relation R on A is symmetric iff R = R-1, that is, if (a, b) \in R \Leftrightarrow (b, a) \in R.

  • 关系的对称性是说一个关系的逆关系与这个关系相等,也就是说一旦里面两个元素之间有关系,这两个元素交换序偶得到的关系也符合这个关系。

Pasted image 20220907120714.png

看上面这个例子:

  • R1 是对称的,因为 a 和 b 序偶交换后不影响和,所以序偶 (a, b) 满足的关系 (b, a) 也会满足,因此具有对称性。
  • R2 是对称的,因为 a 和 b 序偶交换后不影响大小或者和(后面的 a = -b 可以写成 a + b = 0),因此具有对称性。
  • 其余的关系也可以判断出来。

Pasted image 20220907121151.png

看上面这个有向图表示关系的实例:

  • 在有向图表示的关系中,对称性体现为两个不同的结点间,可以无边,如果有边,必须成对出现,且方向相反(包括自环)。通俗地讲就是一旦有关系就需要两者之间双向奔赴。

Antisymmestric 反对称性

Definition 4 A relation R on a set A such that for all a, b \in A, if (a, b) \in R and (b, a) \in R, then a = b is called antisymmestric.

  • 关系的反对称性是说,一个集合上的关系,如果有两个元素之间有关系,且逆关系也和原关系相等,这两个元素一定是同一个元素。换句话说,不同的两个元素之间不能原关系和逆关系同时成立,但单个元素可以与自己有关系。

Pasted image 20220907122141.png

看上面这个例子:

  • R1 中自己与自己都有关系,但任意两个不同的数若存在小于等于的关系,反过来是不可能成立的,所以具有反对称性。
  • 其他关系也可以分析出来。

注意:可能有某种关系既是对称的又是反对称的(如单位矩阵表示的关系)

Pasted image 20220907122335.png

看上面这个有向图表示关系的实例:

  • 在有向图表示的关系中,反对称性体现为两个不同的结点间,可以无边,如果有边,只能由一条边。通俗地讲就是两个不同的元素之间只能有单向的关系不能有双向的关系,但单个元素可以与自己有关系也可以与自己没关系。

Asymmetric 非对称性

非对称性的定义:

xy[(x,y)R(y,x)R]\forall x \forall y\, [(x,y)\in R \to (y,x) \notin R]
  • 仔细理解上面的定义,他要求所有的关系都是单向关系,并且自己和自己不能有关系,因为如果自己和自己有关系的话上面的命题就为假。
  • 所以并不能把非对称性单纯地理解为对称性的对立事件,而是应该明确两点、只能有单项关系,自己和自己不能有关系。在图上体现为只能有单项边或者无边,且不能自环。

Transitve 传递性

Definition 5 A relation R is transitive iff for all a, b, c, (a, b) \in R \land (b, c) \in R \to (a, c) \in R.

  • 传递性就是如果 a 到 b 有关系 b 到 c 有关系则 a 到 c 也应该有关系,要不然就前件不成立也行。蕴含式前件不成立恒为真。

Pasted image 20220907130251.png

看上面用有向图表示关系的实例:

  • 如果两个元素之间有双向关系,两个元素需要有自环,好好理解定义。

注意:独立结点、空关系、恒等关系都是传递的

Pasted image 20220907130540.png

看上面这个例子:

  • R1 中只有自身和自身有关系,符合传递性
  • R2 中 1 到 2 有关系,2 到 3 有关系,但 1 到 3 没有关系,所以不具有传递性
  • R3 中没有符合定义中前件的关系,所以符合传递性

关系的性质总结

A relation R on a set A is :

  • reflexive, if
    • xA,(x,x)R\forall x \in A, \, (x, x)\in R
  • irreflexive, if
    • xA,(x,x)R\forall x \in A, \, (x, x) \notin R
  • symmetric, if
    • xy,(x,y)R(y,x)R\forall x\forall y,\, (x,y)\in R \Rightarrow (y,x)\in R
  • antisymmetric, if
    • xy,(x,y)R(y,x)Rx=y\forall x \forall y,\, (x,y)\in R \land (y,x)\in R \Rightarrow x = y
  • transitive, if
    • xyz,(x,y)R(y,z)R(x,z)R\forall x\forall y\forall z,\, (x,y)\in R \land (y,z)\in R\Rightarrow (x,z)\in R

Counting the reflexive relations

Pasted image 20220907144156.png

💥 重要问题

集合 A 有 n 个元素,有多少个自反关系:

  • 总共有 n2 个序偶,一共有 2n2 种关系,对角 n 个序偶确定,最后自反关系是 2n2-n 种自反关系。
  • 这一类题目要从 0-1 矩阵表示的关系去考虑计数问题。

9.1.5 Combining Relations 关系的组合

Since relations are by definition sets, they can be combined in any way that sets can be combined, including union, intersection, and set difference.

关系的运算:

  • 关系作为集合,适用集合的所有运算
  • 关系的合成运算
  • 关系的幂 —— 一种特殊的合成运算
  • 关系的逆运算
  • 关系的补运算

Pasted image 20220907145225.png

看上面的例子:

  • 将关系用集合的方式表示出来再按集合的运算组合即可。

Composition 关系合成

Definition 6 Let R be a relation from a set A to a set B and S a relation from B to a set C. The composite of R and S is the relation consisting of ordered pairs (a, c), where a ∈ A, c ∈ C, and for which there exists an element b ∈ B such that (a, b) ∈ R and (b, c) ∈ S. We denote the composite of R and S by S◦R.

  • 关系的合成可以用复合函数来理解,如果有中间关系就可以有连接的序偶
  • 注意: R 到 S 的关系写的时候反过来 S◦R
  • 关系合成满足结合律

Pasted image 20220907150200.png

两种合成方式(过河拆桥):

  • 有向图法,直接找到可以传递的关系即可
  • 枚举法,在序偶对中前一个关系中的序偶后面的元素作为后一个关系中序偶前面的元素

Power of a Relation 关系的幂

Definition 7 Let R be a relation on the set A. The powers Rn, n = 1, 2, 3,…, are defined recursively by

R1 = R and Rn+1 = Rn ◦ R.

  • 关系的幂由递归定义,就是 n 个 R 合成。

Negative powers of R can also be defined if desired, by

R-n = (R-1)n R0 = R1 ◦ R-1 = IA

  • 指数为 0 和负数的时候也可以定义关系的幂,更多时候放到矩阵里做乘法来看

Theorem 1 The relation R on a set A is transitive if and only if Rn ⊆ R for n = 1, 2, 3,….

  • 关系的幂有一个重要定理:集合 A 上的关系 R 具有传递性的充要条件是 R 的幂永远是 R 的子集。
  • 用数学归纳法证明,过程如下:

Pasted image 20220907152326.png

最后的核心在于已知 (a, b) 在关系的幂上传递而来,证明了 (a, b) 在 R 上传递而来,则关系的幂就是原关系的子集。

拓展特性:将关系的多重幂取并集,本来不具有传递性的关系会变得有传递性

Pasted image 20220907153359.png

Pasted image 20220907153424.png


关系求逆运算的性质

Pasted image 20220907153655.png

可以从关系的 0-1 矩阵表示理解。


课后作业:

Pasted image 20220907135041.png

Pasted image 20220907134939.png

Pasted image 20220907134957.png

9.2 n-ary Relations and Their Applications n元关系及应用

9.2.1 n-nary Relations n元关系

Definition 1 Let A1, A2,…, An be sets. An n-ary relation on these sets is a subset of A1 × A2 ×⋯× An. The sets A1, A2,…, An are called the domains of the relation, and n is called its degree.

  • n 元关系是 n 个集合之间的笛卡尔积
  • 各集合称为 n元关系的
  • n 称为关系的或者叫

Pasted image 20220907154504.png


9.2.2 Relational Databases 关系型数据库

A relational database is essentially just an n-ary relation R

  • 一种基于关系概念构建数据库的方法称为关系型数据库模型

A domain Ai is a primary key for the database if the relation R is functional in Ai

  • primary key,主键:通常选择唯一 n 元组的域作为主键

A composite key for the database is a set of domains {Ai, Aj, ...} such that R contains at most 1 n-tuple (..., ai, ..., aj, ...) for each composite value (ai, aj, ...) \in Ai × Aj ×⋯

  • composite key,复合主键:当无主键时,可以选多个域的组合为复合主键

Pasted image 20220907160119.png


9.2.3 Operations on n-ary Relations n元关系的运算

Selection operator 选择

Definition 2 Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the selection operator sC maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C.

  • 选择操作符就是按照热特点条件选出符合的关系

Pasted image 20220907160534.png


Projection operator 投影

Definition 3 The projection Pi1i2, …,im where i1 < i2 < ⋯ < im, maps the n-tuple (a1, a2,…, an) to the m-tuple (ai1 , ai2 , …, aim), where m ≤ n.

  • 相当于是一种只看某几列

Pasted image 20220907161049.png


Join operator 连接

Definition 4 Let R be a relation of degree m and S a relation of degree n. The join Jp(R, S), where p ≤ m and p ≤ n, is a relation of degree m + n − p that consists of all (m + n − p)-tuples (a1, a2,…, am-p, c1, c2,…, cp, b1, b2,…, bn-p), where the m-tuple (a1, a2,…, am-p, c1, c2,…, cp) belongs to R and the n-tuple (c1, c2,…, cp, b1, b2,…, bn-p) belongs to S.

  • 根据共同的域来将两种关系连接称为更长的关系

Pasted image 20220907161608.png


9.2.4 Association Rules from Data Mining 数据挖掘的关联规则

  • 关联规则(Association Rules):从大量数据中挖掘潜在的有价值的关联关系
  • 事务(Transaction):每一次购买的活动是一个事务
  • 项(Item):每一个商品是一个项
  • 项集(Item set):项的集合是项集
  • 支持度(support):某一个或多个项同时在事务中出现的频率占比

Pasted image 20220907161757.png

比如下面这个例子:

Pasted image 20220907162024.png

我们可以通过记录事务的表中计算苹果这个项的支持度

Pasted image 20220907162226.png

  • 置信度(Confidence):当在某一项成立的前提下同时含有另一项的频率。有点像条件概率

Pasted image 20220907162036.png


课后作业

Pasted image 20220907143820.png

Pasted image 20220907143848.png

9.3 Representing Relations 关系的表示

9.3.1 Using Zero-One Matrices 0-1矩阵

To represent a binary relation R: A ×\times B by an |A| ×\times |B| 0-1 matrix MR = [ m ij ], let mij = 1 iff (ai, bj) \in R.

  • 用 0-1 矩阵表示二元关系,1 表示有关系,0 表示没关系
  • 用处很大

关系的性质在 0-1 矩阵表示的关系中的体现

Pasted image 20220907163130.png

  • 自反性:主对角线元素全为 1 ,其余元素没有要求
  • 反自反性:主对角线元素全为 0 ,其余元素没有要求
  • 对称性:主对角线元素任意,但其他元素需要跟主对角线保持对称,隐含着确定了一半就确定另一半,在计算特定关系数的时候有用
  • 反对称性:主对角线元素任意,但其他关于主对角线对称的每组元素只有三种情况:0-0、0-1、1-0 ,关系计数有用

Combining Connection Matrices

  • 关系的并:对应矩阵的并
  • 关系的交:对应矩阵的交

Pasted image 20220907164119.png

  • 关系的合成(复合):对应矩阵相乘

Pasted image 20220907164431.png

  • 关系的幂:对应矩阵的幂

Pasted image 20220907164541.png


9.3.2 Using Directed Graphs 有向图

Definition 1 A directed graph, or digraph, consists of a set V of vertices (or nodes) together with a set E of ordered pairs of elements of V called edges (or arcs). The vertex a is called the initial vertex of the edge (a, b), and the vertex b is called the terminal vertex of this edge.

  • 有向图的定义

The relation R on a set A is represented by the directed graph that has the elements of A as its vertices and the ordered pairs (a, b), where (a, b) ∈ R, as edges.

  • 用有向图表示关系,结点是元素,边是序偶

关系的性质在有向图表示关系中的体现

Pasted image 20220907165309.png

  • 自反性:每一个结点都有自环
  • 反自反性:每一个结点都没有自环
  • 对称性:每两个结点间如果有连接则一定是双向的,要么就没有连接

关系的表示小结

Pasted image 20220907165457.png

Pasted image 20220907165545.png

课后作业

Pasted image 20220907143953.png

Pasted image 20220907144014.png

下一节