11.5 作业
写出该无向图的邻接矩阵.
E
=
[
0
1
1
1
1
0
1
0
1
1
0
1
1
0
1
0
]
\mathbf{E}=\begin{bmatrix} 0&1&1&1\\ 1&0&1&0\\ 1&1&0&1\\ 1&0&1&0 \end{bmatrix}
E=⎣⎢⎢⎡0111101011011010⎦⎥⎥⎤
定义无向网络.
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 edge ( v i , v j ) ( v_i, v_j ) (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.4 作业
自己画一棵树, 将其元组各部分写出来 (特别是函数 p p p).
V
=
{
A, B, C, D, E, F, G, H, I, J, K
}
\mathbf{V}=\{\textrm{A, B, C, D, E, F, G, H, I, J, K}\}
V={A, B, C, D, E, F, G, H, I, J, K}
r
=
A
∈
V
r=\textrm{A} \in \mathbf{V}
r=A∈V
p
0
(
K
)
=
K
,
p
1
(
K
)
=
A
p^0(\textrm{K})=\textrm{K} , p^1(\textrm{K})=\textrm{A}
p0(K)=K,p1(K)=A
p
0
(
J
)
=
J
,
p
1
(
J
)
=
G
,
p
2
(
J
)
=
E
,
p
3
(
J
)
=
A
p^0(\textrm{J})=\textrm{J} ,p^1(\textrm{J})=\textrm{G} , p^2(\textrm{J})=\textrm{E} ,p^3(\textrm{J})=\textrm{A}
p0(J)=J,p1(J)=G,p2(J)=E,p3(J)=A
p
0
(
I
)
=
I
,
p
1
(
I
)
=
G
,
p
2
(
I
)
=
E
,
p
3
(
I
)
=
A
p^0(\textrm{I})=\textrm{I} , p^1(\textrm{I})=\textrm{G} , p^2(\textrm{I})=\textrm{E} ,p^3(\textrm{I})=\textrm{A}
p0(I)=I,p1(I)=G,p2(I)=E,p3(I)=A
p
0
(
H
)
=
H
,
p
1
(
H
)
=
E
,
p
2
(
H
)
=
A
p^0(\textrm{H})=\textrm{H} , p^1(\textrm{H})=\textrm{E} , p^2(\textrm{H})=\textrm{A}
p0(H)=H,p1(H)=E,p2(H)=A
p
0
(
F
)
=
F
,
p
1
(
F
)
=
E
,
p
2
(
F
)
=
A
p^0(\textrm{F})=\textrm{F} , p^1(\textrm{F})=\textrm{E} , p^2(\textrm{F})=\textrm{A}
p0(F)=F,p1(F)=E,p2(F)=A
p
0
(
D
)
=
D
,
p
1
(
D
)
=
B
,
p
2
(
D
)
=
A
p^0(\textrm{D})=\textrm{D} , p^1(\textrm{D})=\textrm{B} , p^2(\textrm{D})=\textrm{A}
p0(D)=D,p1(D)=B,p2(D)=A
p
0
(
C
)
=
C
,
p
1
(
C
)
=
B
,
p
2
(
C
)
=
A
p^0(\textrm{C})=\textrm{C} , p^1(\textrm{C})=\textrm{B} , p^2(\textrm{C})=\textrm{A}
p0(C)=C,p1(C)=B,p2(C)=A
p
0
(
A
)
=
A
p^0(\textrm{A})=\textrm{A}
p0(A)=A
针对该树, 将代码中的变量值写出来 (特别是 parent 数组).
public class Tree {
/**
* 节点数. 表示节点 v_0 至 v_{n-1}.
*/
int n;
/**
* 根节点. 0 至 n-1.
*/
int root;
/**
* 父节点.
*/
int[] parent;
/**
* 构造一棵树, 第一个节点为根节点, 其余节点均为其直接子节点, 也均为叶节点.
*/
public Tree(int paraN) {
n = paraN;
parent = new int[n];
parent[0] = -1; // -1 即 \phi
}// Of the constructor
}//Of class Tree
n=11
root=A
parent=[ -1, 0, 0, 0, 1, 1, 2, 2, 2, 7, 7 ]
13.4 作业
画一棵三叉树, 并写出它的 child 数组.
child[][]={{1,2,3},{-1,4,5},{6,7,8},{-1,-1,-1}}
按照本贴风格, 重新定义树. 提示: 还是应该定义 parent 函数, 字母表里面只有一个元素.
Let ϕ \phi ϕ be the empty node, a tree is a quadruple T = ( V , r , Σ , p ) T = (\mathbf{V}, r, \Sigma, p) T=(V,r,Σ,p) where
- V \mathbf{V} V is the set of nodes
- r ∈ V r\in V r∈V is the root node;
- Σ = { 1 } \Sigma=\{1\} Σ={1} is the alphabet;
-
p
:
V
×
Σ
∗
→
V
∪
{
ϕ
}
p:V \times \Sigma^*→V∪\{\phi\}
p:V×Σ∗→V∪{ϕ} satisfying
- ∀ v ∈ V , ∃ 1 s ∈ Σ ∗ , s . t . p { v , s } = r \forall v \in \mathbf{V},\exist1\ s\in \Sigma^*, s.t. p\{v,s\} = r ∀v∈V,∃1 s∈Σ∗,s.t.p{v,s}=r
- p { r , s } = ϕ p\{r,s\} = \phi p{r,s}=ϕ