0. 前言
本节内容主要是对离散数学基本概念的一个梳理,以及回答老师博客中的问题。
1. 集合
1.1 定义
- 集合:由一个或多个确定的元素所构成的整体。一般用大写字母 A \mathbf A A, B \mathbf B B, C \mathbf C C 等表示。
- 元素:构成集合的事物。
1.2 常见数集
- 自然数集: N = { 0 , 1 , 2 , … } \mathbf N = \{0,1,2,\ldots\} N={0,1,2,…}
- 整数集: Z = { … − 2 , − 1 , 0 , 1 , 2 , … } \mathbf{Z}=\{\ldots-2,-1,0,1,2, \ldots\} Z={…−2,−1,0,1,2,…}
- 有理数集: Q = { x ∣ x = p / q , p , q ∈ Z } \mathbf{Q}=\{x \mid x=p / q, p, q \in \mathrm{Z}\} Q={x∣x=p/q,p,q∈Z}
- 复数集: C = { x ∣ x = a + b i , a , b ∈ R , i = − 1 } \mathbf{C}=\{x \mid x=a+b i, a, b \in \mathbf{R}, i=-1\} C={x∣x=a+bi,a,b∈R,i=−1}
1.3 集合元素性质
- 无序性:集合中元素之间无序
- 互异性:每个元素只能出现一次
- 确定性:给定某个集合和某个元素,该元素必定是属于或者不属于该集合
- 任意性:集合的元素也可以是集合。
1.4 基数
简单理解,集合的基数就是集合中元素的个数。一般对于集合 A \mathbf{A} A,其基数可以表示为: ∣ A ∣ |\mathbf{A}| ∣A∣
1.5 笛卡尔积
了解笛卡尔积之前,需要明确什么是有序组(又名有序对)什么是无序组(也名无序对)。
首先,对于2个元素
a
,
b
a,b
a,b,如果它们有次序之分,那么就称其为二元有序组,记作
<
a
,
b
>
<a,b>
<a,b> ;若无次序之分,就称其为二元无序组,记作
(
a
,
b
)
(a,b)
(a,b)
若有两个集合,集合
X
\mathbf X
X 和
Y
\mathbf Y
Y ,它们的笛卡尔乘积就是:
X
×
Y
=
{
<
x
,
y
>
∣
x
∈
X
∧
y
∈
Y
}
\mathbf X × \mathbf Y = \{<x,y>\mid x\in \mathbf X\wedge y \in \mathbf Y\}
X×Y={<x,y>∣x∈X∧y∈Y}
也就是说,笛卡尔积是有序对的集合。
举个例子:
X
=
{
a
,
b
}
\mathbf X=\{a,b\}
X={a,b},
Y
=
{
1
,
2
,
3
}
\mathbf Y=\{1,2,3\}
Y={1,2,3}
X
×
Y
=
{
<
a
,
1
>
,
<
a
,
2
>
,
<
a
,
3
>
,
<
b
,
1
>
,
<
b
,
2
>
,
<
b
,
3
>
}
\mathbf X ×\mathbf Y=\{<a,1>,<a,2>,<a,3>,<b,1>,<b,2>,<b,3>\}
X×Y={<a,1>,<a,2>,<a,3>,<b,1>,<b,2>,<b,3>}
2. 元组
元组这个概念在学习数据库的时候,在关系数据库中,关系是一张表,表中的每一行可以称为一条记录,也可以称为一个元祖。
在 python 中,对于元组的定义是这样的:由一系列变量组成的 不可变 序列容器。即元组一旦创建,就不能再对其进行增删改操作了。
3. 问题
① 习题 1: { 0 , 1 , { 0 , 1 } , { 1 , 2 } } \{ 0 ,1 , \{ 0 , 1 \} , \{ 1 , 2 \} \} {0,1,{0,1},{1,2}} 有几个元素? 机器学习中, 这类形式的集合有什么优点和缺点?
回答:
根据集合元素的性质,可以确定上述集合中有4个元素,分别是
0
,
1
,
{
0
,
1
}
,
{
1
,
2
}
0,1,\{0,1\},\{1,2\}
0,1,{0,1},{1,2}。
机器学习中,这种集合一般表示的是分类的问题。类别一般用第0、1、2类等来表示,上边的4个元素,其中不乏元素本身是集合的情况,也就是说它的标签并不是唯一的。对于属于多个类别的样本,可以很好表示其所属类别。
② 习题 2: ∅ \emptyset ∅ 的基数是多少? { ∅ } \{\emptyset\} {∅} 呢?
回答: ∅ \emptyset ∅ 的基数是0, { ∅ } \{\emptyset\} {∅} 的基数是1。个人认为这个问题还是对于基数概念的一个理解,集合中元素的个数。首先, ∅ \emptyset ∅ 是空集,即一个空的集合,没有元素,所以基数为0;而对于 { ∅ } \{\emptyset\} {∅},实际上这是包含一个元素,且该元素是空集的集合,所以其基数为1。
③ 习题 5: 多标签学习中, 输出为一个向量,相应的学习器算不算函数呢?
回答:
首先,传统的对于函数的定义是,对于非空的两个数集
A
\mathrm A
A
B
\mathrm B
B,它们存在某种对应关系使得对于
A
\mathrm A
A 中的任意一个数,在
B
\mathrm B
B 中都有唯一确定的一个值与其对应。而在机器学习中,虽然输出的是一个向量,但是对于它的每个输入,都存在其对应的一个输出,所以是可以把它相应的学习器看成函数的。
④ 习题 6: 元组只能表达对象的数据部分, 还是可以完整地表达? 用一个具体的程序来说明.
回答:
元组可以完整的表达。
# 定义元组
tupleHobby = ("sing", "dance")
tuplePerson = ("Lucy", "girl", 13, tupleHobby)
# 获取类型
print(type(tuplePerson))
print(tuplePerson)
print(tuplePerson[3])
tupleTest = ("sing", "reading")
# 比较元组
print(tupleHobby == tupleTest)
输出:
<class 'tuple'>
('Lucy', 'girl', 13, ('sing', 'dance'))
('sing', 'dance')
False
⑤ 习题 7: 定义二叉树。
首先,观察了老师 对于树结构的定义如下:
A tree is a triple
T
T
T =
(
V
,
r
,
p
)
(\mathbf V, r, p)
(V,r,p),where
V
=
{
v
1
,
.
.
.
,
v
n
}
\mathbf V=\{v_1, ...,v_n\}
V={v1,...,vn} is the set of nodes,
r
∈
V
r \in \mathbf V
r∈V is the root, and
p
:
V
∖
{
r
}
→
V
p : \mathbf V \setminus\{r\} \rightarrow \mathbf V
p:V∖{r}→V is the parent function satisfying
a)
∀
k
≥
1
\forall k≥1
∀k≥1,
p
k
(
v
)
≠
v
p^k(v)≠v
pk(v)=v, and
b)
∀
v
∈
V
∖
{
r
}
\forall v\in \mathbf V \setminus\{r\}
∀v∈V∖{r},
∃
\exists
∃ 1
k
≥
1
k≥1
k≥1, st.
p
k
(
v
)
=
r
p^k(v)=r
pk(v)=r
说明:
a)
∃
\exists
∃ 1表示存在唯一
b) 第一个条件表示树是没有环的,即没有回路的;第二个条件表示是连通至根节点的
c)
p
1
(
v
)
=
p
(
v
)
p^1(v)=p(v)
p1(v)=p(v);
p
k
(
v
)
=
p
(
p
k
−
1
(
v
)
)
p^k(v)=p(p^{k-1}(v))
pk(v)=p(pk−1(v))
对一棵树, 每个节点存在0个或多个子节点,只有一个父节点,没有父节点的节点是根节点。
对于二叉树
个人理解:二叉树相较于树,除了树本身具有的特点之外,二叉树的限制条件是每个节点最多2个子树。在树定义的基础上,需要对树的左右子树进行定义。且二叉树的形式一般有5种:① 空树;② 只有根节点;③ 只有左子树;④ 只有右子树;⑤ 左右子树都有。
感觉二叉树的定义要比树复杂得多,需要对其左右子树做个定义。感觉要合理定义比较困难。
关于二叉树的定义,看了老师的博客,可以这么定义。
老师的博客地址:https://blog.youkuaiyun.com/minfanphd/article/details/116582678
Let Σ = { l , r } \Sigma=\{l,r\} Σ={l,r} be the alphabet and ϕ \phi ϕ be a bull node. A binary tree is a triple T = ( V , r , c ) T=(\mathbf V,r,c) T=(V,r,c), where V = { v 1 , v 2 , . . . , v n } \mathbf V=\{v_1,v_2,...,v_n\} V={v1,v2,...,vn} is the set of nodes, r ∈ V r\in\mathbf V r∈V is the root, and c : V ∪ { ϕ } × Σ + → V ∪ { ϕ } c:\mathbf V\cup\{\phi\}\times\Sigma^+\rightarrow\mathbf V\cup\{\phi\} c:V∪{ϕ}×Σ+→V∪{ϕ} satisfying ∀ v ∈ V \forall v\in \mathbf V ∀v∈V, ∃ 1 \exist1 ∃1 s ∈ Σ + s\in\Sigma^+ s∈Σ+ st. c ( r , s ) = v c(r,s)=v c(r,s)=v
⑥ 习题 8: 定义带权无向图。
首先,老师 的博客中给出了图、有向图、无向图、带权有向图的定义如下:
图的定义:
A graph is a tuple
G
=
(
V
,
E
)
G = (\mathbf V, \mathbf E)
G=(V,E), where
V
=
{
v
1
,
v
2
,
.
.
.
,
v
n
}
\mathbf V=\{v_1, v_2, ... , v_n\}
V={v1,v2,...,vn} is the set of nodes, and
E
⊆
V
×
V
\mathbf E \subseteq \mathbf V ×\mathbf V
E⊆V×V is the set of edges.
有向图的定义:
A directed graph is a tuple
G
d
=
(
V
,
E
)
G_d = (\mathbf V, \mathbf E)
Gd=(V,E), where
V
=
{
v
1
,
v
2
,
.
.
.
,
v
n
}
\mathbf V=\{v_1, v_2, ... , v_n\}
V={v1,v2,...,vn} is the set of nodes, and
E
⊆
V
×
V
\mathbf E \subseteq \mathbf V ×\mathbf V
E⊆V×V is the set of edges.
无向图的定义:
A undirected graph is a tuple
G
u
=
(
V
,
E
)
G_u = (\mathbf V, \mathbf E)
Gu=(V,E), where
V
=
{
v
1
,
v
2
,
.
.
.
,
v
n
}
\mathbf V=\{v_1, v_2, ... , v_n\}
V={v1,v2,...,vn} is the set of nodes, and
E
⊆
V
×
V
\mathbf E \subseteq \mathbf V ×\mathbf V
E⊆V×V is the set of edges, and
⟨
v
i
,
v
j
⟩
∈
E
\langle v_{i}, v_{j}\rangle \in \mathbf E
⟨vi,vj⟩∈E iff
⟨
v
j
,
v
i
⟩
∈
E
\langle v_{j}, v_{i}\rangle \in \mathbf E
⟨vj,vi⟩∈E
其中,因为是无向图,所以任意两个顶点组成的元组应该是无序的,即这里的
⟨
v
i
,
v
j
⟩
\langle v_{i}, v_{j}\rangle
⟨vi,vj⟩ 和
⟨
v
j
,
v
i
⟩
\langle v_{j}, v_{i}\rangle
⟨vj,vi⟩ 实际与
(
v
i
,
v
j
)
(v_{i}, v_{j})
(vi,vj) 表达的含义相同。
带权有向图的定义:
A weighted directed graph is a tuple
G
w
=
(
V
,
w
)
G_w = (\mathbf V, w)
Gw=(V,w), where
V
=
{
v
1
,
v
2
,
.
.
.
,
v
n
}
\mathbf V=\{v_1, v_2, ... , v_n\}
V={v1,v2,...,vn} is the set of nodes, and
w
:
V
×
V
→
R
+
∪
{
0
}
w: \mathbf V ×\mathbf V \rightarrow \mathbb R^+\cup \{0\}
w:V×V→R+∪{0} is the edge weight function.
其中,
a)
R
+
\mathbb R^+
R+ 表示正实数(不含0),故需要把0算进去,表示成非负实数;
b) 若将
w
w
w 的函数值限定为0或1,即
w
:
V
×
V
→
{
0
,
1
}
w: \mathbf V ×\mathbf V \rightarrow \{0,1\}
w:V×V→{0,1},带权有向图就退化为有向图了。
带权无向图:
A weighted undirected graph is a tuple G u w = ( V , E , w ) G_uw = (\mathbf V, \mathbf E, w) Guw=(V,E,w), where V = { v 1 , v 2 , . . . , v n } \mathbf V=\{v_1, v_2, ... , v_n\} V={v1,v2,...,vn} is the set of nodes, E ⊆ V × V \mathbf E \subseteq \mathbf V ×\mathbf V E⊆V×V is the set of edges, and w : V × V → R + ∪ { 0 } w: \mathbf V ×\mathbf V \rightarrow \mathbb R^+\cup \{0\} w:V×V→R+∪{0} is the edge weight function, and ⟨ v i , v j ⟩ ∈ E \langle v_{i}, v_{j}\rangle \in \mathbf E ⟨vi,vj⟩∈E iff ⟨ v j , v i ⟩ ∈ E \langle v_{j}, v_{i}\rangle \in \mathbf E ⟨vj,vi⟩∈E.
⑦ 习题 9: 考虑 ϕ ϕ ϕ ,重新定义二叉树
Let Σ = { l , r } \Sigma=\{l,r\} Σ={l,r} be the alphabet and ϕ \phi ϕ be a bull node. A binary tree is a triple T = ( V , r , c ) T=(\mathbf V,r,c) T=(V,r,c), where V = { v 1 , v 2 , . . . , v n } \mathbf V=\{v_1,v_2,...,v_n\} V={v1,v2,...,vn} is the set of nodes, r ∈ V r\in\mathbf V r∈V is the root, and c : V ∪ { ϕ } × Σ ∗ → V ∪ { ϕ } c:\mathbf V\cup\{\phi\}\times\Sigma^*\rightarrow\mathbf V\cup\{\phi\} c:V∪{ϕ}×Σ∗→V∪{ϕ} satisfying ∀ v ∈ V \forall v\in \mathbf V ∀v∈V, ∃ 1 \exist1 ∃1 s ∈ Σ ∗ s\in\Sigma^* s∈Σ∗ st. c ( r , s ) = v c(r,s)=v c(r,s)=v
4. 有限状态自动机
老师的博客:https://blog.youkuaiyun.com/minfanphd/article/details/116582575?spm=1001.2014.3001.5501
首先,看看有限自动机的定义。以下定义出自老师的博客。
确定的有穷状态自动机定义如下:
A deterministic finite state automata (DFA) is a 5-tuple M = ( Σ , Q , q 0 , T , f ) M=(\Sigma,\mathbf Q,\mathbf q_0, \mathbf T,f ) M=(Σ,Q,q0,T,f), where
a) Σ \Sigma Σ is the alphabet;
b) Q \mathbf Q Q is the set of states;
c) q 0 ∈ Q \mathbf q_0 \in \mathbf Q q0∈Q is the start of state;
d) T ⊆ Q \mathbf T \subseteq \mathbf Q T⊆Q is the set of terminal states;
e) f : Q × Σ ∗ → Q f:\mathbf Q×\Sigma^*\rightarrow\mathbf Q f:Q×Σ∗→Q is the transition function.
Any s ∈ Σ ∗ s\in\Sigma^* s∈Σ∗ is accepted by the automata iff f ( q 0 , s ) ∈ T f(\mathbf q_0,s)\in\mathbf T f(q0,s)∈T
说明:
a) DFA的开始状态只有一个,故为 Q \mathbf Q Q 的元素;
b) DFA的终止状态可以有多个,故为 Q \mathbf Q Q 的子集;
c) DFA的基础目标是看状态 s s s 是否合法(被接受)
习题 3.1 模仿自动机的样子来重新定义二叉树。
老师认为,可以将二叉树看做是自动机。因为它满足如下的条件:
a) 有一个字母表,即 ( l , r ) (l,r) (l,r);
b) 有一个状态集合,包含所有的节点和空节点;
c) 有一个开始状态,即根节点 r r r;
d) 有一个终止状态,即空节点 ϕ \phi ϕ,但此处有差别;
e) 从任一状态读入任一字母,确定地转移到下一状态(可以是自己),即可用状态函数来描述。
由此,对二叉树进行定义,以自动机这种方式。
A binary tree is a 5-tuple
T
T
T =
(
Σ
,
Q
,
r
,
ϕ
,
c
)
(\Sigma,\mathbf Q,r, \phi,c)
(Σ,Q,r,ϕ,c),where
a)
Σ
\Sigma
Σ is the alphabet, and
Σ
=
{
l
,
r
}
\Sigma=\{l,r\}
Σ={l,r};
b) let
V
=
{
v
1
,
.
.
.
,
v
n
}
\mathbf V=\{v_1,...,v_n\}
V={v1,...,vn} be the set of nodes and
ϕ
\phi
ϕ be null node.
Q
\mathbf Q
Q is the set of states, and
Q
=
V
∪
{
ϕ
}
\mathbf Q=\mathbf V\cup\{\phi\}
Q=V∪{ϕ};
c)
r
∈
Q
r \in \mathbf Q
r∈Q is the root;
d)
ϕ
⊆
Q
\phi \subseteq \mathbf Q
ϕ⊆Q is the set of terminal states;
e)
c
:
Q
×
Σ
∗
→
Q
c:\mathbf Q×\Sigma^*\rightarrow\mathbf Q
c:Q×Σ∗→Q is the transition function, satisfying
∀
v
∈
Q
∖
{
ϕ
}
\forall v\in\mathbf Q\setminus\{\phi\}
∀v∈Q∖{ϕ},
∃
1
\exist1
∃1
s
∈
Σ
∗
s\in\Sigma^*
s∈Σ∗ st.
c
(
r
,
s
)
=
v
c(r,s)=v
c(r,s)=v
习题3.2 模仿自动机的样子来重新定义树。
个人感觉以自动机的方式定义树和二叉树的区别在于字母表,故定义如下:
A tree is a 5-tuple
T
T
T =
(
Σ
,
Q
,
r
,
ϕ
,
f
)
(\Sigma,\mathbf Q,r, \phi,f)
(Σ,Q,r,ϕ,f),where
a)
Σ
\Sigma
Σ is the alphabet;
b) let
V
\mathbf V
V is the set of nodes,
ϕ
\phi
ϕ is null node.
Q
\mathbf Q
Q is the set of states, and
Q
=
V
∪
{
ϕ
}
\mathbf Q=\mathbf V\cup\{\phi\}
Q=V∪{ϕ};
c)
r
∈
Q
r \in \mathbf Q
r∈Q is the root;
d)
ϕ
⊆
Q
\phi \subseteq \mathbf Q
ϕ⊆Q is the set of terminal states;
e)
f
:
Q
×
Σ
∗
→
Q
f:\mathbf Q×\Sigma^*\rightarrow\mathbf Q
f:Q×Σ∗→Q is the transition function, satisfying
∀
v
∈
Q
∖
{
ϕ
}
\forall v\in\mathbf Q\setminus\{\phi\}
∀v∈Q∖{ϕ},
∃
1
\exist1
∃1
s
∈
Σ
∗
s\in\Sigma^*
s∈Σ∗ st.
f
(
r
,
s
)
=
v
f(r,s)=v
f(r,s)=v