SlideShare a Scribd company logo
1 of 18
Advanced Algorithms
Exercise 5

Group 2
陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧
2014/1/3

Q1 Q8

1
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

Q1 Q8

2
Q1
• 攤銷分析

• 0 → 1 cost : 3
• 1 → 0 cost : 0
• 花費 3 來自於
1.將自己從 0 變成 1
2.預先支付自己會從 1 變成 0
3.重設時的花費 0 → 1 or 1 → 0
• 因此整體來說是 O(n)
2014/1/3

Q1 Q8

3
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

Q1 Q8

4
Q2 – efficient algorithm
stack<> instack, outstack;
Enqueue(val)
instack.push(val)
Dequeue() {
if(outstack.empty)
while(!instack.empty)
outstack.push(instack.pop())
return outstack.pop();
}
2014/1/3

Q1 Q8

5
Q2
• 攤銷分析
• 只會進入 instack push
之後再從 instack pop
接著再從 outstack push
最後再從 outstack pop
•
Enqueue cost 4
Dequeue cost 0

2014/1/3

Q1 Q8

1
1
1
1

次,
次,
次,
次。

6
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 EXTRACTMIN is O(1) and show that it works. Recall that
heapsort consists of two phases: an O(n) time heapconstruction 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

Q1 Q8

7
Q3
• INSERT
cost 2logn
EXTRACT-MIN cost 1
攤銷分析是預先支付
INSERT 先幫 EXTRACT 支付費用

• 但是 O(n)-heap-construction 並沒有預先支
付 EXTRACT-MIN 的操作,因此不能說
EXTRACT-MIN 的操作是 O(1)。更不能推論到
heap 整體是 O(n)

2014/1/3

Q1 Q8

8
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|/2
elements 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 implementation
should also include a way to output the elements of
S in O(|S|) time.
2014/1/3
Q1 Q8
9
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|/2
elements 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
Q1 Q8
10
Q4
• INSERT is
• one for
• one for
• one for
• one for
• one for
• one for

6 :
inserting itself
moving itself
moving another
removing itself
removing another
select-algorithm cost

• DELETE-HALF is ?

2014/1/3

Q1 Q8

11
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

Q1 Q8

12
Q5
• NOT SURE.
• TABLE-DELETE cost 2
The total credit = size[T]*2/3 - num[T]

2014/1/3

Q1 Q8

13
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 
nk1, nk2, … , n0 We have k sorted rrays
.
A0, A1, … , Ak1, where for i = 0, 1, …, k1, 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 worstcase and amortized running times.
c) Discuss how to implement DELETE.

•
•

2014/1/3

Q1 Q8

14
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

Q1 Q8

15
Q7
• Study the famous KMP algorithm for the
string matching problem and give an
amortized analysis of the algorithm.

2014/1/3

Q1 Q8

16
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

1 Q8

17
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

1 Q8

18

More Related Content

What's hot

不偏ゲームがNIMに帰着する証明
不偏ゲームがNIMに帰着する証明不偏ゲームがNIMに帰着する証明
不偏ゲームがNIMに帰着する証明Azaika At
 
AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Inc.
 
تحليل وحيدات الحد
تحليل وحيدات الحدتحليل وحيدات الحد
تحليل وحيدات الحدng1234567ng
 
AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説AtCoder Inc.
 
Deep Learning A-Z™: AutoEncoders - Module 6
Deep Learning A-Z™: AutoEncoders  - Module 6Deep Learning A-Z™: AutoEncoders  - Module 6
Deep Learning A-Z™: AutoEncoders - Module 6Kirill Eremenko
 
ネットワークフロー
ネットワークフローネットワークフロー
ネットワークフローKohei Shinohara
 
Indeedなう B日程 解説
Indeedなう B日程 解説Indeedなう B日程 解説
Indeedなう B日程 解説AtCoder Inc.
 
Yolo v1 urop 발표자료
Yolo v1 urop 발표자료Yolo v1 urop 발표자료
Yolo v1 urop 발표자료DaeHeeKim31
 
παδ. 2 κανακη σοφια
παδ. 2   κανακη σοφιαπαδ. 2   κανακη σοφια
παδ. 2 κανακη σοφιαSoknk
 
AtCoder Beginner Contest 008 解説
AtCoder Beginner Contest 008 解説AtCoder Beginner Contest 008 解説
AtCoder Beginner Contest 008 解説AtCoder Inc.
 
Codasip application class RISC-V processor solutions
Codasip application class RISC-V processor solutionsCodasip application class RISC-V processor solutions
Codasip application class RISC-V processor solutionsRISC-V International
 
Coronary artery bypass grafting (CABG)
Coronary artery bypass grafting (CABG)Coronary artery bypass grafting (CABG)
Coronary artery bypass grafting (CABG)Robert Chen
 
Πρώτες Βοήθειες σε κακώσεις των άκρων
Πρώτες Βοήθειες σε κακώσεις των άκρωνΠρώτες Βοήθειες σε κακώσεις των άκρων
Πρώτες Βοήθειες σε κακώσεις των άκρωνCostasPanayotidis
 
