SlideShare a Scribd company logo
1 of 11
‫پیشرفته‬ ‫های‬ ‫الگوریتم‬
‫تحلیل‬
‫سرشکنی‬
(
Amortized Analysis
)
‫اول‬‫بخش‬
‫مدرس‬
:
‫بهجتی‬‫شهاب‬
‫اسالید‬
1
‫از‬
11
‫مطالب‬ ‫رئوس‬
•
‫مقدمه‬
•
‫تجمعی‬‫ش‬‫و‬‫ر‬
(
Aggregate
)
•
‫ی‬‫حسابدار‬‫ش‬‫و‬‫ر‬
(
Accounting
)
•
‫پتانسیل‬‫ش‬‫و‬‫ر‬
(
Potential
)
•
‫پویا‬‫جداول‬
(
Dynamic tables
)
‫اسالید‬
2
‫از‬
11
‫مقدمه‬
•
‫است‬‫حالت‬‫بدترین‬‫تحلیل‬‫نوعی‬،‫سرشکنی‬‫تحلیل‬
.
•
‫داریم‬‫ا‬‫ر‬ ‫اعمال‬‫از‬‫توالی‬‫یک‬‫تحلیل‬‫عمل‬‫یک‬‫تحلیل‬‫جای‬‫به‬‫اما‬
.
•
‫داده‬‫ساختمان‬
D
‫است‬‫شده‬‫داده‬‫ن‬‫ا‬‫ی‬‫و‬‫ر‬‫عمل‬‫تعدادی‬‫با‬‫اه‬‫ر‬‫هم‬
.
•
‫از‬‫توالی‬‫یک‬‫حالت‬‫بدترین‬‫تحلیل‬،‫سرشکنی‬‫تحلیل‬‫از‬‫هدف‬
n
‫است‬‫داده‬‫ساختمان‬‫این‬‫ی‬‫و‬‫ر‬‫عمل‬
.
•
‫است‬‫توالی‬‫این‬‫در‬‫عمل‬‫ن‬‫ا‬‫هزینه‬‫متوسط‬‫عمل‬‫هر‬‫سرشکن‬‫هزینه‬
.
•
‫ب‬‫ن‬‫ا‬‫ی‬‫و‬‫ر‬‫شده‬‫تعریف‬‫عملیات‬‫تباط‬‫ر‬‫ا‬‫و‬‫داده‬‫ساختمان‬‫ساختار‬‫ی‬‫و‬‫ر‬‫دقت‬‫مستلزم‬‫سرشکنی‬‫تحلیل‬‫انجام‬
‫است‬‫یکدیگر‬‫ا‬
.
‫اسالید‬
۳
‫از‬
11
‫تجمعی‬ ‫روش‬
•
‫از‬‫توالی‬‫یک‬‫مجموع‬‫هزینه‬‫ابتدا‬،‫ش‬‫و‬‫ر‬‫این‬‫در‬
n
‫کنیم‬‫می‬‫محاسبه‬‫حالت‬‫بدترین‬‫در‬‫ا‬‫ر‬‫عمل‬
.
•
‫ابر‬‫ر‬‫ب‬‫شده‬‫محاسبه‬‫هزینه‬‫کنیم‬‫می‬‫فرض‬
𝑇(𝑛)
‫باشد‬
.
•
‫ابر‬‫ر‬‫ب‬‫و‬‫یکسان‬‫توالی‬‫این‬‫در‬‫عمل‬‫هر‬‫سرشکن‬‫هزینه‬،‫ت‬‫ر‬‫صو‬‫این‬‫در‬
𝑇(𝑛)
𝑛
‫است‬
.
•
‫که‬‫است‬‫این‬‫تحلیل‬‫این‬‫در‬‫توجه‬‫قابل‬‫ته‬‫نک‬
«
‫ساخ‬‫هر‬‫عملیات‬‫از‬‫توالی‬‫یک‬‫در‬‫عمل‬‫یک‬‫حالت‬‫بدترین‬
ً‫تبا‬‫ر‬‫م‬‫داده‬‫تمان‬
‫افتد‬‫نمی‬‫اتفاق‬
»
.
•
‫شویم‬‫می‬‫شنا‬‫ا‬‫ش‬‫و‬‫ر‬‫این‬‫با‬‫مثال‬‫چند‬‫ائه‬‫ر‬‫ا‬‫با‬‫ادمه‬‫در‬
.
‫اسالید‬
۴
‫از‬
11
‫تجمعی‬ ‫روش‬
:
‫پشته‬ ‫عملیات‬
•
‫پشته‬‫داده‬‫ساختمان‬
S
‫عمل‬‫دو‬‫شامل‬‫که‬‫ا‬‫ر‬‫معمولی‬
push
‫و‬
pop
‫بگیرید‬‫نظر‬‫در‬‫ا‬‫ر‬‫است‬
.
•
‫جدید‬‫عمل‬
multipop
‫کنیم‬‫می‬‫اضافه‬‫پشته‬‫عملیات‬‫به‬‫زیر‬‫ت‬‫ر‬‫صو‬‫به‬‫ا‬‫ر‬
:
•
‫ای‬‫ر‬‫اج‬‫زمان‬
multipop
‫تبه‬‫ر‬‫م‬‫از‬
Θ(min 𝑠,𝑘 )
‫ن‬‫ا‬‫در‬‫که‬‫است‬
𝑠 = 𝑆
.
•
‫عمل‬‫دو‬‫ای‬‫ر‬‫اج‬‫زمان‬
push
‫و‬
pop
‫کدام‬‫هر‬
𝑂(1)
.
•
‫سئوال‬
:
‫ای‬‫ر‬‫اج‬‫زمان‬
n
‫چیست؟‬‫حالت‬‫بدترین‬‫در‬‫تهی‬‫پشته‬‫یک‬‫ی‬‫و‬‫ر‬‫عمل‬
‫اسالید‬
۵
‫از‬
11
‫تجمعی‬ ‫روش‬
:
‫پشته‬ ‫عملیات‬
(
‫ادامه‬
)...
•
‫شتابزده‬‫تحلیل‬
:
‫از‬‫توالی‬‫یک‬‫در‬
n
‫عمل‬،‫عمل‬
multipop
‫هزینه‬‫حالت‬‫بدترین‬‫در‬
𝑂(𝑛)
‫دارد‬
.
‫نتیجه‬‫در‬
:
𝑇 n = n × 𝑂 𝑛 = 𝑂(𝑛2)
‫ابر‬‫ر‬‫ب‬‫عمل‬‫هر‬‫سرشکن‬‫هزینه‬‫و‬
𝑇 𝑛
𝑛
= 𝑂(𝑛)
‫است‬
.
•
‫دقیق‬‫تحلیل‬
:
‫است‬‫زیر‬‫کلیدی‬‫ته‬‫نک‬‫پایه‬‫بر‬
:
•
‫از‬‫توالی‬‫یک‬‫در‬‫چون‬
n
‫ثر‬‫حداک‬‫عمل‬
n
‫تا‬
push
‫ثر‬‫حداک‬‫لذا‬،‫داریم‬
n
‫تا‬
pop
(
‫عمل‬‫شامل‬‫که‬
multipop
‫هم‬
‫شود‬‫می‬
)
‫داریم‬
.
•
‫لذا‬
𝑇(𝑛) ≤ 𝑂(𝑛)
‫ابر‬‫ر‬‫ب‬‫عمل‬‫هر‬‫سرشکن‬‫هزینه‬‫و‬
𝑇 𝑛
𝑛
= 𝑂(1)
‫است‬
.
‫ته‬‫نک‬
:
‫عناصر‬‫تعداد‬
pop
‫عناصر‬‫تعداد‬‫ابر‬‫ر‬‫ب‬‫ثر‬‫حداک‬‫پشته‬‫از‬‫شده‬
push
‫است‬‫پشته‬ ‫داخل‬‫شده‬
.
‫اسالید‬
۶
‫از‬
11
‫تجمعی‬ ‫روش‬
:
‫دودویی‬ ‫شمارنده‬
•
‫ی‬‫دودوی‬‫شمانده‬
𝑘
‫بیتی‬
𝐴 = 0..𝑘 − 1
‫که‬‫بگیرید‬‫نظر‬‫در‬‫ا‬‫ر‬
𝐴[0]
‫و‬
𝐴[𝑘 − 1]
‫کم‬‫تیب‬‫ر‬‫ت‬‫به‬
‫هستند‬‫ن‬‫ا‬‫های‬‫بیت‬‫شترین‬‫ز‬‫باار‬‫و‬‫شترین‬‫ز‬‫ار‬
.
•
‫ایش‬‫ز‬‫اف‬‫عمل‬‫داده‬‫ساختمان‬‫این‬‫ی‬‫و‬‫ر‬
Increment
‫می‬‫تعریف‬‫زیر‬‫ت‬‫ر‬‫صو‬‫به‬‫ا‬‫ر‬
‫مقدار‬‫به‬‫واحد‬‫یک‬‫بار‬‫هر‬‫که‬‫کنیم‬
‫کند‬‫می‬‫اضافه‬‫نده‬‫ر‬‫شما‬
:
•
‫می‬‫فرض‬
‫است‬‫صفر‬‫ابر‬‫ر‬‫ب‬‫نده‬‫ر‬‫شما‬‫اولیه‬‫مقدار‬‫کنیم‬
.
‫ابطه‬‫ر‬‫از‬‫لحظه‬‫هر‬‫در‬‫نده‬‫ر‬‫شما‬‫مقدار‬
𝑖=0
𝑘−1
𝐴 𝑖 .2𝑖
‫دست‬‫به‬
‫می‬
‫ید‬‫ا‬
.
•
‫می‬
‫از‬‫توالی‬‫یک‬‫در‬‫عمل‬‫هر‬‫سرشکن‬‫هزینه‬‫بدانیم‬‫خواهیم‬
n
‫هزینه‬‫چه‬‫حالت‬‫بدترین‬‫در‬‫ایش‬‫ز‬‫اف‬‫عمل‬
‫دارد؟‬‫ای‬
‫اسالید‬
۷
‫از‬
11
‫تجمعی‬ ‫روش‬
:
‫دودویی‬ ‫شمارنده‬
(
‫ادامه‬
)...
•
‫حالت‬،‫مثال‬‫عنوان‬‫به‬
𝑘 = 8,𝑛 = 16
‫بگیرید‬‫نظر‬‫در‬‫زیر‬‫شکل‬‫مطابق‬‫ا‬‫ر‬
:
•
‫بیت‬
𝐴[𝑖]
‫هر‬
𝑛
2𝑖
‫تغییر‬ ‫تبه‬‫ر‬‫م‬
‫می‬
‫کند‬
(
‫فلیپ‬
.)
•
‫مجموع‬‫با‬‫متناسب‬ ‫ا‬‫ر‬‫اج‬‫زمان‬
‫است‬ ‫ها‬‫بیت‬ ‫تغییر‬‫تعداد‬
.
•
‫دست‬‫به‬‫زیر‬‫ابطه‬‫ر‬‫از‬‫مقدار‬‫این‬
‫می‬
‫ید‬‫ا‬
:
𝑇 𝑛 =
𝑖=0
𝑘−1
𝑛
2𝑖
< n
𝑖=0
∞
1
2𝑖
= 2𝑛 = 𝑂 𝑛
•
‫هزینه‬‫نتیجه‬‫در‬
‫ی‬‫تبه‬‫ر‬‫م‬‫از‬‫ایش‬‫ز‬‫اف‬‫عمل‬‫هر‬‫سرشکن‬ ‫ی‬
𝑇 𝑛
𝑛
= 𝑂(1)
‫است‬
.
‫اسالید‬
۸
‫از‬
11
‫حسابداری‬ ‫روش‬
•
‫هزینه‬‫مختلف‬‫اعمال‬‫به‬‫ش‬‫و‬‫ر‬‫ش‬‫و‬‫ر‬‫این‬‫در‬
‫می‬‫نسبت‬‫ژ‬‫شار‬ ‫عنوان‬‫به‬‫متفاوتی‬‫سرشکن‬‫های‬
‫دهیم‬
.
–
‫و‬‫است‬ ‫ن‬‫ا‬‫واقعی‬ ‫هزینه‬‫از‬ ‫بیش‬‫اعمال‬‫از‬‫برخی‬‫ژ‬‫شار‬
–
‫است‬‫ن‬‫ا‬‫واقعی‬ ‫هزینه‬‫از‬‫ر‬‫کمت‬‫اعمال‬‫از‬‫برخی‬‫ژ‬‫شار‬
.
•
‫است‬‫عمل‬‫ن‬‫ا‬‫ای‬‫ر‬‫ب‬‫شده‬‫ژ‬‫شار‬‫هزینه‬‫ان‬‫ز‬‫می‬‫ابر‬‫ر‬‫ب‬‫عمل‬‫هر‬‫سرشکن‬‫ی‬‫هزینه‬
.
•
‫ع‬‫به‬‫داده‬‫ساختمان‬‫اشیاء‬‫برخی‬‫ی‬‫و‬‫ر‬‫هزینه‬‫اد‬‫ز‬‫ما‬،‫است‬‫واقعی‬‫هزینه‬‫از‬‫بیش‬‫سرشکن‬‫ی‬‫هزینه‬‫که‬‫زمانی‬
‫یا‬‫اعتبار‬‫نوان‬
‫شود‬‫می‬‫ذخیره‬‫موجودی‬
.
•
‫پرداخت‬‫شده‬‫ذخیره‬‫اعتبار‬‫از‬‫ن‬‫ا‬‫التفاوت‬‫مابه‬،‫است‬‫واقعی‬‫هزینه‬‫از‬‫کمتر‬‫سرشکن‬‫ی‬‫هزینه‬‫که‬‫زمانی‬
‫گردد‬‫می‬
.
•
‫تجمیعی‬‫ش‬‫و‬‫ر‬ ‫با‬‫ی‬‫حسابدار‬‫ش‬‫و‬‫ر‬ ‫تفاوت‬
:
–
‫هستند‬‫مختلف‬ ‫سرشکن‬ ‫ی‬‫هزینه‬‫ای‬‫ر‬‫دا‬‫مختلف‬ ‫اعمال‬،‫ی‬‫حسابدار‬‫روش‬ ‫در‬
.
–
‫است‬ ‫یکسان‬ ‫هم‬‫با‬ ‫اعمال‬ ‫تمامی‬ ‫سرشکن‬ ‫ی‬‫هزینه‬،‫تجمیعی‬‫روش‬ ‫در‬
.
•
‫ی‬‫حسابدار‬‫ش‬‫و‬‫ر‬‫در‬‫موجودی‬‫شرط‬
:
‫باشد‬‫نامنفی‬‫همواره‬ ‫باید‬‫اعتبار‬‫یا‬‫موجودی‬‫ان‬‫ز‬‫می‬
.
•
‫شود‬‫می‬‫بیان‬‫زیر‬‫ت‬‫ر‬‫صو‬‫به‬‫موجودی‬‫شرط‬
:
𝑖=1
𝑛
𝑐𝑖 −
𝑖=1
𝑛
𝑐𝑖 ≥ 0
‫اسالید‬
۹
‫از‬
11
‫حسابداری‬ ‫روش‬
:
‫پشته‬
•
‫عمل‬‫هر‬‫ای‬‫ر‬‫ب‬
push
‫هزینه‬
2 = 𝑂(1)
‫گیریم‬‫می‬‫نظر‬‫در‬‫واحد‬
.
‫ن‬‫ا‬‫ی‬‫و‬‫ر‬‫واحد‬‫یک‬‫و‬‫عنصر‬‫درج‬‫ای‬‫ر‬‫ب‬‫واحد‬‫یک‬
‫که‬‫زمانی‬‫ای‬‫ر‬‫ب‬
pop
‫شود‬‫می‬‫ذخیره‬،‫شود‬‫می‬
.
•
‫اعمال‬‫ای‬‫ر‬‫ب‬
pop
‫و‬
multipop
‫گیریم‬‫می‬‫نظر‬‫در‬‫صفر‬‫سرشکن‬‫ی‬‫هزینه‬
.
•
‫اعمال‬‫واقعی‬‫ی‬‫هزینه‬
push
‫و‬
pop
‫ابر‬‫ر‬‫ب‬
1 = 𝑂(1)
‫است‬‫واحد‬
.
•
‫واقعی‬‫هزینه‬
multipop
‫ابر‬‫ر‬‫ب‬
𝑘′
= min(𝑠,𝑘)
‫که‬‫است‬
s
‫است‬‫پشته‬‫داخل‬‫عناصر‬‫تعداد‬
.
•
‫شامل‬‫توالی‬‫یک‬‫سرشکن‬‫هزینه‬‫مجموع‬‫ت‬‫ر‬‫صو‬‫این‬‫در‬
n
‫اعمال‬‫تعداد‬‫با‬‫متناسب‬‫عمل‬
push
‫ابر‬‫ر‬‫ب‬‫ثر‬‫حداک‬‫که‬‫بوده‬
n
‫است‬
.
‫لذا‬
𝑇 𝑛 ≤ 𝑛 × 2 = 2𝑛 = 𝑂 𝑛
.
•
‫موجودی‬‫شرط‬
:
‫ابر‬‫ر‬‫ب‬‫پشته‬‫موجودی‬
‫تعداد‬
‫است‬‫نامنفی‬‫همواره‬‫که‬‫بوده‬‫پشته‬‫داخل‬‫عناصر‬
.
‫اسالید‬
۱۰
‫از‬
11
‫حسابداری‬ ‫روش‬
:
‫دودویی‬ ‫شمارنده‬
•
‫عمل‬‫هر‬
increment
‫است‬‫کردن‬‫صفر‬‫و‬‫کردن‬‫یک‬‫ای‬‫پایه‬‫عمل‬‫دو‬‫شامل‬
.
•
‫سرشکن‬‫ی‬‫هزینه‬‫بیت‬‫یک‬‫عمل‬‫هر‬‫ای‬‫ر‬‫ب‬
2 = 𝑂(1)
‫گیریم‬‫می‬‫نظر‬‫در‬‫واحد‬
:
–
‫ای‬‫ر‬‫ب‬‫واحد‬‫یک‬
‫کردن‬‫یک‬
‫بیت‬‫ن‬‫ا‬
–
‫ای‬‫ر‬‫ب‬‫واحد‬‫یک‬
‫کردن‬‫صفر‬
‫بیت‬‫ان‬
•
‫گیریم‬‫می‬‫نظر‬‫در‬‫صفر‬‫سرشکن‬‫ی‬‫هزینه‬‫کردن‬‫صفر‬‫ای‬‫ر‬‫ب‬
.
•
‫ابر‬‫ر‬‫ب‬‫کردن‬‫صفر‬‫یا‬‫کردن‬‫یک‬‫عمل‬‫هر‬‫واقعی‬‫ی‬‫هزینه‬
1 = 𝑂(1)
‫است‬
.
•
‫شامل‬‫توالی‬‫یک‬‫در‬
n
‫عمل‬
increment
‫ثر‬‫حداک‬،
2𝑛
‫داریم‬‫بیت‬‫تغییر‬
.
•
‫ابر‬‫ر‬‫ب‬‫کل‬‫سرشکن‬‫ی‬‫هزینه‬‫لذا‬
𝑇 𝑛 = 2𝑛 × 𝑂 1 = 𝑂 𝑛
‫است‬
.
•
‫موجودی‬‫شرط‬
:
‫ابر‬‫ر‬‫ب‬‫نده‬‫ر‬‫شما‬‫موجودی‬
‫تعداد‬
1
‫است‬‫نامنفی‬‫همواره‬‫که‬‫بوده‬‫نده‬‫ر‬‫شما‬‫های‬
.
‫اسالید‬
۱۱
‫از‬
11

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Amortized Analysis(ver 0.9)-part1.pptx