csi35chapter 11 review - natna.info · 11 csi35chapter 11 review 5. a) how many edges does a tree...
TRANSCRIPT
1
CSI35 Chapter 11 Review
1. Which of the graphs are trees?
a b
c d
e f
a b
c
d
e f
a bc
d
e f
x y
q r
z p
g
2
CSI35 Chapter 11 Review
1. Which of the graphs are trees?
a b
c d
e f
a b
c
d
e f
a bc
d
e f
x y
q r
z p
g
3
CSI35 Chapter 11 Review
2. Answer the questions about the following treea
b c d e
f
1) Which vertex is the root of the tree?2) what is the height of the tree?3) Which vertices are children of h?4) Which vertices are siblings of n?
5) What vertices are internal?6) Which vertices are leaves?7) Which vertex is parent of u ?8) Which vertices are ancestors of t ?9) Which vertices are descendants of f ?10) what vertices are at level 3?11) Is the tree balanced?
g h i j k m
n o p q r st u v
w x yz
4
CSI35 Chapter 11 Review
2. Answer the questions about the following treea
b c d e
f
1) Which vertex is the root of the tree? a2) what is the height of the tree? 43) Which vertices are children of h? none4) Which vertices are siblings of n? 0 and p
5) What vertices are internal? a, b, c, d, e, f, I, j, m, n, q.6) Which vertices are leaves? g, h, k, o, p, r, s, t, u, v, w, x, y, z, , 7) Which vertex is parent of u ? m8) Which vertices are ancestors of t ? m, e, a9) Which vertices are descendants of f ? n, o, p, w, x10) what vertices are at level 3? n, o, p, q, r, s, t, u, v11) Is the tree balanced? No, because we have leaves at levels 2,3,and 4
g h i j k m
n o p q r st u v
w x yz
5
CSI35 Chapter 11 Review
3. Determine which trees are full, complete, or neither.
a
b c
d e f
h
g
T1 T
2
a
b c
d e
f g
T3
T4
T5
6
CSI35 Chapter 11 Review
3. Determine which trees are full, complete, or neither.
a
b c
d e f
h
g
T1 T
2
a
b c
d e
f g
T3
T4
T5
neitherneither
full
complete complete
7
CSI35 Chapter 11 Review
4. a) How many nonisomorphic rooted trees are there with five vertices?
b) How many nonisomorphic unrooted trees are there with five vertices?
8
CSI35 Chapter 11 Review
4. a) How many nonisomorphic unrooted trees are there with five vertices?
2 vertices of degree 1, and 3 vertices of degree 2
3 vertices of degree 1, 1 vertex of degree 2, and 1 vertex of degree 3
4 vertices of degree 1, and 1 vertex of degree 4
Answer: 3 trees
9
CSI35 Chapter 11 Review
4. a) How many nonisomorphic unrooted trees are there with five vertices?
2 vertices of degree 1, and 3 vertices of degree 2
3 vertices of degree 1, 1 vertex of degree 2, and 1 vertex of degree 3
4 vertices of degree 1, and 1 vertex of degree 4
b) How many nonisomorphic rooted trees are there with five vertices?
Answer: 9 trees
10
CSI35 Chapter 11 Review
5. a) How many edges does a tree with 80 vertices have?
b) How many internal vertices does a full binary 3-ary tree with 113 leaves have?
c) How many vertices does a complete 4-ary tree of height 6 have?
11
CSI35 Chapter 11 Review
5. a) How many edges does a tree with 80 vertices have?
A tree with n vertices has n-1 edges, hence the given tree has 79 edges. Answer: 79 edges
b) How many internal vertices does a full binary 3-ary tree with 113 leaves have?
m = 3, l = 113, hence
Answer: 56 internal verticesc) How many vertices does a complete 4-ary tree of height
6 have?
i=l−1m−1
=113−13−1
=1122
=56
...
level 0: 1 vertex 40
level 1: 4 vertices 1*4 = 41
level 2: 4 vertices 1*41 = 42 … 46
Answer: 1 + 4 + 42 + 43 + 44+ 45 + 46 vertices.
12
CSI35 Chapter 11 Review
6. Given a binary tree (see below), insert the following numbers, one by one: 10, 14, 66, 79, and 37.
45
26 76
65 89
53 70
13
CSI35 Chapter 11 Review
6. Given a binary tree (see below), insert the following numbers, one by one: 10, 14, 66, 79, and 37.
45
26 76
65 8910
7053
37
14
66
79
14
CSI35 Chapter 11 Review
7. How many weighings of a balance scale are needed to find a counterfeit coin among four coins if the counterfeit coin can be either heavier or lighter than the others?
15
CSI35 Chapter 11 Review
7. How many weighings of a balance scale are needed to find a counterfeit coin among four coins if the counterfeit coin can be either heavier or lighter than the others? Describe an algorithm.Answer: 3 weighings. The algorithm:
x1,x
2 : x
3,x
4
x1: x
2
x1,x
2 lighter
x3: x
4
x3,x
4 lighter
x1 x
3x
2x
4
x1lighter x
2 lighter x
3 l x
4 lighter
same
x1: x
3
same
x3
x4
x1: x
3
same
same
x1
x2
not balanced
not balanced
16
CSI35 Chapter 11 Review
8. Construct the binary tree with prefix codes representing these coding schemes:a: 1 b: 01 c: 001 d: 0001 e: 00001
17
CSI35 Chapter 11 Review
8. Construct the binary tree with prefix codes representing these coding schemes:a: 1 b: 01 c: 001 d: 0001 e: 00001
d
a
b
c
0
0
0
1
1
1
1
A variation of a question: determine which of the given codes are prefix codes.
1e
0
18
CSI35 Chapter 11 Review
9. Use Huffman coding to encode the symbols with the given frequencies.a: 0.4 b: 0.2 c: 0.1 d: 0.15 e: 0.13 f: 0.02
19
CSI35 Chapter 11 Review
9. Use Huffman coding to encode the symbols with the given frequencies.a: 0.4 b: 0.2 c: 0.1 d: 0.15 e: 0.13 f: 0.02
0.02
f c
0.1
e
0.13
d
0.15
b
0.2
a
0.4 Initial step
0.12
fc
0 11st iteration
2nd iteration
e
0.13
d
0.15
b
0.2
a
0.4
0.25
fc
e0 1
0 1
d
0.15
b
0.2
a
0.4
20
CSI35 Chapter 11 Review
9. Use Huffman coding to encode the symbols with the given frequencies.a: 0.4 b: 0.2 c: 0.1 d: 0.15 e: 0.13 f: 0.02
4th iteration
3rd iteration
a
0.40.25
fc
e0 1
0 10.35
db
0 1
db
0 1
fc
e0 1
0 1
100.6
a
0.4
21
CSI35 Chapter 11 Review
9. Use Huffman coding to encode the symbols with the given frequencies.a: 0.4 b: 0.2 c: 0.1 d: 0.15 e: 0.13 f: 0.02
5th iteration
101
db
0 1
fc
e0 1
0 1
0 1a
prefix code:a: 1b: 000c: 0110d: 001e: 010f: 0111
The average number of bits required to encode a symbol is:0.4*1 + 0.2*3 + 0.1*4 + 0.15*3 + 0.13*3 + 0.02*4 = 2.32
22
CSI35 Chapter 11 Review
10. List the vertices of the tree visited during aa) preorder traversal b) inorder traversalc) postorder traversalof the tree.
a
b c
e g h
mk
d
n
jif
23
CSI35 Chapter 11 Review
10. List the vertices of the tree visited during aa) preorder traversal b) inorder traversalc) postorder traversalof the tree.
preorder traversal: a, b, e, k, m, f, c, g, h, d, i, n, jinorder traversal: k, e, m, b, f, a, g, c, h, n, i, d, jpostorder traversal: k, m, e, f, b, g, h, c, n, i, j, d, a
a
b c
e g h
mk
d
n
jif
24
CSI35 Chapter 11 Review
10. Represent (A B) – (A (B – A)) using an ordered rooted tree.Then write the expression in prefix, postfix and infix notations.
25
CSI35 Chapter 11 Review
10. Represent (A B) – (A (B – A)) using an ordered rooted tree.Then write the expression in prefix, postfix and infix notations.
–
A B A –
B A
prefix : – A B A – B Apostfix: A B A B A – –infix: (A B) – (A (B – A))
26
CSI35 Chapter 11 Review
11. Evaluate the prefix expression + - ^ 3 2 ^ 2 3 / 6 – 4 2 .
27
CSI35 Chapter 11 Review
11. Evaluate the prefix expression + - ^ 3 2 ^ 2 3 / 6 – 4 2 .
+ - ^ 3 2 ^ 2 3 / 6 – 4 2 4-2= 2+ - ^ 3 2 ^ 2 3 / 6 2 6/2 = 3+ - ^ 3 2 ^ 2 3 3
23 = 8+ - ^ 3 2 8 3 32 = 9+ - 9 8 3 9 – 8 = 1+ 1 31 + 3 = 4
+ - ^ 3 2 ^ 2 3 / 6 – 4 2 = 4
28
CSI35 Chapter 11 Review
12. Use depth-first-search (DFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
29
CSI35 Chapter 11 Review
12. Use depth-first-search (DFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
30
CSI35 Chapter 11 Review
12. Use depth-first-search (DFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
31
CSI35 Chapter 11 Review
13. Use breadth-first-search (BFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
32
CSI35 Chapter 11 Review
13. Use breadth-first-search (BFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
L = {a, b, c, d, e, f, i, g, h, j, k, q, m, n, o, p}
33
CSI35 Chapter 11 Review
13. Use breadth-first-search (BFS) algorithm to produce a spanning tree for the given simple graph. Use vertex a as the root of the spanning tree.
bc g
a
jd
i
e
h
f
ko
m
n
p
q
L = {a, b, c, d, e, f, i, g, h, j, k, q, m, n, o, p}
34
CSI35 Chapter 11 Review
14. Use Prim's or Kruskal's algorithms to find a minimum spanning tree for the given weighted graph.
b c
g
a
j
d
i
e hf
k
o
m
n p q3
3
3 3
2 2
2
2
2
2
2 2
4
1 2 1
2
2
1
32
1
3
3 3
3 4
4 3
35
CSI35 Chapter 11 Review
14. Use Prim's or Kruskal's algorithms to find a minimum spanning tree for the given weighted graph.
b c
g
a
j
d
i
e hf
k
o
m
n p q3
3
3 3
2 2
2
2
2
2
2 2
4
1 2 1
2
2
1
32
1
3
3 3
3 4
4 3
36
CSI35 Chapter 11 Review
14. Use Prim's or Kruskal's algorithms to find a minimum spanning tree for the given weighted graph.
b c
g
a
j
d
i
e hf
k
o
m
n p q
3 3
2 2
2
2 2
2 2
1 1
2
2
1
2
1