- Mark
Length i | 1 | 2 | 3 | 4 |
---|---|---|---|---|
Price Pi | 15 | 20 | 33 | 36 |
Limit Li | 2 | 1 | 1 | 1 |
Example : length = 4
4 => 36
1,3 => 48
2,1,1 => 50 best
最優子結構解法:
1,1,1,1 => 60
在有 Limit Li 的限制下,rod cutting problem將子問題獨立解決(如ex.會超出limit限制),所以最優子結構不存在。
所求:
- xian
Activity | 1 | 2 | 3 |
---|---|---|---|
Start | 6 | 5 | 1 |
Finish | 10 | 8 | 6 |
Duration | 4 | 3 | 5 |
Greedy:
Optimal:
Activity | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
Start | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 5 | 6 |
Finish | 2 | 3 | 3 | 4 | 5 | 6 | 7 | 7 | 8 |
# of overlapping activities | 2 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 2 |
Greedy:
Optimal:
Activity | 1 | 2 | 3 | 4 |
---|---|---|---|---|
Start | 2 | 1 | 3 | 5 |
Finish | 4 | 8 | 4 | 7 |
earliest start time | 2 | 1 | 3 | 5 |
Greedy:
Optimal:
- yee
Consider a modification to the activity-selection problem in which each activity has, in addition to a start and finish time, a value . The objective is no longer to maximum the number of activities scheduled, but instead to maximize the total value of the activities scheduled. That is, we wish to choose a set of compatible activities such that is maximized. Give a polynomial-time algorithm for this problem. (Hint: refer to Exercise 16.1-1)
透過DP來解此題,主要要思考如何找到合適的最佳解,並且老師說時間複雜度要到,所以用何種方法找就蠻重要的,於是我採用bottom-up的方式來做,並且用BinarySearch來降低找最佳解的時間。
: 兩次排序所花時間為 加上乘上一次 所花時間為 加DP建表時間 ,總時間複雜度為
: 多建了三個陣列來記錄 因此空間複雜度為
- songchiu
解題方式:透過DP來解
【用到的變數】
【遞迴式】
()
【蘇都扣】
【題目所求的解答】
即是該陣列最右下角的元素
【時間複雜度】
跟a小題的方式很類似,但要針對初始值進行一點修改
這樣就可以確保DP表格的資料,只有在「剛好拿滿特定值」時,才會被存進去
【遞迴式】
【蘇都扣】
【時間複雜度】
,但是有可能會,所以是個pseudo-polynomial time algorithm
- chung
Professor Gekko has always dreamed of inline skating across North Dakota. He plans to cross the state on highway U.S. 2, which runs from Grand Forks, on the eastern border with Minnesota, to Williston, near the western border with Montana. The professor can carry two liters of water, and he can skate m miles before running out of water. (Because North Dakota is relatively flat, the professor does not have to worry about drinking water at a greater rate on uphill sections than on flat or downhill sections.) The professor will start in Grand Forks with two full liters of water. His official North Dakota state map shows all the places along U.S. 2 at which he can refill his water and the distances between these locations.
The professor’s goal is to minimize the number of water stops along his route across the state. Give an efficient method by which he can determine which water stops he should make. Prove that your strategy yields an optimal solution, and give its running time.
此題採用Greedy Approach,每次先確認剩餘水量是否可以到達下一個補水站,如果可以就跳過繼續走,反之則停留裝水
【Prove Optimal Substructure】
假設總共有n個補水站,s是最佳補水次數。令k為最佳解的第一個補水站,可分為k和n-k兩個子問題,n-k個補水站中補了s-1次水,若在這個子問題中存在更好的最佳解,則最佳補水次數會小於s,與當初假設矛盾,故得證。
【Prove Greedy-Choice Property】
假設在起點m英里範圍內有n個補給點。 Greedy-Choice解決方案選擇第k個位置作為其第一站,第k個位置以外的點都不能作為第一站,因為Professor Gekko會先把水用完,如果一個解法是選擇j<k的位置作為第一站,則Professor Gekko仍可以選擇第k個位置,使得他離開k點時,水至少和他選擇第j個位置一樣多,因此,如果他選了第k點作為第一站,他可以走到同樣遠的地方而不需增加加水次數,故可證明每次到最遠的地方加水是最佳解。
時間複雜度 = O(n)
- chung
Show how to solve the fractional knapsack problem in O(n) time.
Fractional Knapsack Problem中物品可以被切割,取物時可只取部分比例。此題採用Greedy Approach,優先取出單位價值(價值與重量的比值)最高的物品,直到背包無法裝入為止。
假設W是背包容量,R是所有物品的性價比
Find the ratio(profit/weight)= O(n) time, then find the median item(using selection procedure).The median of medians algorithm returns the median of a given array in time O(n). The recurrence here is T(n)=T(n/2)+O(n), and we have that T(n)=O(n).
快速選擇(Quickselect)是一種選擇元素的演算法,利用Quick Sort演算法,在排序序列的同時,選擇出序列中第K小或是第K大的元素。若我們只想要從序列中找出一個第K小或是第K大元素,使用Quickselect會比使用Quick Sort來得快很多,因為前者不需要把序列的排序完整做完,平均只需線性時間即可找到結果,不過最差時間複雜度仍要。
快速選擇(Quickselect)演算法,快速尋找第K小或是第K大的元素
Quick Sort 演算法原理與實作
Generalize Huffman's algorithm to ternary codewords (i.e., codewords using the
symbols 0, 1, and 2), and prove that it yields optimal ternary codes.
使用三元樹,每次挑出出現頻率最低的三個符號,將三者的頻率總和作為新節點的頻率
對於任意三元樹 Leaf node數量 = Internal node數x2+1
對於節點數偶數加上頻率為0的dummy,使其在建構過程保持三元樹的性質
Time complexity:
Let be a alphabet with frequency defined for each character .
Let be three characters in with minimum frequencies. Then there exists an optimal prefix code for in which the codewords for have the same length and differ only in the last bit.
假設 其中一個在上一層,將其調換編碼長度會更低
同理可證與,與
Let be three characters in C with minimum frequency. Let = . Define for as for , and . Let be any tree representing an optimal prefix code for the alphabet . Then the tree , obtained from by replacing the leaf node for with an internal node having as children, represents an optimal prefix code for the alphabet C.
假設不是最佳解,即存在一個,以致,從引理一可得知中存在sebling關係的,如果我們將與其parent改為一節點,且,稱作那麼
此時發生矛盾,即並非Optimal coding tree for ,由此反證必為Optimal coding tree for 。
- LXS
Find the Huffman codes of the data given below by drawing the tree like the
figure 16.5 in the page 432. You should write down each step of the Huffman’s
algorithm.
a:22 b:2 c:5 d:3 e:5 f:8 g:7 h:11
Character | Huffman code | frequency | depth/length |
---|---|---|---|
a | 11 | 22 | 2 |
b | 10100 | 2 | 5 |
c | 0011 | 5 | 4 |
d | 10101 | 3 | 5 |
e | 010 | 5 | 3 |
f | 100 | 8 | 3 |
g | 011 | 7 | 3 |
h | 00 | 11 | 2 |