Day 4
作业11
问题1:写出该无向图的邻接矩阵.
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⎦⎥⎥⎤
问题2:定义无向网络
Definition 1. A undirected net is a tuple G = ( V , w ) G = (\mathbf V, w) G=(V,w), where
- V ≠ ∅ \mathbf V \ne \emptyset V=∅ is the set of nodes,
-
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⟩ satisfying
- ∀ ( v i , v j ) ∈ V × V \forall (v_i,v_j)\in \mathbf V \times \mathbf V ∀(vi,vj)∈V×V, 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
问题1:自己画一棵树, 将其元组各部分写出来 (特别是函数
p
p
p).
Let
ϕ
\phi
ϕ be the empty node, the tree is a triple
G
=
(
V
,
r
,
p
)
G = (\mathbf V, r, p)
G=(V,r,p) where
- V = [ v 0 . . v 9 ] \mathbf V = [v_0..v_9] V=[v0..v9] is the set of nodes;
- r = v 0 r = v_0 r=v0 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 , ∃ ! n ≥ 0 , s.t. p ( n ) ( v ) = r \forall v \in \mathbf V, \exist ! n \ge 0, \textrm {s.t.} p^{(n)}(v) = r ∀v∈V,∃!n≥0,s.t.p(n)(v)=r.
问题2:针对该树, 将代码中的变量值写出来 (特别是 parent 数组).
以 -1 为空结点,有如下代码:
class myTree{
private:
//结点数量
int numNodes;
//根结点
int root;
//父结点数组
int *parent;
public:
//该树的构造函数
myTree(){
numNodes = 10;
root = 0;
parent = new int[numNodes];
if (parent == NULL){
cout << "new mem fail" << endl;
return;
}
parent[0] = -1;
parent[1] = 0;
parent[2] = 1;
parent[3] = 1;
parent[4] = 3;
parent[5] = 3;
parent[6] = 3;
parent[7] = 0;
parent[8] = 7;
parent[9] = 7;
}
~myTree(){
delete[] parent;
}
//找父结点
int findParent(int node, int n){
int resParent = node;
for (int i = 0; i < n; i++)
{
resParent = parent[resParent];
if (resParent == -1)
{
break;
}
}
return resParent;
}
};
作业13
问题1:画一棵三叉树, 并写出它的 child 数组.
再用用作业12的图,最大的度为3,也是三叉树。
child [10][3] :
[
1
−
1
7
2
−
1
3
−
1
−
1
−
1
4
5
6
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
−
1
8
−
1
9
−
1
−
1
−
1
−
1
−
1
−
1
]
\begin{bmatrix} 1 &-1 &7\\ 2 &-1 &3\\ -1 &-1 &-1\\ 4 &5 &6\\ -1 &-1 &-1\\ -1 &-1 &-1\\ -1 &-1 &-1\\ 8 &-1 &9\\ -1 &-1 &-1\\ -1 &-1 &-1 \end{bmatrix}
⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡12−14−1−1−18−1−1−1−1−15−1−1−1−1−1−173−16−1−1−19−1−1⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤
问题2:按照本贴风格, 重新定义树. 提示: 还是应该定义 parent 函数, 字母表里面只有一个元素.
Definition 2. Let ϕ \phi ϕ be the empty node, a tree is a quadruple G = ( V , r , p , Σ ) G = (\mathbf V, r, p, \Sigma) G=(V,r,p,Σ) where
- Σ = { u } \Sigma=\{\mathrm u\} Σ={u} is the alphabat;
- V ≠ ∅ \mathbf V \ne \emptyset V=∅ is the set of nodes;
- r ∈ V r\in \mathbf V r∈V is the root node;
-
p
:
(
V
⋃
{
ϕ
}
)
×
Σ
∗
→
V
⋃
{
ϕ
}
p : (\mathbf V\bigcup\{\phi\}) \times \Sigma^* \to \mathbf V \bigcup \{\phi\}
p:(V⋃{ϕ})×Σ∗→V⋃{ϕ} is the parent mapping satisfying
- ∀ v ∈ V , ∃ ! s ∈ Σ ∗ , s.t. p ( v , s ) = r \forall v \in \mathbf V, \exist !s \in \Sigma^* , \textrm {s.t.} p(v, s) = r ∀v∈V,∃!s∈Σ∗,s.t.p(v,s)=r
问题3: 根据图、树、 m m m-叉树的学习, 谈谈你对元组的理解.
元组由元组名和其中的元素组成,例如
G
=
(
V
,
r
,
p
)
G = (\mathbf V, r, p)
G=(V,r,p),其中
G
G
G 为元组名,
V
,
r
,
p
\mathbf V, r, p
V,r,p 为该元组的元素。元素可以是任何东西,如矩阵、函数……
这样一来,就可以理解为面向对象编程语言当中的类。类里面可以有成员变量、可以有函数。
Day5
作业14
问题:定义一个标签分布系统, 即各标签的值不是 0/1, 而是 [ 0 , 1 ] [0, 1] [0,1] 区间的实数, 且同一对象的标签和为 1.
Definition 3. A Label Distribution system is a tuple S = ( X , Y ) S = (\mathbf X, \mathbf Y) S=(X,Y) , where
- X = [ x i j ] n × m ∈ R n × m \mathbf X = [x_{ij}]_{n \times m} \in \mathbb R^{n \times m} X=[xij]n×m∈Rn×m is the data matrix, and x i = [ x i 1 , … , x i m ] \mathbf{x}_i = [x_{i1}, \dots, x_{im}] xi=[xi1,…,xim] is an instance;
-
Y
=
[
y
i
k
]
n
×
l
∈
[
0
,
1
]
n
×
l
\mathbf Y = [y_{ik}]_{n \times l} \in [0, 1]^{n \times l}
Y=[yik]n×l∈[0,1]n×l is the lable matrix, and
y
i
=
[
y
i
1
,
…
,
y
i
l
]
\mathbf{y}_i = [y_{i1}, \dots, y_{il}]
yi=[yi1,…,yil]is the label vector of
x
i
\mathbf{x}_i
xi satisfying
- ∀ y i ⊂ Y , ∑ t = 1 l y i t = 1 \forall \mathbf y_i \subset \mathbf Y, \sum_{t = 1}^{l}y_{it} = 1 ∀yi⊂Y,∑t=1lyit=1.
- n n n is the number of instances;
- m m m is the number of features;
- l l l is the number of distribution labels.