写出该无向图的邻接矩阵
答:由于是无向图,即相邻两边都有路径到达对方。该邻接矩阵为:
[
0
1
1
1
1
0
1
0
1
1
0
1
1
0
1
0
]
\left[ \begin{matrix} 0&1&1&1\\ 1&0&1&0\\ 1&1&0&1\\ 1&0&1&0 \end{matrix} \right]
⎣⎢⎢⎡0111101011011010⎦⎥⎥⎤
定义无向网络
答:有向网络为:A directed net is a tuple G = ( V , w ) , \mathbf{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 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⟩
则无向图为:A indirected net is a tuple
G
=
(
V
,
w
)
,
\mathbf{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 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
)
(v_i,v_j)
(vi,vj)
自己画一棵树, 将其元组各部分写出来 (特别是函数 p p p).
答:该树如下:
定义如下: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 ≠ ∅ \mathrm{V} \ne \emptyset V=∅ is the set of nodes;
- r ∈ V r \in \mathrm{V} r∈Vis the root node;
- p : V → V ∪ { ∅ } p:\mathrm{V} \to\mathrm{V} \ \cup\{\emptyset\} p:V→V ∪{∅}is the parent mapping satisfying
- p ( r ) = ϕ p(r)=\phi p(r)=ϕ
- ∀ v ∈ V , ∃ 1 n ≥ 0 , s t . p ( n ) ( v ) = r \forall v \in \mathbf{V}, \exist 1 n \ge 0, st. p^{(n)}(v) = r ∀v∈V,∃1n≥0,st.p(n)(v)=r
其中
V
=
{
A
,
B
,
C
,
D
,
E
,
F
}
\mathbf{V}=\{A,B,C,D,E,F\}
V={A,B,C,D,E,F}
r
i
s
t
h
e
r
o
o
t
,
r
i
s
A
r \mathrm{\ is \ the \ root} \ ,\ r \ \mathrm{is}\ A
r is the root , r is A
针对该树, 将代码中的变量值写出来 (特别是 parent 数组).
答:Java代码如下:
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
=
6
,
r
o
o
t
=
A
,
p
n=6, root =A,p
n=6,root=A,p数组:
p
[
−
1
,
0
,
0
,
0
,
1
,
1
]
p[-1,0,0,0,1,1]
p[−1,0,0,0,1,1] (nodes的按从左到右编号)
画一棵三叉树, 并写出它的 child 数组.
答:该树如下:
该三叉树的child数组为:用矩阵表示为
[
1
2
3
4
5
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
]
\left[ \begin{matrix} 1 & 2 &3 \\ 4 & 5 & -1 \\ -1 & -1 & -1\\ -1 & -1 & -1\\ -1 & -1 & -1\\ -1 & -1 & -1 \end{matrix} \right]
⎣⎢⎢⎢⎢⎢⎢⎡14−1−1−1−125−1−1−1−13−1−1−1−1−1⎦⎥⎥⎥⎥⎥⎥⎤
按照本贴风格, 重新定义树. 提示: 还是应该定义 parent 函数, 字母表里面只有一个元素.
答:
A
t
r
e
e
i
s
a
5
−
t
u
p
l
e
M
=
(
Σ
,
V
,
r
,
T
,
p
)
,
w
h
e
r
e
a
)
Σ
=
{
c
}
i
s
t
h
e
a
l
p
h
a
b
e
t
,
r
e
p
r
e
s
e
n
t
a
c
h
i
l
d
n
o
d
e
b
)
V
=
{
v
1
,
.
.
.
.
.
,
v
n
}
i
s
t
h
e
s
e
t
o
f
n
o
d
e
s
c
)
r
∈
V
i
s
t
h
e
r
o
o
t
o
f
t
h
e
t
r
e
e
,
a
l
s
o
i
s
t
h
e
s
t
a
r
t
n
o
d
e
d
)
ϕ
i
s
t
h
e
s
e
t
o
f
t
e
r
m
i
n
a
l
s
t
a
t
e
e
)
p
:
V
∪
{
ϕ
}
×
Σ
∗
→
V
∪
{
ϕ
}
i
s
t
h
e
t
r
a
n
s
i
t
i
o
n
f
u
n
t
i
o
n
A
n
y
s
∈
Σ
∗
i
s
a
c
c
e
p
t
e
d
b
y
t
h
e
a
u
t
o
m
a
t
a
i
f
f
∀
v
∈
V
,
∃
1
s
∈
Σ
∗
s
t
.
c
(
r
,
s
)
=
v
\mathrm{A\ tree \ is\ a\ 5-tuple\ M}=(\Sigma,\mathbf{V},\mathbf{r},\mathbf{T},p),\mathrm{where} \\a)\ \Sigma=\{c\}\mathrm{\ is\ the\ alphabet,\ represent\ a\ child\ node} \\b)\ \mathbf{V}=\{v_1,.....,v_n\}\ \mathrm{is\ the\ set\ of\ nodes} \\c)\ \mathbf{r}\in\ \mathbf{V}\ \mathrm{is\ the\ root\ of\ the\ tree,\ also\ is\ the\ start\ node} \\d)\ \phi\ \mathrm{is\ the\ set\ of\ terminal\ state} \\e)\ p:\mathbf{V}\cup \{\phi\}\times\Sigma^* \to \mathbf{V}\cup\{\phi\}\ \mathrm{is\ the\ transition\ funtion} \\Any\ s\in\Sigma^*\ \mathrm{is\ accepted\ by\ the\ automata\ iff\ \forall}v\in\ \mathbf{V},\exists1s\in\Sigma^*st.c(\mathbf{r},s)=v
A tree is a 5−tuple M=(Σ,V,r,T,p),wherea) Σ={c} is the alphabet, represent a child nodeb) V={v1,.....,vn} is the set of nodesc) r∈ V is the root of the tree, also is the start noded) ϕ is the set of terminal statee) p:V∪{ϕ}×Σ∗→V∪{ϕ} is the transition funtionAny s∈Σ∗ is accepted by the automata iff ∀v∈ V,∃1s∈Σ∗st.c(r,s)=v
根据图、树、 m m m-叉树的学习, 谈谈你对元组的理解.
答:今天又学习了一遍元组,最深刻的印象就是元组可以存储不同类型的数据以及用不同的数据类型来描述复杂的对象。同时也回想起Python元组的一些性质如:元组属于不可变序列,不能修改元组中的元素,因此,元组没有增加元素,修改元素,删除元素相关的方法等性质。