Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1.

For the array of integers (6, 1, 5, 8, 3, 2, 7, 4), sort the elements


in increasing order (show the process step-by-step) by performing
the following methods: (a) quick-sort, (b) heap sort, © insertion
sort, (d) count sort. (20%)

(a) Quick sort

• (6, 1, 5, 8, 3, 2, 7, 4), • (6) , (1, 5, 8, 3, 2, 7, 4),


• (6, 1, 5, 4, 3, 2, 7, 8), • (1, 6) , (5, 8, 3, 2, 7, 4),
• (2, 1, 5, 4, 3), (6), (7, 8), • (1, 5, 6) (8, 3, 2, 7, 4),
• (1), (2), (5, 4, 3), (6), (7, 8), • (1, 5, 6, 8) (3, 2, 7, 4),
• (1), (2), (3, 4, 5), (6), (7, 8), • (1, 3, 5, 6, 8) (2, 7, 4),
• (1, 2, 3, 5, 6, 8) (7, 4),
• (1, 2, 3, 5, 6, 7, 8) (4),
• (1, 2, 3, 4, 5, 6, 7,8)
Heap sort skip
construct max-heap/output and restore

• Count sort
• (6, 1, 5, 8, 3, 2, 7, 4), counter (1,1,1,1,1,1,1,1)
• > (1, 2, 3, 4, 5, 6, 7, 8)
2. For the given graph: (a) Use Prim’s algorithm to
find the minimum spanning tree staring from node a
(5%). (b) Find the shortest path and distance from the
source node a to all nodes in the graph by the
Dijkstra’s algorithm. (5%) (show the process step-by-
step

(1)

(2) (3)

(5) (4)

MST
SPT
a-b 3
a-c 2
a-c-f-d 6
a-b-e or a-c-e 7
a-c-f 5

SPT
SPT
3. (a) Please describe the pseudo code of the algorithm for
finding the minimum cycle basis problem. (b) For the previous
graph, find the minimum cycle basis of the graph (solved by
ignoring the weight of the edge, i.e., the weight of each edge is 1)
(10%).
• 忽略權重 wi=1
• 1. |E|-|V|+1=12-6+1=7
• 2. {a,b,c}{a,c,f} {c,d,e} 必要
• {b,c,d} {b,c, e}{b, d,e} 3選2
• {c,e,f}{c,d,f}{d,e, f} 3選2
4. Assume the optimal Huffman coding method is used to
encoding the eight messages (A, B, C, D, E, F, G. H) with access
frequencies (9, 12, 7, 5, 4, 2, 10, 14). Draw the decode tree for
this set of code, (5%) and decode the message
“01011011100101111…” (5%) A 101
B 00
63 C 100
D 1110
26 37 E 11111
F 11110
G 110
(12)B (14)H 16 21 H 01

(7)C (9)A
(10)G 11

(5)D 6

01+01+110+1110+01+01+11111…
(2)F (4)E
H H G D H H E…
5. Show that the average case time-complexity of the binary
search for a given sorted array with size n is O(log n ). (10%)
Let
1
𝑇(𝑛) ≤ 2𝑇( 𝑛) + 𝑐𝑛
3
when n ≥ 2, and T(1) = c. Using the recursion tree method to find the closed form of
the time complexity O(T(n)).

1
𝑇(𝑛) ≤ 2𝑇( 𝑛) + 𝑐𝑛
3
= 𝑐𝑛+ 2𝑇( 𝑛)
=𝑐𝑛+ 𝑐𝑛 + 4𝑇( 𝑛)
=𝑐𝑛+ 𝑐𝑛 + 𝑐𝑛 + 8𝑇( 𝑛)
=𝑐𝑛+ 𝑐𝑛 + 𝑐𝑛 +…+( ⁄ ) +2 𝑇 1
=∑ ( ⁄ ) cn+Θ(𝑛 )
( )
= cn + Θ(𝑛 )

< 𝑐𝑛 + 𝐶(𝑛 )
/
=3cn+Θ 𝑛 = Θ(n)
7. What is the UNION-FIND operations and PATH-
COMPRESSION operations? Please explain these by
examples. (10%)
8. Use the reduction method to show that the lower bound of convex
hull problem is Ω (nlog n) for a set of n points in 2-D plane, please
describe the transformation method and reduction idea. (10%)

轉換成sorted結果
9. For the knapsack problem, assume n=3, M=16, P=
(P1, P2, P3) = (24, 24, 20) and W= (W1, W2, W3) = (8, 4,
5). Let Xi be the selected value of the i-th item, find
the optimal solution X=(X1, X2, X3) of the instance with
maximal profit. (5%)

每單位獲利計算
24/8=3
24/4=6
20/5=4
X2=1, X3=1, X1=7/8
總獲利 24*7/8+24*1+20*1=21+24+20=65
10. For the given recursive formula, compute A(2, 2).
(5%)

• A(2,2)
• =A(1, A(2,1))
• =A(1, 2)
• =A(0, A(1, 1))
• =A(0, 2)
•4

You might also like