25
授授授授 授授授 Data Structures -4 th exam-

授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

  • View
    231

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

授課教授:李錫智

Data Structures-4th exam-

Page 2: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

1. [10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm to search for the first occurrence of the pattern in the text. How many comparisons between two characters are performed before you get the answer?

Page 3: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans: 27

Text a b a c a a b a d c a b a c a b a a b b1 2 3 4 5 6

a b a c a b

7

a b a c a b

8 9

a b a c a b

10

a b a c a b

11 12

a b a c a b

13 14 15 16

a b a c a b

17

a b a c a b

18 19

a b a c a b

20

a b a c a b

21

a b a c a b

22 23 24 25 26 27

a b a c a b

Page 4: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

2. [10] Consider again the text and the pattern shown in Fig.1. You apply the Boyer-Moore algorithm to search for the first occurrence of the pattern in the text.A. [5] What is the last-occurrence function L, i.e., what are the

values of L(a), L(b), L(c), and L(d)? B. [5] How many comparisons between two characters in the

text and the pattern, respectively, are performed before you get the answer?

Page 5: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:A.

L(a)=4, L(b)=5, L(c)=3, L(d)=-1B.

Text a b a c a a b a d c a b a c a b a a b b1

a b a c a b

4 3 2

a b a c a b

5

a b a c a b

6

a b a c a b

7

a b a c a b

13 12 11 10 9 8

a b a c a b

Page 6: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

3. [5] Draw a standard trie for the following set of strings: {abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa}

Ans:

Page 7: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

4. [5] Draw a compressed trie for the following set of strings:{abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa}

Ans:

Page 8: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

5. [10] List the BFS traversal sequence, starting from node 8, for the graph in Fig.2. When several nodes are acceptable, select first the node with the smallest number.

Ans:8, 5, 6, 10, 2, 3, 7, 9, 1, 4

Page 9: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

6. [10] List the DFS traversal sequence, starting from node 8, for the graph in Fig.2. When several nodes are acceptable, select first the node with the smallest number.

Ans:8, 5, 2, 1, 3, 4, 9, 6, 7, 10

Page 10: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

7. [10] Find the minimum spanning tree for Fig.3 using the Prim-Jarnik algorithm, starting with the node J. Please draw the intermediate result each time a new node and a new edge are added.

Page 11: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(1)

(2)

(3)

(4)

Page 12: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(5)

(6)

(7)

(8)

Page 13: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

8. [10] Find the minimum spanning tree for Fig.3 using Kruskal’s algorithm. Please draw the intermediate result each time a new edge is added.

Page 14: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(1)

(2)

(3)

(4)

Page 15: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(5)

(6)

(7)

Page 16: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

9. [10] Consider again the text and the pattern shown in Fig.1. You apply the KMP algorithm to search for the first occurrence of the pattern in the text.A. [5] What is the failure function F, i.e., what are the values of

F(0), F(1), F(2), F(3), F(4), and F(5)?B. [5] How many comparisons between two characters in the

text and the pattern, respectively, are performed before you get the answer?

Page 17: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:A.

F(0)=0, F(1)=0, F(2)=1, F(3)=0, F(4)=1, F(5)=2B.

Text a b a c a a b a d c a b a c a b a a b b1 2 3 4 5 6

a b a c a b

7

a b a c a b

8 9 10 11

a b a c a b

12

a b a c a b

13

a b a c a b

14

a b a c a b

15 16 17 18 19 20

a b a c a b

Page 18: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

10. [10] Suppose you want to transmit the string “dogs do not spot hot pots or cats” with Huffman encoding. Note that there is a blank between a pair of two words, and blanks should also be transmitted.A. [5] What are the codes for the involved characters?

B. [5]What is the string of bits transmitted for this communication along the communication channel?

Page 19: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans: A.

B. 010110000010011110101101101000100011101100011000111000011100011100011000101111100100111000001000000001011

a c g h n r d p s t o _1 1 1 1 1 1 2 2 4 5 7 7

a c g h n r d p s t o _000000 000001 000010 000011 01000 01001 0101 0001 011 001 10 11

PS. _代表space

Page 20: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

11. [10] Find the shortest paths from node E to all the nodes in Fig.4. Please show the updated shortest distances each time a node is assured of its final answer.

Page 21: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(1)

(2)

(3)

(4)

Page 22: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(5)

(6)

(7)

Page 23: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

12. [10] Find the minimum spanning tree for Fig.3 using Baruvka’s algorithm as shown below. Please draw T1 each time statement 4 is encountered.

1) Algorithm BaruvkaMST(G)2) T1 V {just the vertices of G}

3) T2 V {just the vertices of G}

4) while T1 has fewer than n-1 edges do \\ n is the number of vertices in G

5) for each connected component C in T1 do

6) Let edge e be the smallest-weight edge of all the edges between

C and the other connected components in T1

7) if e is not already in T2 then

8) Add edge e to T2

9) T1 T2

10) return T1

Page 24: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

12. 1) Algorithm BaruvkaMST(G)2) T1 V {just the vertices of G}

3) T2 V {just the vertices of G}

4) while T1 has fewer than n-1 edges do \\ n is the number of vertices in G

5) for each connected component C in T1 do

6) Let edge e be the smallest-weight edge of all the edges between

C and the other connected components in T1

7) if e is not already in T2 then

8) Add edge e to T2

9) T1 T2

10) return T1

Page 25: 授課教授:李錫智 Data Structures -4 th exam-. 1.[10] Suppose we have a text and a pattern shown in Fig.1. You apply the brute-force pattern matching algorithm

Ans:

(1)

(2)

(3)