8月2日课后作业
作业一
- 写出该无向图的邻接矩阵.
答: 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⎦⎥⎥⎤
- 定义无向网络.
答:Definition 1. An undirected net is a tuple G = ( V , w ) , G=(\mathbf{V},w), G=(V,w),where V \mathbf{V} Vis the set of nodes, and w : V × V → R w:\mathbf{V} \times \mathbf{V} \to \R w:V×V→Ris 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 ⟩ \lang v_i,v_j\rang ⟨vi,vj⟩and arc ⟨ v j , v i ⟩ . \lang v_j,v_i\rang. ⟨vj,vi⟩.
作业二
- 自己画一棵树, 将其元组各部分写出来 (特别是函数
p
p
p).
答:let ϕ \phi ϕ be the empty node, the tree is a tripe T = ( V , r , p ) T = (\mathbf{V},r,p) T=(V,r,p) where
- V = { v 0 , v 1 , v 2 , v 3 , v 4 , v 5 , v 6 , v 7 } ; \mathbf{V}=\{ v_0,v_1,v_2, v_3,v_4,v_5,v_6,v_7\}; V={v0,v1,v2,v3,v4,v5,v6,v7};
- r = v 0 ; r=v_0; r=v0;
- p ( v 0 ) = ϕ , p ( v 1 ) = v 0 , p ( v 2 ) = v 0 , p ( v 3 ) = v 1 , p ( v 4 ) = v 1 , p ( v 5 ) = v 1 , p ( v 6 ) = v 2 , p ( v 7 ) = v 4 ; p(v_0)=\phi, p(v_1)=v_0, p(v_2)=v_0,p(v_3)=v_1, p(v_4)=v_1, p(v_5)=v_1,p(v_6)=v_2, p(v_7)=v_4; p(v0)=ϕ,p(v1)=v0,p(v2)=v0,p(v3)=v1,p(v4)=v1,p(v5)=v1,p(v6)=v2,p(v7)=v4;
- 针对该树, 将代码中的变量值写出来 (特别是 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 = 7;
root = 0;
parent = [-1,0,0,1,1,1,2,4]
作业三
- 画一棵三叉树, 并写出它的 child 数组.
答:child[9][3] = [ 1 − 1 2 3 4 5 − 1 − 1 6 − 1 − 1 − 1 − 1 7 − 1 − 1 − 1 − 1 8 − 1 − 1 − 1 − 1 − 1 − 1 − 1 − 1 ] \left[\begin{matrix}1&-1&2\\ 3&4&5\\ -1&-1&6\\ -1&-1&-1\\ -1&7&-1\\ -1&-1&-1\\ 8&-1&-1\\ -1&-1&-1\\ -1&-1&-1\end{matrix}\right] ⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡13−1−1−1−18−1−1−14−1−17−1−1−1−1256−1−1−1−1−1−1⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤
- 按照本贴风格, 重新定义树. 提示: 还是应该定义 parent 函数, 字母表里面只有一个元素.
答:Definition2. let ϕ \phi ϕ be the empty node, a tree is a tripe G = ( V , r , Σ , p ) G=(\mathbf{V},r,\Sigma,p) G=(V,r,Σ,p)where
- V \mathbf{V} Vis the set of nodes;
- r ∈ V r\in \mathbf{V} r∈Vis the root node;
- Σ = { a } \Sigma=\{a\} Σ={a} is the alphabet;
- p : ( V ∪ { ϕ } ) × Σ ∗ → V ∪ { ϕ } p:(\mathbf{V}\cup\{\phi\})\times \Sigma^*\to \mathbf{V}\cup\{\phi\} p:(V∪{ϕ})×Σ∗→V∪{ϕ} satisfying
- ∀ v ∈ V , ∃ 1 s ∈ Σ ∗ , s t . p ( v , s ) = r \forall v \in \mathbf{V},\exist 1 s\in\Sigma^*,st.p(v,s)=r ∀v∈V,∃1s∈Σ∗,st.p(v,s)=r
- 根据图、树、m mm-叉树的学习, 谈谈你对元组的理解.
答:元组由元组明和元组中的元素组成,图 (Graph) 是最经典的元组。G=(V,E),其中G时元组名,V,E是其中的元素。元组中的元素可以是数组,可以是矩阵,可以是任何东西,然后这些元素之间也可能存在某种联系。有点像面向对象里面的类,里面有类的变量,也有这些变量的一些方法。