E N D
Data Structures-4th exam- 授課教授:李錫智
[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?
[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. • [5] What is the last-occurrence function L, i.e., what are the values of L(a), L(b), L(c), and L(d)? • [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.
[5] Draw a standard trie for the following set of strings: {abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa} Ans:
[5] Draw a compressed trie for the following set of strings: {abab, baba, aaccc, bcbc, ccaaa, ccab, bbaacc, cbcc, cbca, abaaa} Ans:
[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
[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
[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.
(1) (2) Ans: (3) (4)
(5) (6) Ans: (7) (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.
(1) (2) Ans: (3) (4)
(6) (5) Ans: (7)
[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. • [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)? • [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.
[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. • [5] What are the codes for the involved characters? • [5]What is the string of bits transmitted for this communication along the communication channel?
Ans: A. B. 010110000010011110101101101000100011101100011000111000011100011100011000101111100100111000001000000001011 PS. _代表space
[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.
(1) (2) Ans: (3) (4)
(5) (6) Ans: (7)
[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. • AlgorithmBaruvkaMST(G) • T1V{just the vertices of G} • T2V{just the vertices of G} • whileT1 has fewer than n-1 edges do \\ n is the number of vertices in G • for each connected component C in T1do • Let edge e be the smallest-weight edge of all the edges between C and the other connected components in T1 • ife is not already in T2then • Add edge e to T2 • T1T2 • return T1
AlgorithmBaruvkaMST(G) • T1V{just the vertices of G} • T2V{just the vertices of G} • whileT1 has fewer than n-1 edges do \\ n is the number of vertices in G • for each connected component C in T1do • Let edge e be the smallest-weight edge of all the edges between C and the other connected components in T1 • ife is not already in T2then • Add edge e to T2 • T1T2 • return T1
(2) (1) Ans: (3)