NTU-Exam 板


LINE

課程名稱︰資料結構與演算法 課程性質︰系必修 課程教師︰蔡欣穆 開課學院:電資學院 開課系所︰資工系 考試日期(年月日)︰2012/06/19 考試時限(分鐘):180 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : Data Structure and Algorithm, Spring 2012 Final Examination 124 points Problem 1. In each of the following question, please specify if the statement is true or false. If the statement is true explain why it is true. If the statement is true, explain why it is true. If it is false, explain what the correct answer is and why. (24 points, 1 point for true/false and 3 points for the explanation for each question) 1. In a strongly-connected component of a graph, there is at least one articulation point. 2. After performing a depth-first search(DFS) in an undirected graph, there is no cross edge in the generated depth-first forest. 3.If the costs of all edges in a graph are distinct and the graph is connected, then the minimum spanning tree of this graph is unique. 4. When sorting on multiple keys, least-significant-key-first sorting is easier to implement then most-significant-ket-first sorting. 5. When the load factor is high(close to 1), the performance of a hash table is much worse when using chaining than when using open addressing. 6. When using the directory-less dynamic hash table as shown in the lecture, the overflow problem caused by an insertion is always resolved after expanding the hash table. Problem 2. "Short answer" questions: (32 points) 1. Describe the worst-case for the heap sort algorithm. When dose it happen and what is the worst case running time? (4 points) 2. Please explain why when a new element is added to a red-black tree it is initially set to have red color. (4 points) 3. Please calculate the value of the prefix function pi[i], 1<=i<=18 for the pattern string "abacabadabacabacaa" and fill out Table 3. Please use the definition of the prefix function from the lecture(not the homework). In other words, the minimal output value is 0, not -1. (6 points. Each incorrecet entry deduct 2 points. More the 2 incorrect entries result in 0 point) 4. Continuing with the previous question. Please specify which characters(and their indices) in the pattern string did you compare to when you calculate the prefix function for the following input: "d" at index 8 and "c" at index 16 using the KMP algorithm. (4 points) 5. What is a stable sorting algorithm? What is an adaptive sorting algorithm? Please explain (4 points) 6. Describe the reason that in the implementation of quick sort sometimes we prefer to choose the pivot randomly instead of always choosing the left most or the right most elements in the array as the pivot (4 points) 7. Fill out the blank in following C code segment so that the function performs counting sort: (6 points) void CountingSort(int A[], int n, int B[], int k) { //A contains the input array with length n //B contains the sorted array (to be output)_ int C[K], i, j; for (i=0;i<K;++i) C[i]=0; for (j=0;j<n;++j) C[A[j]]=C[A[j]]+1; for (i=1;i<K;++i) C[i]=C[i]+C[j-1]; /* Fill out the blank here!! */ } Problem 3. Please answer the following questions about graph. (12 points) 1. Please use the Bellman-Ford algorithm to determine the costs of the shortest paths (the number next to the edges in the graph are the costs for traveling through them) from vertex 1 to all other vertices in the graph in Figure 1. Use Table 1 to show how the algorithm is executed in each iteration. (6 points) 2. Please use the Dijkstra's algorithm to determine the costs of the shortest paths (the number next to the edges in the graph are the costs for traveling through them) from vertex 1 to all other vertices in the graph in Figure 1. Use Table 2 to show how the algorithm is executed in each iteration. (6 points) Problem 4. A depth-first forest classifies the edges of a graph into tree, back forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source of the search into the same four categories. Prove that in a breadth-first search(BFS) of an undirected graph, the following properties hold. (recall that v.d of the vertex v stores the distance from the source s to vertex v calculated by the BFS algorithm) (12 points) 1. There are no back edges and no forward edges. (6 points) 2. For each cross edge (u,v), we have v.d=u.d or v.d=u.d+1 (or v.d=u.d-1 since u and v can be exchanged). (6 points) Problem 5. Hashing: (18 points) Suppose we will build a hash table using open addressing with quadratic probing. The hash function is h(k,i) = (h'(k) + c1 i + c2 i^2) mod m. And the auxiliary hash function is h'(k) = k mod m. 1. Suppose we select m=7, c1=0.5, c2=0.5. Show the content of the hash table after inserting all of the following keys into the initially empty hash table in the given order: {57,38,52,16,73,8}. (4 points) 2. Explain why when using quadratic probing, we have a milder form of clustering called secondary clustering, as opposed to primary clustering, which occurs when using linear probing. (3 points) 3. How do we make sure when an item is deleted from the hash table, we can still find other item? For example, if 57 is deleted in 1., can we still find 8? (3 points) 4. Based on your answer to the previous question and using open addressing with quadratic probing, complete the C function below so that it can correctly find the item to be searched. (4 points) int search(int key, int H[], int m, int c1, int c2) { //key is the key of the item to be searched //H[] is the hash table //m, c1, c2, are the parameters used in the hash function int index=key % m; while( /* fill in the blank here */) { /* fill in the blank here */ } return index; //if the item is not found, you should set the index to be -1 } Problem 6. Multi-pattern string matching problem: (16 points) In the worst case, Rabin-Karp algorithm still has a matching time of O((n-m+1)m) = O(nm), although on average the matching time is only O(n+m), where n is the length of the string and m is the length of the pattern. Therefore, for the regular "single-pattern" string matching problem it is considered inferior to algorithm such as KMP, which has a matching time of only O(n). However, Rabin-Karp has great performance in the "multi-pattern" string matching problem. In this problem, there are k patterns of the same length m, {P1, P2, ..., Pk} and the string T of length n. A shift is valid if we can find at least a pattern Pi such that T[s+1..s+m]==Pi[1..m], 1<=i<=k. To solve the problem, you need to find all valid shifts. The pseudo-code of the original Rabin-Karp algorithm is shown at the end of the problem description. 1. Please show how you would modify the original Rabin-Karp algorithm to solve the multi-pattern string matching problem. Fill in the blank of the Rabin-Karp- Multi-Matcher function. (8 points) 2. Show that your algorithm has a matching time of O(n+km) (to simplify the analysis, as shown in the lecture notes you can assume the number of valid shifts is a constant and the probability for spurious hits is 1/q, where q is the modulus used in the algorithm and is much larger than m). (8 points) Rabin-Karp-Matcher(T, P, d, q) n=T.length m=P.length h=d^(m-1) mod q p=0 t=0 for i=1 to m p=(dp+P[i]) mod q q=(dt+T[i]) mod q for s=0 to n-m if p==t if P[1..m]==T[s+1..s+m] print "Pattern occurs with shift" s if s < n-m t=(d(t-T[s+1]h)+T[s+m+1]) mod q Rabin-Karp-Multi-Matcher(T, P[], k, d, q) /* fill in your code here */ Problem 7. Out of all topics we covered in the lectures this semester, which one do you like the most? Which one do you dislike the most? Why? Please give some constructive suggestions. Any other course-related comments are also welcomed. (I am sorry that I did not find the time to write down my comments to your feedbacks in the midterm. But I did read all of them and take them seriously. Your feedbacks are very important for me to improve the courses, so I hope that most of you can kindly write a few sentences at least. Thanks a lot!) (10 points) --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.98 ※ 編輯: poao 來自: 140.112.30.98 (06/21 19:52)
1F:推 suhorng :推 06/21 20:13
2F:推 s864372002 :推毛毛蟲 06/21 20:54
3F:推 peter506g :你真的打了XDDD 06/21 23:05
4F:推 jennyPDM :推XD 06/21 23:21







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP