授課教授:李錫智 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 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?
Ans: 27 Textabacaabadcabacabaabb abacab 7 abacab 89 abacab 10 abacab 1112 abacab abacab 17 abacab 1819 abacab 20 abacab 21 abacab abacab
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?
Ans: A. L(a)=4, L(b)=5, L(c)=3, L(d)=-1 B. Textabacaabadcabacabaabb 1 abacab 432 abacab 5 abacab 6 abacab 7 abacab abacab
3.[5] Draw a standard trie for the following set of strings: {abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa} Ans:
4.[5] Draw a compressed trie for the following set of strings: {abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa} Ans:
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
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
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.
Ans: (1)(2) (3) (4)
Ans: (5) (6) (7) (8)
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.
Ans: (1)(2) (3)(4)
Ans: (5) (6) (7)
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?
Ans: A. F(0)=0, F(1)=0, F(2)=1, F(3)=0, F(4)=1, F(5)=2 B. Textabacaabadcabacabaabb abacab 7 abacab abacab 12 abacab 13 abacab 14 abacab abacab
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?
Ans: A. B acghnrdpsto_ acghnrdpsto_ PS. _ 代表 space
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.
Ans: (1)(2) (3)(4)
Ans: (5)(6) (7)
12.[10] Find the minimum spanning tree for Fig.3 using Baruvka’s algorithm as shown below. Please draw T 1 each time statement 4 is encountered. 1) Algorithm BaruvkaMST(G) 2) T 1 V {just the vertices of G} 3) T 2 V {just the vertices of G} 4) while T 1 has fewer than n-1 edges do \\ n is the number of vertices in G 5) for each connected component C in T 1 do 6) Let edge e be the smallest-weight edge of all the edges between C and the other connected components in T 1 7) if e is not already in T 2 then 8) Add edge e to T 2 9) T 1 T 2 10) return T 1
12. 1) Algorithm BaruvkaMST(G) 2) T 1 V {just the vertices of G} 3) T 2 V {just the vertices of G} 4) while T 1 has fewer than n-1 edges do \\ n is the number of vertices in G 5) for each connected component C in T 1 do 6) Let edge e be the smallest-weight edge of all the edges between C and the other connected components in T 1 7) if e is not already in T 2 then 8) Add edge e to T 2 9) T 1 T 2 10) return T 1
Ans: (1) (2) (3)