AtCoder Regular Contest 001
AtCoder Regular Contest 001AtCoder Regular Contest 001
AtCoder Regular Contest 001AtCoder Inc.
 
Code Formula 2014 予選A 解説
Code Formula 2014 予選A 解説Code Formula 2014 予選A 解説
Code Formula 2014 予選A 解説AtCoder Inc.
 
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forestMutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forestJun Kurihara
 
AtCoder Beginner Contest 021 解説
AtCoder Beginner Contest 021 解説AtCoder Beginner Contest 021 解説
AtCoder Beginner Contest 021 解説AtCoder Inc.
 
ενδοκρινείς αδένες (ι)
ενδοκρινείς αδένες (ι)ενδοκρινείς αδένες (ι)
ενδοκρινείς αδένες (ι)dkelefiotis
 
Nimとgrundy数の大雑把な説明
Nimとgrundy数の大雑把な説明Nimとgrundy数の大雑把な説明
Nimとgrundy数の大雑把な説明yuta kasai
 
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理QlikPresalesJapan
 

What's hot (20)

不偏ゲームがNIMに帰着する証明
不偏ゲームがNIMに帰着する証明不偏ゲームがNIMに帰着する証明
不偏ゲームがNIMに帰着する証明
 
AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説
 
تحليل وحيدات الحد
تحليل وحيدات الحدتحليل وحيدات الحد
تحليل وحيدات الحد
 
AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説
 
Deep Learning A-Z™: AutoEncoders - Module 6
Deep Learning A-Z™: AutoEncoders  - Module 6Deep Learning A-Z™: AutoEncoders  - Module 6
Deep Learning A-Z™: AutoEncoders - Module 6
 
ネットワークフロー
ネットワークフローネットワークフロー
ネットワークフロー
 
Indeedなう B日程 解説
Indeedなう B日程 解説Indeedなう B日程 解説
Indeedなう B日程 解説
 
Yolo v1 urop 발표자료
Yolo v1 urop 발표자료Yolo v1 urop 발표자료
Yolo v1 urop 발표자료
 
παδ. 2 κανακη σοφια
παδ. 2   κανακη σοφιαπαδ. 2   κανακη σοφια
παδ. 2 κανακη σοφια
 
AtCoder Beginner Contest 008 解説
AtCoder Beginner Contest 008 解説AtCoder Beginner Contest 008 解説
AtCoder Beginner Contest 008 解説
 
Codasip application class RISC-V processor solutions
Codasip application class RISC-V processor solutionsCodasip application class RISC-V processor solutions
Codasip application class RISC-V processor solutions
 
Coronary artery bypass grafting (CABG)
Coronary artery bypass grafting (CABG)Coronary artery bypass grafting (CABG)
Coronary artery bypass grafting (CABG)
 
Πρώτες Βοήθειες σε κακώσεις των άκρων
Πρώτες Βοήθειες σε κακώσεις των άκρωνΠρώτες Βοήθειες σε κακώσεις των άκρων
Πρώτες Βοήθειες σε κακώσεις των άκρων
 
AtCoder Regular Contest 001
AtCoder Regular Contest 001AtCoder Regular Contest 001
AtCoder Regular Contest 001
 
Code Formula 2014 予選A 解説
Code Formula 2014 予選A 解説Code Formula 2014 予選A 解説
Code Formula 2014 予選A 解説
 
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forestMutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
Mutualized Oblivious DNS (μODNS): Hiding a tree in the wild forest
 
AtCoder Beginner Contest 021 解説
AtCoder Beginner Contest 021 解説AtCoder Beginner Contest 021 解説
AtCoder Beginner Contest 021 解説
 
ενδοκρινείς αδένες (ι)
ενδοκρινείς αδένες (ι)ενδοκρινείς αδένες (ι)
ενδοκρινείς αδένες (ι)
 
Nimとgrundy数の大雑把な説明
Nimとgrundy数の大雑把な説明Nimとgrundy数の大雑把な説明
Nimとgrundy数の大雑把な説明
 
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理
TECHTALK 20230214 ビジネスユーザー向け機械学習入門 第2回~機械学習のための学習データの前処理
 

Similar to Aaex5 group2(中英夾雜)

DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdfASMAALWADEE2
 
Lecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfLecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfssuser400105
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!idescitation
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithmsDr. Rupa Ch
 
IntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docxIntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docxmariuse18nolet
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptxASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptxsunitha1792
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptxEnosSalar
 

Similar to Aaex5 group2(中英夾雜) (20)

220exercises2
220exercises2220exercises2
220exercises2
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
Lecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfLecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdf
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!
K-Sort: A New Sorting Algorithm that Beats Heap Sort for n 70 Lakhs!
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Aaex3 group2
Aaex3 group2Aaex3 group2
Aaex3 group2
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
IntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docxIntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docx
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptxASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 

More from Shiang-Yun Yang

