aaex5 group2(中英夾雜)

18
Advanced Algorithms Exercise 5 Group 2 陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧 2014/1/3 1 Q1 Q8

Upload: shiang-yun-yang

Post on 12-Jul-2015

728 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Aaex5 group2(中英夾雜)

Advanced Algorithms

Exercise 5

Group 2

陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧

2014/1/3 1Q1 Q8

Page 2: Aaex5 group2(中英夾雜)

Q1

• Suppose we wish not only to increment a counter but also to reset it to zero (i.e., make all bits in it 0). Counting the time to examine or modify a bit as O(1), show how to implement a counter as an array of bits so that any sequence of n INCREMENT and RESET operations takes time O(n) on an initially zero counter.(Hint: Keep a pointer to the high-order 1.)

2014/1/3 2Q1 Q8

Page 3: Aaex5 group2(中英夾雜)

Q1

• 攤銷分析

• 0 → 1 cost : 3

• 1 → 0 cost : 0

• 花費 3 來自於

1.將自己從 0 變成 12.預先支付自己會從 1 變成 03.重設時的花費 0 → 1 or 1 → 0

• 因此整體來說是 O(n)2014/1/3 3Q1 Q8

Page 4: Aaex5 group2(中英夾雜)

Q2

• Show how to implement a queue with two ordinary stacks so that the amortized cost of each Enqueue and Dequeue operation is O(1).

2014/1/3 4Q1 Q8

Page 5: Aaex5 group2(中英夾雜)

Q2 – efficient algorithm

2014/1/3 5Q1 Q8

stack<> instack, outstack;

Enqueue(val)instack.push(val)

Dequeue() {if(outstack.empty)

while(!instack.empty)outstack.push(instack.pop())

return outstack.pop();}

Page 6: Aaex5 group2(中英夾雜)

Q2

• 攤銷分析

• 只會進入 instack push 1 次,

之後再從 instack pop 1 次,

接著再從 outstack push 1 次,

最後再從 outstack pop 1 次。

Enqueue cost 4Dequeue cost 0

2014/1/3 6Q1 Q8

Page 7: Aaex5 group2(中英夾雜)

Q3

• Consider an ordinary binary min-heap data structure with n elements supporting the instructions INSERT and EXTRACT-MIN in O(lg n) worst-case time. Give a potential function such that the amortized cost of INSERT is O(lgn) and the amortized cost of EXTRACT-MIN is O(1) and show that it works. Recall that heapsort consists of two phases: an O(n) time heap-construction phase and a sequence of n Extract-Min operations. According to the above amortized analysis, does it imply that the time complexity of heapsort is O(n)?

2014/1/3 7Q1 Q8

Page 8: Aaex5 group2(中英夾雜)

Q3

• INSERT cost 2lognEXTRACT-MIN cost 1攤銷分析是預先支付

INSERT 先幫 EXTRACT 支付費用

• 但是 O(n)-heap-construction 並沒有預先支

付 EXTRACT-MIN 的操作,因此不能說

EXTRACT-MIN 的操作是 O(1)。更不能推論到

heap 整體是 O(n)

2014/1/3 8Q1 Q8

Page 9: Aaex5 group2(中英夾雜)

Q4

• Design a data structure to support the following two operations for a dynamic multiset S of integers, which allows duplicate values:

INSERT(S, x) inserts x into S.

DELETE-LARGER-HALF(S) deletes the largest |S|/2elements from S.

Explain how to implement this data structure so that any sequence of m INSERT and DELETE-LARGER-HALF operations runs in O(m) time. Your implementationshould also include a way to output the elements of S in O(|S|) time.

2014/1/3 9Q1 Q8

Page 10: Aaex5 group2(中英夾雜)

Q4

• Key : Dynamic Tables & Selection algorithm

• Dynamic Tables : INSERT & DELETE for all elements -O(n)

• (1) INSERT(S, x) inserts x into S.using the same step with Dynamic Table - O(1)

• (2) DELETE-LARGER-HALF(S) deletes the largest |S|/2elements from S.

same as DELETE |S|/2 elements - O(|S|/2)but it will use selection algorithm move the largest

|S|/2 elements to the tail. Selection algorithm by O(|S|)

2014/1/3 10Q1 Q8

Page 11: Aaex5 group2(中英夾雜)

Q4

