0% found this document useful (0 votes)
5 views

L2 (3)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

L2 (3)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

‫طراحی الگوریتمها‬

‫فصل دوم‬

‫‪[email protected]‬‬ ‫صمد نجارقابل‬


‫‪2‬‬

‫فهرست مطالب‬
‫‪‬مقدمهای بر الگوریتمها و مفاهیم پایه‬
‫‪‬معرفی پیچیدگی زمانی و حافظهای و روشهای تحلیل مسائل‬
‫‪ ‬روش های حل مسئله‬
‫‪‬تقسیم و غلبه (حل)‬
‫‪‬برنامه نویسی پویا‬
‫‪‬روش حریصانه‬
‫‪‬راهبرد عقبگرد‬
‫‪‬شاخه و حد‬
‫‪‬آشنایی نظریه ‪ NP‬و مسائل مربوط به آن‬
‫‪3‬‬

‫معرفی پیچیدگی زمانی و حافظهای و روشهای تحلیل مسائل‬


4

Analysis of Algorithms
• Different input requires different amount of resources. Instead of
dealing with input itself, we prefer to deal with the Size of Input.

• Examples for Size of Input :


▫ For an array: The size can be considered as the number of its elements...
▫ For an integer: The size can be considered as the number of its bit in
binary representation...
▫ For a graph: The size can be considered as the number of vertices or
edges...
5

Analysis of Algorithms (Cont.)


• Definition Analysis: Predicting the amount of resources that the algorithm requires.
• Measure the required time (time complexity) and memory (space complexity).
▫ Best case: Measuring the required amount of resources for easy instances.
▫ Worse case: Measuring the required amount of resources for hard instances.
▫ Average case: Measuring the required amount of resources for all instances
divided by the number of instances.
• Should be independent from the current technology (Hardware, Programming
Languages, etc.).
• Should be independent from the way of implementing the algorithm.
• By analyzing several candidate algorithms for a specific problem, the most
efficient one can be easily identified.
6

Analysis of Algorithms (Cont.)


• There are infinitely many functions. In order to compare them with each other, we
prefer to classify them as simple functions without loss of their properties. In fact
we want to choose one simple function for each class of functions.
7

Practical Complexity

250

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8

Practical Complexity Cont.


500

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
9

Practical Complexity Cont.

1000

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 3 5 7 9 11 13 15 17 19
10

Practical Complexity Cont.

5000

4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000 f(n) = n^3
f(n) = 2^n

1000

0
1 3 5 7 9 11 13 15 17 19
11

Notation
12

Notation (Cont.)
13

Notation (Cont.)
14

Notation (Cont.)
15

Notation (Cont.)
• Examples
▫ 3n+2=O(n) /* 3n+24n for n2 */
▫ 3n+3=O(n) /* 3n+34n for n3 */
▫ 100n+6=O(n) /* 100n+6101n for n10 */
▫ 10n2+4n+2=O(n2) /* 10n2+4n+211n2 for n5 */
▫ 6*2n+n2=O(2n) /* 6*2n+n2 7*2n for n4 */
16

Notation (Cont.)
• O(1): constant
• O(n): linear
• O(n2): quadratic
• O(n3): cubic
• O(2n): exponential
• O(logn): logarithmic
• O(nlogn): log linear
17

Notation (Cont.)
‫‪18‬‬

‫روشهای حل مسئله های بازگشتی‬


‫استقرا‬ ‫•‬
‫با استفاده از معادله مشخصه‬ ‫•‬
‫حل بازگشت با جایگزینی‬ ‫•‬
‫قضیه اصلی‬ ‫•‬
‫و‪...‬‬ ‫•‬
19

Homework …

You might also like