老师原文链接:
11. 图与网络
11.5 作业
1、画出该图的邻接矩阵
E
=
[
0
1
1
1
1
0
1
0
1
1
0
1
1
0
1
0
]
\mathbf{E}=\left[ \begin{matrix} 0 & 1 & 1 &1 \\1 & 0 &1 & 0 \\ 1 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \end{matrix}\right]
E=⎣⎢⎢⎡0111101011011010⎦⎥⎥⎤
2、定义无向网络.
Definition 11.3 An undirected net is a tuple
G
=
(
V
,
w
)
G = (\mathbf{V}, w)
G=(V,w) where
V
\mathbf{V}
V is the set of nodes, and
w
:
V
×
V
→
R
w : \mathbf{V} \times \mathbf{V} \to \mathbb{R}
w:V×V→R is the weight function where
w
(
v
i
,
v
j
)
w(v_i, v_j)
w(vi,vj) is the weight of the arc
⟨
v
i
,
v
j
⟩
\langle v_i, v_j \rangle
⟨vi,vj⟩, and
w
(
v
i
,
v
j
)
=
w
(
v
j
,
v
i
)
w(v_i, v_j)=w(v_j, v_i)
w(vi,vj)=w(vj,vi).
老师原文链接:
12. 树
12.4 作业
1、自己画一棵树, 将其元组各部分写出来 (特别是函数
p
p
p ).
2、针对该树,将代码中的变量值写出来(特别是parent数组).
首先是树的定义:
Let
ϕ
\phi
ϕ be the empty node, a tree is a triple
T
=
(
V
,
r
,
p
)
T=(\mathbf{V}, r, p)
T=(V,r,p) where
- V ≠ ∅ \mathbf{V} \neq \empty V=∅ is the set of nodes;
- r ∈ V r \in \mathbf{V} r∈V is the root node;
-
p
:
V
→
V
∪
{
ϕ
}
p : \mathbf{V} \to \mathbf{V} \cup \{\phi\}
p:V→V∪{ϕ} is the parent mapping satisfying
- p ( r ) = ϕ p(r) = \phi p(r)=ϕ;
- ∀ v ∈ V , ∃ 1 n ≥ 0 , \forall v \in \mathbf{V}, \exist1n \ge 0, ∀v∈V,∃1n≥0, st. p ( n ) ( v ) = r p^{(n)}(v)=r p(n)(v)=r.
对于上图的树,定义中的元组各部分具体为:
V
=
{
v
0
,
v
1
,
v
2
,
v
3
,
v
4
,
v
5
,
v
6
,
v
7
,
v
8
,
v
9
}
\mathbf{V}=\{v_0, v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8, v_9\}
V={v0,v1,v2,v3,v4,v5,v6,v7,v8,v9}.
r
=
v
0
r=v_0
r=v0.
p
(
0
)
(
v
0
)
=
v
0
,
p
(
1
)
(
v
1
)
=
v
0
,
p
(
1
)
(
v
2
)
=
v
0
,
p
(
1
)
(
v
6
)
=
v
0
,
p
(
2
)
(
v
3
)
=
v
0
,
p
(
2
)
(
v
7
)
=
v
0
,
p
(
2
)
(
v
8
)
=
v
0
,
p
(
2
)
(
v
9
)
=
v
0
,
p
(
3
)
(
v
4
)
=
v
0
,
p
(
3
)
(
v
5
)
=
v
0
.
p^{(0)}(v_0)=v_0, \\p^{(1)}(v_1)=v_0, p^{(1)}(v_2)=v_0, p^{(1)}(v_6)=v_0, \\p^{(2)}(v_3)=v_0, p^{(2)}(v_7)=v_0, p^{(2)}(v_8)=v_0, p^{(2)}(v_9)=v_0, \\ p^{(3)}(v_4)=v_0, p^{(3)}(v_5)=v_0.
p(0)(v0)=v0,p(1)(v1)=v0,p(1)(v2)=v0,p(1)(v6)=v0,p(2)(v3)=v0,p(2)(v7)=v0,p(2)(v8)=v0,p(2)(v9)=v0,p(3)(v4)=v0,p(3)(v5)=v0.
针对该树,代码中各变量的值为:
n = 10,
root = 0,
parent[0]=-1, parent[1]=0, parent[2]=0, parent[3]=2, parent[4]=3, parent[5]=3, parent[6]=0, parent[7]=6, parent[8]=6, parent[9]=6,
老师原文链接:
13. m叉树
13.4 作业
- 画一棵三叉树, 并写出它的 child 数组.
- 按照本贴风格, 重新定义树. 提示: 还是应该定义 parent 函数, 字母表里面只有一个元素.
- 根据图、树、 m m m-叉树的学习, 谈谈你对元组的理解.
首先是
m
m
m-叉树的定义:
Let
ϕ
\phi
ϕ be the empty node, an
m
m
m-tree is a quadruple
M
T
=
(
V
,
r
,
Σ
,
c
)
MT = (\mathbf{V}, r, \Sigma, c)
MT=(V,r,Σ,c) where
- V \mathbf{V} V is the set of nodes;
- r ∈ V r \in \mathbf{V} r∈V is the root node;
-
c
:
(
V
∪
{
ϕ
}
)
×
Σ
∗
→
V
∪
{
ϕ
}
c : (\mathbf{V} \cup \{\phi\}) \times \Sigma^* \to \mathbf{V} \cup \{\phi\}
c:(V∪{ϕ})×Σ∗→V∪{ϕ} satisfying
- ∀ v ∈ V , ∃ 1 s ∈ Σ ∗ \forall v \in \mathbf{V}, \exist1 s \in \Sigma^* ∀v∈V,∃1s∈Σ∗ st. c ( r , s ) = v c(r, s)=v c(r,s)=v.
1) 一个三叉树如下:
因此,定义
Σ
=
{
1
,
2
,
3
}
\Sigma =\{1,2,3\}
Σ={1,2,3} 分别表示孩子结点的三个方向.
所以 child 数组为:
{{1, 2, 3}, {4, 5, -1}, {-1, 6, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}}.
child 矩阵为:
[
1
2
3
4
5
−
1
−
1
6
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
]
\left[\begin{matrix}1 & 2 & 3 \\ 4 & 5 & -1 \\ -1& 6 & -1 \\ -1& -1 & -1 \\ -1 &-1 &-1 \\ -1 & -1& -1\end{matrix}\right]
⎣⎢⎢⎢⎢⎢⎢⎡14−1−1−1−1256−1−1−13−1−1−1−1−1⎦⎥⎥⎥⎥⎥⎥⎤
- let ϕ \phi ϕ be the empty node, a tree is quadruple T = ( V , r , Σ , c ) T = (\mathbf{V}, r, \Sigma, c) T=(V,r,Σ,c) where
- V ≠ ∅ \mathbf{V} \neq \empty V=∅ is the set of nodes;
- r ∈ V r \in \mathbf{V} r∈V is the root node;
- Σ \Sigma Σ is the alphabet, and Σ = { 1 } \Sigma =\{1\} Σ={1};
-
p
:
(
V
∪
{
ϕ
}
)
×
Σ
∗
→
V
∪
{
ϕ
}
p : (\mathbf{V} \cup \{\phi\}) \times \Sigma^* \to \mathbf{V} \cup \{\phi\}
p:(V∪{ϕ})×Σ∗→V∪{ϕ} satisfying
- ∀ v ∈ V , ∃ 1 s ∈ Σ ∗ , \forall v \in \mathbf{V}, \exist1s \in \Sigma^*, ∀v∈V,∃1s∈Σ∗,st. p ( v , s ) = r p(v, s)=r p(v,s)=r.
- 元组是可以把看似没有关系或者关系跟弱的对象联系在一起,并且存到一个容器中.