درس طراحی الگوریتم ها (با شبه کد های c ++)

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Median/Order Statistics Algorithms
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Notes on the analysis of multiplication algorithms.. Dr. M. Sakalli, Marmara University.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Recursion!. Can a method call another method? YES.
PSU CS 311 – Algorithm Design and Analysis Dr. Mohamed Tounsi 1 CS 311 Design and Algorithms Analysis Dr. Mohamed Tounsi
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Analysis and Design of Algorithms. According to math historians the true origin of the word algorism: comes from a famous Persian author named ál-Khâwrázmî.
Chapter 2 - Mathematical Review Functions
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Data Structure Introduction.
Dr. O.Bushehrian ALGORITHM DESIGN MINIMUM SPANNING TREES.
Introduction to Analysis of Algorithms CS342 S2004.
Time Complexity of Algorithms
Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.
Lecture 2COMPSCI AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) 
Foundations of Algorithms, Fourth Edition
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
© Haluk Bingöl v2.23 Data Structures and Algorithms - 01 Algorithms Analysis Dr. Haluk Bingöl BÜ - CmpE BU-SWE.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Questions 4) What type of algorithmic problem-solving technique (greedy, divide-and-conquer, dynamic programming)
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Презентацию подготовила Хайруллина Ч.А. Муслюмовская гимназия Подготовка к части С ЕГЭ.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 2 Algorithm Analysis
Sum of natural numbers class SumOfNaturalNumbers {
Reasons to study Data Structures & Algorithms
Algorithm Analysis The case of recursion.
Foundations of Algorithm 유관우
Amortised Analysis.
CS 3343: Analysis of Algorithms
Andy Wang Object Oriented Programming in C++ COP 3330
فصل سوم: برنامه نویسی پویا
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
ساختمان داده ها و الگوریتم ها
Data Structures and Algorithms
מבני נתונים ויעילות אלגוריתמים
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
CS201: Data Structures and Discrete Mathematics I
שיעור רביעי: פונקציות, מבוא לרקורסיה
Programming and Data Structure
Programming and Data Structure
Reasons to study Data Structures & Algorithms
Data Structures for Java William H. Ford William R. Topp
Algorithms Dr. Youn-Hee Han April-May 2013
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
How much does your program take ?
Module 6: Introduction to Parallel Computing
Running Time Exercises
Presentation transcript:

درس طراحی الگوریتم ها (با شبه کد های c ++) تعداد واحد: 3 تهیه کننده : جعفر پورامینی منبع : کتاب طراحی الگوریتمها مترجم : جعفر نژاد قمی

فصل اول کارایی ، تحلیل و مرتبه الگوریتم ها فصل اول کارایی ، تحلیل و مرتبه الگوریتم ها

این کتاب در باره تکنیک های مربوط به حل مسائل است. تکنیک ، روش مورد استفاده در حل مسائل است. مسئله ، پرسشی است که به دنبال پاسخ آن هستیم.

بکار بردن تکنیک منجر به روشی گام به گام (الگوریتم ) در حل یک مسئله می شود. منظورازسریع بودن یک الگوریتم، یعنی تحلیل آن از لحاظ زمان و حافظه.

الگوریتم: روال قدم به قدم برای حل همه نمونه ها مثالی از یک مسئله: تعیین کنید که آیا عدد x در لیست s متشکل از n عدد وجود دارد یا خیر؟ پارامترهای این مسئله: مسئله مقادیر خاصی به آنها انتساب می دهد. s, n, x الگوریتم: روال قدم به قدم برای حل همه نمونه ها

نوشتن الگوریتم به زبان فارسی دو ایراد دارد: 1- نوشتن الگوریتم های پیچیده به این شیوه دشوار است. 2- مشخص نیست از توصیف فارسی الگوریتم چگونه می توان یک برنامه کامپیوتری ایجاد کرد.

الگوریتم 1-1: جست و جوی ترتیبی Void seqsearch ( int n const keytype S[ ] keytype x, index& location) { location = 1; while (location <= n && S[location] ! = x) location++; if (location > n ) location = 0 ; } هدف ارائه الگوریتم واضح، قابل فهم و مستقل از زبان برنامه نویسی است

الگوریتم 2-1:محاسبه مجموع عناصر آرایه number sum (int n , const number s[ ]) { index i; number result; result = 0; for (i = 1; i <= n; i++) result = result + s[i]; return result; }

الگوریتم 3-1:مرتب سازی تعویضی مسئله: n کلید را به ترتیب غیر نزولی مرتب سازی کنید. void exchangesort (int n , keytype S[ ]) { index i,j; for (i = 1 ; i<= n -1; i++) for (j = i +1; j <= n ; j++) if ( S[j] < S[i]) exchange S[i] and S[j]; }

الگوریتم 4-1:ضرب ماتریس ها void matrixmult (int n const number A [ ] [ ], const number B [ ] [ ], number C [ ] [ ], { index i , j, k; for ( i = 1; I <= n ; i++) for (i = 1; j <= n ; j++) C [i] [j] = 0; for (k = 1 ; k <= n ; k++) C [i][j] = C[i] [j] + A [i][k] * B [k][j] } }

2- 1اهمیت ساخت الگوریتم های کارآمد جست و جوی دودویی معمولا بسیار سریع تر ازجست و جوی ترتیبی است. تعداد مقایسه های انجام شده توسط جست و جوی دودویی برابر با lg n + 1 است . اندازه آرایه تعداد مقایسه های انجام شده توسط جستجوی ترتیبی تعداد مقایسه های انجام شده توسط جستجوی دودویی 128 8 1024 11 1048576 21 4294967294 33

الگوریتم 1-1: جست و جوی ترتیبی Void seqsearch ( int n const keytype S[ ] keytype x, index& location) { location = 1; while (location <= n && S[location] ! = x) location++; if (location > n ) location = 0 ;

الگوریتم 5-1: جست و جوی دودویی Void binsearch (int n, const keytype S[ ], keytype x, index& location) { index low, high, mid; low = 1 ; high = n; location = 0; while (low <= high && location = = 0) { mid = Į(low + high)/2⌡;

if ( x = = S [mid]) location = mid; else if (x < S [mid]) high = mid – 1; else low = mid + 1; }

جست وجوی دودویی-تحلیل در داخل حلقه با یک پیاده سازی مناسب یک مقایسه داریم. بیشترین تعداد مقایسه: x از همه کوچک و یا از همه بزرگتر اگر n=32 و x از همه بزرگتر: مقایسه با اندیسهای 16، 24، 28، 30، 31، 32 تعداد کل مقایسه: lg n + 1

الگوریتم 6-1: جمله n ام فیبوناچی (بازگشتی) int fib (int n) { if ( n <= 1) return n; else return fib (n – 1) + fib (n – 2); }

الگوریتم 7-1:جمله nام فیبوناچی (تکراری) int fib2 (int n) { index i; int f [0..n]; f[0] = 0; if (n > 0) { f[1] = 1; for (i = 2 ; I <= n; i++) f[i] = f [i -1] + f [i -2]; } return f[n];

جمله n ام فیبوناچی (بازگشتی)-تحلیل تعداد جملات محاسبه شده برای fib(n) بطور مثال برای محاسبه fib(5) تعداد 15 فراخوانی انجام می شود.(شکل درخت درج شود)

جمله n ام فیبوناچی (بازگشتی)-تحلیل 1 2 3 4 5 6 تعداد جملات محاسبه شده 9 15 25 اگر T(n) تعداد جملات در درخت بازگشتی باشد آنگاه T(n) >2 × T(n-2) T(n)> 2 ×2 ×T(n-4) ... T(n)> 2n/2 ×T(0)= 2n/2

مقایسه دو الگوریتم فیبوناچی اندازه آرایه n n+1 2n/2 زمان اجرا الگوریتم 7-1 زمان اجرا الگوریتم 6-1 40 41 1048576 41 ns 1048 ms 60 61 1.1×109 61 ns 1 s 80 81 1.1×1012 81 ns 18 min 100 101 1.1×1015 101 ns 13 day 120 121 1.2×1018 121 ns 36 year 160 161 1.2×1024 161 ns 3.8 × 107 year

3-1 تحلیل الگوریتم ها برای تعیین میزان کارایی یک الگوریتم را باید تحلیل کرد. 1-3-1 تحلیل پیچیدگی زمانی تحلیل پیچیدگی زمانی یک الگوریتم ، تعیین تعداد دفعاتی است که عمل اصلی به ازای هر مقدار از ورودی انجام می شود. اندازه ی ورودی: اندازه آرایه، تعداد سطرها، تعداد یال و نود و .... بسته به مسئله

A(n) را پیچیدگی زمانی در حالت میانگین می گویند. T(n) را پیچیدگی زمانی الگوریتم در حالت معمول می گویند. W(n) را تحلیل پیچیدگی زمانی در بدترین حالت می نامند. A(n) را پیچیدگی زمانی در حالت میانگین می گویند.

T(n) = n عمل اصلی: افزودن یک عنصر از آرایه به sum. تحلیل پیچیدگی زمانی برای حالت معمول برای الگوریتم(جمع کردن عناصرآرایه) عمل اصلی: افزودن یک عنصر از آرایه به sum. اندازه ورودی: n، تعداد عناصر آرایه. T(n) = n

تحلیل پیچیدگی زمانی برای حالت معمول برای الگوریتم(مرتب سازی تعویضی) عمل اصلی: مقایسه S [j] با S[i] . اندازه ورودی: تعداد عناصری که باید مرتب شوند. T(n) = n(n -1) /2

W (n) = n عمل اصلی: مقایسه یک عنصر آرایه با x. تحلیل پیچیدگی زمانی دربدترین حالت برای الگوریتم(جست و جوی ترتیبی) عمل اصلی: مقایسه یک عنصر آرایه با x. اندازه ورودی: , n تعداد عناصر موجود در آرایه. W (n) = n

تحلیل پیچیدگی زمانی در بهترین حالت برای الگوریتم(جست وجوی ترتیبی) عمل اصلی: مقایسه یک عنصر آرایه با x. اندازه ورودی: , n تعداد عناصر آرایه. B (n) = 1

4-1مرتبه الگوریتم الگوریتم ها یی با پیچیدگی زمانی ازقبیل n و100n را الگوریتم های زمانی خطی می گویند. مجموعه کامل توابع پیچیدگی را که با توابع درجه دوم محض قابل دسته بندی باشند، n²) ( θ می گویند.

برخی از گروه های پیچیدگی متداول در زیر داده شده است: مجموعه ای ازتوابع پیچیدگی که با توابع درجه سوم محض قابل دسته بندی باشند، n³) ( θ نامیده می شوند. برخی از گروه های پیچیدگی متداول در زیر داده شده است: θ(lg n) < θ (n) < θ (n lg n) < θ (n²) < θ (n³) < θ (2 ⁿ)

واحد زمانی: 1 ns(شکل رشد)

2-4-1آشنایی بیشتر با مرتبه الگوریتم ها برای یک تابع پیچیدگی مفروض ƒ(n) ،O (ƒ (n)) ”O بزرگ“ مجموعه ای از توابع پیچیدگی g (n) است که برای آن ها یک ثابت حقیقی مثبت c و یک عدد صحیح غیر منفی N وجود دارد به قسمی که به ازای همه ی N =< n داریم: g (n) >= c × ƒ (n)

برای یک تابع پیچیدگی مفروض ƒ(n) ، (Ω (ƒ(n)مجموعه ای از توابع پیچیدگی g (n) است که برای آن ها یک عدد ثابت حقیقی مثبت c و یک عدد صحیح غیر منفی N وجود دارد به قسمی که به ازای همه ی N =< n داریم: g (n) =< c × ƒ (n)

θ (ƒ(n)) = O (ƒ(n)) ∩ Ω (ƒ(n)) یعنی θ(ƒ(n)) مجموعه ای از توابع پیچیدگی g (n) است که برای آن ها ثابت های حقیقی مثبت c وd و عدد صحیح غیر منفی N وجود دارد به قسمی که : c × ƒ (n) <= d × ƒ(n)

برای یک تابع پیچیدگی ƒ(n) مفروض،( o(ƒ(n) ”o کوچک” عبارت ازمجموعه کلیه توابع پیچیدگیg (n) است که این شرط را برآورده می سازند : به ازای هرثابت حقیقی مثبت c ،یک عدد صحیح غیر منفی N وجود دارد به قسمی که به ازای همه ی N =< n داریم: g (n) =< c × ƒ (n)

نمایشی از Ω، O و θ

مثال در شکل زیر نشان می دهد که n2+10n از مرتبه O(n2) می باشد

مثال چند عضو از مجموعه های On2 Ωn2 θn2

ویژگی های مرتبه 1- O (ƒ(n)) Є g (n) اگروفقط اگر.ƒ (n) Є Ω (g(n)) 2- (ƒ(n)) θ Є g (n) اگروفقط اگرƒ (n) Є θ (g (n)). 3- اگر b >1 و a > 1، در آن صورت: log ⁿa Є θ (log ⁿb) 4- اگر b > a > 0 ،در آن صورت: aⁿ Є o (bⁿ)

c × g(n) + d × h (n) Є θ (ƒ(n)) 5- به ازای همه ی مقادیر a > 0 داریم : aⁿ Є o (n!) 6- اگرc >= 0، d >0 ، g (n) Є o (ƒ(n)) و h(n) Є θ(ƒ(n)) باشد، درآن صورت: c × g(n) + d × h (n) Є θ (ƒ(n))

7- ترتیب دسته های پیچیدگی زیر را در نظربگیرید: θ (lg n) θ (n) θ(n lg n) θ(n²) θ(n^j) θ (n^k) θ (aⁿ) θ (bⁿ) θ (n!) که در آن k > j > 2 و b > a > 1 است. اگر تابع پیچیدگی g (n) در دسته ای واقع در طرف چپ دسته ی حاوی ƒ (n) باشد، در آن صورت: g (n) Є o (ƒ(n))

استفاده از حد برای تعیین مرتبه

تمرینات سوال 12، 17، 19، 26، 27، 28، 29، 32