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

L2_New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

L2_New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

‫ساختمان دادهها و الگوریتمها‬

‫فصل دوم‬

‫‪[email protected]‬‬ ‫‪۱۳۹۴‬‬ ‫صمد نجار‬


‫‪2‬‬

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

‫سوال‬

‫• اگر دو الگوریتم داشته باشیم که یک هدف را انجام دهند‪ ،‬چگونه باید فهمید که کدام بهتر‬
‫عمل میکنند؟‬

‫• معیار ارزیابی چه پارامترهایی میتواند باشد؟‬


4

Space Complexity
S(P)=C+SP(I)
• Fixed Space Requirements (C)
Independent of the characteristics of the inputs and
outputs
▫ instruction space
▫ space for simple variables, fixed-size structured variable,
constants
• Variable Space Requirements (SP(I))
depend on the instance characteristic I
▫ number, size, values of inputs and outputs associated with I
▫ recursive stack space, formal parameters, local variables, return
address
5

Example
Program 1.9: Simple arithmetic function
Sabc(I) = 0
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}

Program 1.10: Iterative function for summing a list of numbers


float sum(float list[ ], int n)
{
float tempsum = 0; Ssum(I) = 0
int i;
for (i = 0; i<n; i++)
tempsum += list [i];
return tempsum;
}
6

Example (Cont.)
*Program 1.11: Recursive function for summing a list of numbers (p.20)
float rsum(float list[ ], int n)
{
if (n > 0) return rsum(list, n-1) + list[n-1];
return 0;
} Srsum(I) = Srsum(n)= 6n

Space needed for one recursive call of Program 1.11


7

Time Complexity
T(P)=C+TP(I)
• Compile time (C)
independent of instance characteristics
• run (execution) time TP
• Definition
A program step is a syntactically or semantically
meaningful program segment whose execution
time is independent of the instance
characteristics.
• Example
▫ abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
▫ abc = a + b + c
8

Methods to compute the step count

• Introduce variable count into programs


• Tabular method
▫ Determine the total number of steps contributed by each statement
step per execution  frequency
▫ add up the contribution of all statements
9

Variable Count Method (Example)


*Program 1.12: Program 1.10 with count statements
2n + 3 Steps
float sum(float list[ ], int n)
{
float tempsum = 0; count++; /* for assignment */
int i;
for (i = 0; i < n; i++) {
count++; /*for the for loop */
tempsum += list[i]; count++; /* for assignment */
}
count++; /* last execution of for */
count++; /* for return */
return tempsum;
}
10

Variable Count Method (Example Cont.)

*Program 1.13: Simplified version of Program 1.12

float sum(float list[ ], int n)


{
float tempsum = 0; 2n + 3 Steps
int i;
for (i = 0; i < n; i++)
count += 2;
count += 3;
return 0;
}
11

Variable Count Method (Example Cont.)

*Program 1.14: Program 1.11 with count statements added

float rsum(float list[ ], int n)


{
2n + 2 Steps
count++; /*for if conditional */
if (n > 0) {
count++; /* for return and rsum invocation */
return rsum(list, n-1) + list[n-1];
}
count++;
return 0;
}
12

Variable Count Method (Example Cont.)

*Program 1.15: Matrix addition

void add( int a[ ] [MAX_SIZE], int b[ ] [MAX_SIZE],


int c [ ] [MAX_SIZE], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
for (j= 0; j < cols; j++)
c[i][j] = a[i][j] +b[i][j];
}
13

Variable Count Method (Example Cont.)


*Program 1.16: Matrix addition with count statements (p.25)

void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE],


int c[ ][MAX_SIZE], int row, int cols )
{
int i, j;
for (i = 0; i < rows; i++){
count++; /* for i for loop */ 2rows * cols + 2 rows +1
for (j = 0; j < cols; j++) {
count++; /* for j for loop */
c[i][j] = a[i][j] + b[i][j];
count++; /* for assignment statement */
}
count++; /* last time of j for loop */
}
count++; /* last time of i for loop */
}
14

Variable Count Method (Example Cont.)


*Program 1.17: Simplification of Program 1.16

void add(int a[ ][MAX_SIZE], int b [ ][MAX_SIZE],


int c[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for( i = 0; i < rows; i++) 2rows * cols + 2 rows + 1
{
for (j = 0; j < cols; j++)
count += 2;
count += 2;
}
count++; Suggestion: Interchange the
} loops when rows >> cols
15

Tabular Method (Example)


steps/execution
Iterative function to sum a list of numbers
16

Tabular Method (Example Cont.)


Step count table for recursive summing function
17

Tabular Method (Example Cont.)

Step count table for matrix addition


18

Home Work 1
19

Trade off between time and space complexity


20

Asymptotic Notation

• Determining step counts help us to compare the time complexities of two


programs and to predict the growth in run time as the instance characteristics
change.
• But determining exact step counts could be very difficult. Since the notion of
a step count is itself inexact, it may be worth the effort to compute the exact
step counts.

• How does the algorithm behave as the problem size gets very large?
21
22
23
24
25

Asymptotic Notation (O)

• Definition(Big oh)
• f(n) = O(g(n)) iff there exist positive constants c and n0 such that f(n)
 cg(n) for all n, n  n0.
• 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 */
26

Asymptotic Notation Cont. (O)

• O(1): constant
• O(n): linear
• O(n2): quadratic
• O(n3): cubic
• O(2n): exponential
• O(logn) logarithmic
• O(nlogn) log linear
27

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
28

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
29

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
30

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
31

Asymptotic Notation (Cont.)


32

Asymptotic Notation (Cont.)


33
34
35
36
38
39
40
41
42
43

Asymptotic Notation (Cont.)

• Definition: [Omega] f(n) = Ω(g(n)) iff there exist positive constants c


and n0 such that f(n) ≥ cg(n) for all n, n ≥ n0.
• Example:
▫ 3n + 2 = Ω(n)
▫ 100n + 6 = Ω(n)
▫ 10n2 + 4n + 2 =Ω(n2)
44

Asymptotic Notation (Cont.)

Definition: f(n) = Θ(g(n)) iff there exist positive constants c1, c2, and
n0 such that c1g(n) ≤ f(n) ≤ c2g(n) for all n, n ≥ n0.
45

Asymptotic Notation (Cont.)

Theorem 1.2: If f(n) = amnm + … + a1n + a0, then f(n) =


O(nm).
Proof: m
f ( n)  | a
i 0
i | ni
m
n m
| a i | nim
0
for n ≥ 1
m
 n m  | ai |
0

So, f(n) = O(nm)


46

Asymptotic Notation (Cont.)

Theorem 1.3: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) =


Ω(nm).

Theorem 1.4: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) =


Θ(nm).
47
48
49
50
51
52
53
54
55
56
57
58

Home Work 2

You might also like