Dept. of Computer Science and Information Engineering, National Dong Hwa University Data Structures – Final Exam, Jan. 10, 2011 1. (10%) Find the shortest path from A to H in the following directed network (its edges with weights are shown below). AB 14 ; AC 5 ; AE 7 ; BD 4 ; CB 13 ; CF 16 ; DF 3 ; DH 18 ; EB 6 ; FH 11 Ans:

A -> H Shortest path : A->E->B->D->F->H ,path length : 31 2. (10%) Construct Huffman binary code for eight characters whose probabilities of appearance are A: 0.15, B: 0.19, C:0.23, D:0.04, E:0.11, F:0.08, G:0.14 and H: 0.06. What is the average number of bits per character?

Ans : A B C D E F G H Weight = {0.15, 0.19, 0.23, 0.04, 0.11, 0.08, 0.14, 0.06}

average number of bits per character = (0.19 + 0.23)*2 + (0.11+0.14+0.15)*3 + 0.08*4 + (0.04+0.06)*5 = 0.84 + 1.2 + 0.32 + 0.5 = 2.86 3. (25%) Fill the following table of sorting n elements with various algorithms. Determine the time and space complexity with Big-Oh and whether it is stable or unstable. Time Best case

Worst case

Space Average case

Stable/ Unstable

Insertion Sort Selection Sort Quick Sort Merge Sort Heap sort Ans: Time

Space

Stable/ Unstable

Best case

Worst case

Average case

Insertion Sort

O(n)

O( )

O(

)

O(1)

Stable

Selection Sort

O( )

O( )

O(

)

O(1)

Unstable

Quick Sort

O(nlogn)

O( )

O(nlogn)

O(n)~O(nlogn)

Unstable

Merge Sort

O(nlogn)

O(nlogn)

O(nlogn)

O(n)

Stable

Heap sort

O(nlogn)

O(nlogn)

O(nlogn)

O(1)

Unstable

4. (10%) Given the sequence of numbers: 8, 1, 7, 2, 6, 3, 5, 4 we intend to sort them into ascending order. a. How many swaps does it take to sort this sequence via Insertion sort? b. How many recursive calls does it take by the Merge sort to get the job done? Ans : a. Step 1: 8, 1, 7, 2, 6, 3, 5, 4 Step 2: 1, 8, 7, 2, 6, 3, 5, 4 Step 3: 1, 7, 8, 2, 6, 3, 5, 4 Step 4: 1, 2, 7, 8, 6, 3, 5, 4 Step 5: 1, 2, 6, 7, 8, 3, 5, 4 Step 6: 1, 2, 3, 6, 7, 8, 5, 4 Step 7: 1, 2, 3, 5, 6, 7, 8, 4 Step 8: 1, 2, 3, 4, 5, 6, 7, 8 總共要 swap 16 次 b.

-> 1 次 ->1 次 ->2 次 ->2 次 ->3 次 ->3 次 ->4 次

[8, 1, 7, 2, 6, 3, 5, 4] 1次 [8, 1, 7, 2]

[6, 3, 5, 4] 2次

[8, 1]

[7, 2]

[6, 3]

[5, 4] 4次

[8]

[1]

[7] [2] [6] [3]

[5]

[4]

Ans:

6. (10%) An unsorted sequence 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 is stored in an array and to be sorted in increasing order. Write the contents of the array in the first two passes of heap sort. Ans : Input: 26

5

77

1

61

11

59

15

48

19

Initial: 77

61

59

48

59

11

26

15

1

5

59

15

19

11

26

5

1

77

26

15

19

11

1

5

61

77

Pass 1 : 61

48

Pass 2 : 59

48

7. (15%) Assume that the data and key are both integers. The hash table size(HSize) is 10. The index runs from 0 to 9. Please fill in the content of the hash table if numbers 6, 3, 35, 15, 27, 95, and 23 are swquentially inserted into the hash table using the following scheme. If a number can be inserted to hash table, please indicate it. a. Hash Function: key mod HSize, open addressing with linear probing. 0 1 2 3 4 5 6 7 8

9

b. HF: key mod HSize, open addressing with quadratic probing. (use HF’ if HF is overflow). HF’: (key 0

) mod HSize , i = 1, 2, 3, … 1

2

3

4

5

6

7

8

9

c. Some numbers may not be able to insert to the table even though there are still spaces in the hash table for quadratic probing or double hashing. What can we do to reduce the chance of this mishap to happen when we design the hash table. Ans: a. 0

1

2

3

4

5

6

3

23

35

6

2

3

4

5

23

3

15

35

7

8

9

15

27

95

7

8

9

b. 0

1

c. 使用 chaining 方式解決

6 6

27

95

8. (10%) Compute the lengths of all-pair shortest paths for the directed graph of 5 vertices represented by the following matrix such that each entry =t represents that there is an edge directed from vertex i to vertex j with weight t.

Ans:

=

=

=

=

=

=

13514268541041808003