Polarity analysis for sentiment classification
Polarity analysis for sentiment classificationPolarity analysis for sentiment classification
Polarity analysis for sentiment classificationShiang-Yun Yang
 
文明的進程第十組
文明的進程第十組文明的進程第十組
文明的進程第十組Shiang-Yun Yang
 
計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover setsShiang-Yun Yang
 
N grams as linguistic features
N grams as linguistic featuresN grams as linguistic features
N grams as linguistic featuresShiang-Yun Yang
 
第二十組福斯汽車
第二十組福斯汽車第二十組福斯汽車
第二十組福斯汽車Shiang-Yun Yang
 
計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...Shiang-Yun Yang
 
Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Shiang-Yun Yang
 
Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Shiang-Yun Yang
 

More from Shiang-Yun Yang (14)

User interface
User interfaceUser interface
User interface
 
Polarity analysis for sentiment classification
Polarity analysis for sentiment classificationPolarity analysis for sentiment classification
Polarity analysis for sentiment classification
 
文明的進程第十組
文明的進程第十組文明的進程第十組
文明的進程第十組
 
計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets計算幾何論文報告 Minimum local disk cover sets
計算幾何論文報告 Minimum local disk cover sets
 
N grams as linguistic features
N grams as linguistic featuresN grams as linguistic features
N grams as linguistic features
 
軍事報告 電磁砲
軍事報告 電磁砲軍事報告 電磁砲
軍事報告 電磁砲
 
第二十組福斯汽車
第二十組福斯汽車第二十組福斯汽車
第二十組福斯汽車
 
敏捷簡報
敏捷簡報敏捷簡報
敏捷簡報
 
計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...計算型智慧論文報告 Building optimal regression tree ...
計算型智慧論文報告 Building optimal regression tree ...
 
Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)
 
Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探Rpg 角色扮演遊戲 – 初探
Rpg 角色扮演遊戲 – 初探
 
通識報告
通識報告通識報告
通識報告
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
 
Alex1 group2
Alex1 group2Alex1 group2
Alex1 group2
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

Aaex5 group2(中英夾雜)

  • 1. Advanced Algorithms Exercise 5 Group 2 陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧 2014/1/3 Q1 Q8 1
  • 2. 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 Q1 Q8 2
  • 3. Q1 • 攤銷分析 • 0 → 1 cost : 3 • 1 → 0 cost : 0 • 花費 3 來自於 1.將自己從 0 變成 1 2.預先支付自己會從 1 變成 0 3.重設時的花費 0 → 1 or 1 → 0 • 因此整體來說是 O(n) 2014/1/3 Q1 Q8 3
  • 4. 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 Q1 Q8 4
  • 5. Q2 – efficient algorithm stack<> instack, outstack; Enqueue(val) instack.push(val) Dequeue() { if(outstack.empty) while(!instack.empty) outstack.push(instack.pop()) return outstack.pop(); } 2014/1/3 Q1 Q8 5
  • 6. Q2 • 攤銷分析 • 只會進入 instack push 之後再從 instack pop 接著再從 outstack push 最後再從 outstack pop • Enqueue cost 4 Dequeue cost 0 2014/1/3 Q1 Q8 1 1 1 1 次, 次, 次, 次。 6
  • 7. 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 EXTRACTMIN is O(1) and show that it works. Recall that heapsort consists of two phases: an O(n) time heapconstruction 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 Q1 Q8 7
  • 8. Q3 • INSERT cost 2logn EXTRACT-MIN cost 1 攤銷分析是預先支付 INSERT 先幫 EXTRACT 支付費用 • 但是 O(n)-heap-construction 並沒有預先支 付 EXTRACT-MIN 的操作,因此不能說 EXTRACT-MIN 的操作是 O(1)。更不能推論到 heap 整體是 O(n) 2014/1/3 Q1 Q8 8
  • 9. 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|/2 elements 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 implementation should also include a way to output the elements of S in O(|S|) time. 2014/1/3 Q1 Q8 9
  • 10. 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|/2 elements 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 Q1 Q8 10
  • 11. Q4 • INSERT is • one for • one for • one for • one for • one for • one for 6 : inserting itself moving itself moving another removing itself removing another select-algorithm cost • DELETE-HALF is ? 2014/1/3 Q1 Q8 11
  • 12. 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 Q1 Q8 12
  • 13. Q5 • NOT SURE. • TABLE-DELETE cost 2 The total credit = size[T]*2/3 - num[T] 2014/1/3 Q1 Q8 13
  • 14. 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  nk1, nk2, … , n0 We have k sorted rrays . A0, A1, … , Ak1, where for i = 0, 1, …, k1, 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 worstcase and amortized running times. c) Discuss how to implement DELETE. • • 2014/1/3 Q1 Q8 14
  • 15. 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 Q1 Q8 15
  • 16. Q7 • Study the famous KMP algorithm for the string matching problem and give an amortized analysis of the algorithm. 2014/1/3 Q1 Q8 16
  • 17. 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 1 Q8 17
  • 18. 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 1 Q8 18