• INSERT is 6 :• one for inserting itself• one for moving itself• one for moving another• one for removing itself• one for removing another• one for select-algorithm cost

• DELETE-HALF is ?

2014/1/3 11Q1 Q8

Page 12: Aaex5 group2(中英夾雜)

Q5

• Suppose that in the dynamic table operations, instead of contracting a table by halving its size when its load factor drops below 1/4, we contract it by multiplying its size by 2/3 when its load factor drops below 1/3. What is the amortized cost of a TABLE-DELETE that uses this strategy?

2014/1/3 12Q1 Q8

Page 13: Aaex5 group2(中英夾雜)

Q5

• NOT SURE.

• TABLE-DELETE cost 2The total credit = size[T]*2/3 - num[T]

2014/1/3 13Q1 Q8

Page 14: Aaex5 group2(中英夾雜)

Q6

• Binary search of a sorted array takes logarithmic search time, but the time to insert a new element is linear in the size of the array. We can improve the time for insertion by keeping several sorted arrays. Specifically, suppose that we wish to support SEARCH and INSERT on a set of n elements. Let k = lg(n+1), and let the binary representation of n be nk1, nk2, … , n0. We have k sorted rraysA0, A1, … , Ak1, where for i = 0, 1, …, k1, the length of array Ai is 2^i. Each array is either full or empty, depending on whether ni = 1 or ni = 0, respectively. The total number of elements held in all k arrays is therefore exactly n. Although each individual array is sorted, elements in different arrays bear no particular relationship to each other.

• a) Describe how to perform the SEARCH operation for this data structure. Analyze its worst-case running time.

• b) Describe how to perform the INSERT operation. Analyze its worst-case and amortized running times.

• c) Discuss how to implement DELETE.

2014/1/3 14Q1 Q8

Page 15: Aaex5 group2(中英夾雜)

Q6

• 共有 log(n+1) 堆,每堆 n/ log(n+1) 個數字,這 log(n+1) 個陣列分

別做排序,維護每一堆的個數在 [n/log(n+1)/2, n/(log(n+1))] 之間。

• a) SEARCH O(log n * log n)// 對每一堆都進行二分搜尋 - O(log n * log n)

• b) 找未滿堆中最少元素的堆 O(log n)(1) 如果找得到未滿堆,直接對其進行插入排序 O(n/log n)(2) 將一堆滿的,對分兩堆,對其中一半進行插入排序 O(n/log n) INSERT O(n/log n)

• c) 找進行刪除 O(n/log n + log n * log n)// 當有一堆維護條件不足時,找最大堆進行合併排序 O(n / log n)// 當刪除後堆數不會變少的情況下,再進行均分。

DELETE O(n/log n)

2014/1/3 15Q1 Q8

Page 16: Aaex5 group2(中英夾雜)

Q7

• Study the famous KMP algorithm for the string matching problem and give anamortized analysis of the algorithm.

2014/1/3 16Q1 Q8

Page 17: Aaex5 group2(中英夾雜)

Q7

• j:=0; for i:=1 to n do begin while (j>0) and (B[j+1]<>A[i]) do j:=P[j]; if B[j+1]=A[i] then j:=j+1; if j=m then begin writeln('Pattern occurs with shift ',i-m);

j:=P[j];end;

2014/1/3 171 Q8

Page 18: Aaex5 group2(中英夾雜)

Q7

• 为什么这个程序是 O(n) 的?其实,主要的争议在于,while 循环

使得执行次数出现了不确定因素。我们将用到时间复杂度的摊还分析

中的主要策略,简单地说就是通过观察某一个变量或函数值的变化来

对零散的、杂乱的、不规则的执行次数进行累计。

• KMP 的时间复杂度分析可谓摊还分析的典型。我们从上述程序的 j 值入手。每一次执行 while 循环都会使 j 减小(但不能减成负

的),而另外的改变 j 值的地方只有第五行。每次执行了这一行,

j 都只能加1;因此,整个过程中j最多加了 n个 1。于是,j最多只

有n次减小的机会(j值减小的次数当然不能超过n,因为j永远是非负

整数)。这告诉我们,while循环总共最多执行了n次。按照摊还分析

的说法,平摊到每次for循环中后,一次 for循环的复杂度为O(1)。

整个过程显然是O(n)的。

• 这样的分析对于后面P数组预处理的过程同样有效,同样可以得到预

处理过程的复杂度为 O(m)。

2014/1/3 181 Q8