NTU-Exam 板


LINE

课程名称︰ 演算法 课程性质︰ 系订选修 课程教师: 张耀文 开课学院: 电资学院 开课系所︰ 电机系 考试日期(年月日)︰ 2008/11/12 考试时限(分钟): 150mins 是否需发放奖励金: yes (如未明确表示,则不予发放) 试题 : Problem 1. (16 pts total) For each of the following recurrence relations, give the asymptotic growth rateof the solution using the θ notation. Assume in each case that T(n) is θ(1) for n≦10. (a) (5 pts) T(n) = 5T(n/2) + (n^3)(lg n). (b) (5 pts) T(n) = T(√n) + lglg n. (c) (6 pts) T(n) = T(pn) + T(qn) + n, where p + q = 1. Problem2. (20 pts total) Please give a brief answer for each of the following questions. Q1. If f(n) is asymptotically positive, is f(n) + o(f(n)) = O(f(n))? Why? Q2. Can we have a priority queue in the comparison sorting model with both the properties:(1)EXTRACT-MIN runs in O(lglg n) time, and (2)BUILD-MAX-HEAP runs in O(n) time? Why? Q3. Suppose that an array contains n numbers, each of which is 0, 1, or 2. Then, can this array be sorted in linear time in the worst case? How to do it if it can, or why it is not possible? Q4. Can we put the numbers 1,2, ...., 10 in a tree such that it is both a valid max-heap and binary search tree at the same time? Why? Problem 3. (12 pts total) Let A0 be a numerical array of length n, originally sorted into ascending order. Assume that k entries of A0 are overwritten with new values, producing an array A. Futhermore assume you have an array B containing n values (0 or 1), where B[i] is true if A[i] is one of the k values that was overwritten, and false otherwise. (a) (9 pts) Give a fast algorithm to sort A into ascending order, with time complexity better than O(nk). (Hint:Separate out A into two lists.) (b) (3 pts) Give the time complexity of your algorithm in big-O notation, as a function of n and k (the tighter, the better). Problem 4. (16 pts total) Search trees. (a) (5 pts) Give the binary search tree that results from successively inserting the keys 9,10, 2, 1, 7, 6, 8 into an initially empty tree. (b) (5 pts) Label each node in the tree with R or B denoting the respective colors RED and BLACK so that the tree is a legal red-black tree. (c) (6 pts) Give the red-black tree that results from inserting the key 3 into the tree of (b). (Hint:The pseudocode for inserting a node in a red-black tree is given below) ---------- RB-Insert(T,x) 1. Tree-Insert(T,x); 2. color[x]←RED; 3. while x ≠ root[T] and color[p[x]]=RED do 4. if p[x]=left[p[p[x]]] then 5. y←right[p[p[x]]]; 6. if color[y]=RED then /*uncle's color is red*/ /*Case 1*/ 7. color[p[x]]←BLACK; 8. color[y]←BLACK; 9. color[p[p[x]]]←RED; 10. p←[p[x]]; 11. else if x=right[p[x]] then /*uncle's color is black*/ /*Case 2*/ 12. x←p[x]; 13. Left-Rotate(T,x); /*Case 3*/ 14. color[p[x]]←BLACK; 15. color[p[p[x]]]←RED; 16. Right-Rotate(T,p[p[x]]); 17. else (same as then clause with "right" and "left" exchanged) 18. color[root[T]]←BLACK; ------------- Problem 5. (16 pts total) You are asked to determine the cost and structure of an optimal binary search tree fpr a set K=<k1,k2,k3,k4> of n=4 keys with the following prababilities: i 0 1 2 3 4 pi - 0.10 0.10 0.20 0.05 qi 0.05 0.10 0.20 0.10 0.10 a set of probabilities P=<p1, p2, p3, p4>for searching the keys in K and Q=<q0, q1, q2, q3, q4>for unsuccessful searches, as discussed in class. (a) (12 pts)Fill every field in the e,w, and root tables, where e[i,j] gives the expected cost of searching an optimal binary tree containing the keys ki,...,kj, w[i,j]=w[i,j-1] + pj + qj, and root[i,j] records the root of subtree containing the keys ki,...,kj. (b) (4 pts)Find an optimal binary search tree of the given probabilities and give the expected search cost of the tree. e w 4 1 4 1 j 3 /\ 2 i j 3 /\ 2 i 2 /\/\ 3 2 /\/\ 3 1 /\/\/\ 4 1 /\/\/\ 4 0 /\/\/\/\ 5 0 /\/\/\/\ 5 /\/\/\/\/\ /\/\/\/\/\ \/\/\/\/\/ \/\/\/\/\/ root 4 1 j 3 /\ 2 i 2 /\/\ 3 1 /\/\/\ 4 /\/\/\/\ \/\/\/\/ Problem 6. (12 pts total) Proffessor Chang plans to drive a car from Taipei to Tainan along the Formosa highway (Highway #3) for a meeting. His car's gas tank, when full, holds enough gas to travel n kilometers, and his map gives the diatances between gas stations on his route. He wishes to make as few gas stops as possible along the way. Give an efficient algorithm to determine at which gas stations he should stop and prove the optimality of your algorithm. Problem 7. (18 pts total) Given a log of wood of length k, Woody the woodcutter will cut it once, in any place you choose, for the priceof k dollars. Suppose you have a log of length L, marked to be cut in n different locations labeled 1,2,...,n. For simplicity, let indices 0 and n+1 denote the left and right endpoints of the original log of length L. Let the distance of mark i from the left end of the log be di, and assume that 0=d0<d1<d2<...<dn<d(n+1)=L. The wood-cutting problem is the problem of determining the sequence of cuts to the log that will (1) cut the log at all the marked places, and (2) minimize your total payment to Woody. (a) (4 pts) Give an example illustrating that two different sequences of cuts to the same marked log can result in two different costs. (b) (8 pts) Let c(i,j) be the minimum cost of cutting a log with left endpoint i and right endpoint j at all its marked locations. Suppose the log is cut at position m, somewhere between i and j. Define the recurrence of c(i,j) in terms of i,m,j,di and dj. Briefly justify your answer. (c) (6 pts) Using part (b), give an efficient algorithm to solve the wood- cutting problem. Use a table C of size (n+1)×(n+1) to hold the values C[i][j]=c(i,j). What is the running time of your algorithm? Problem 8. (4 pts total) Please give two of your opinion(s)/comment(s) on this course (e.g., strengths and weakness)? Any specific comments that may improve the quality of this course are very much welcome. Thank you. --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.165.159.105 ※ 编辑: Hsnuary 来自: 118.165.155.7 (11/15 08:31)







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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP