View
30
Download
0
Category
Preview:
DESCRIPTION
COP 3502: Computer Science I Spring 2004 – Day 9 – Algorithm Analysis – Part 3. Instructor : Mark Llewellyn markl@cs.ucf.edu CC1 211, 823-2790 http://www.cs.ucf.edu/courses/cop3502/spr04. School of Electrical Engineering and Computer Science University of Central Florida. - PowerPoint PPT Presentation
Citation preview
COP 3502: Computer Science I (Day 9) Page 1 © Mark Llewellyn
COP 3502: Computer Science ISpring 2004
– Day 9 – Algorithm Analysis – Part 3
School of Electrical Engineering and Computer ScienceUniversity of Central Florida
Instructor : Mark Llewellyn markl@cs.ucf.edu
CC1 211, 823-2790http://www.cs.ucf.edu/courses/cop3502/spr04
COP 3502: Computer Science I (Day 9) Page 2 © Mark Llewellyn
Dealing with Summations
• Most programs contain some sort of looping constructs which causes a repetition of the execution of some program instructions.
• When analyzing running time costs for programs with loops, we need to add up the costs for each time the loop is executed.
• This is an example of a summation. Summations are simply the sum for some function over a range of parameter values.
• Summations are typically written using the “Sigma” notation:
n
1i
)i(f
COP 3502: Computer Science I (Day 9) Page 3 © Mark Llewellyn
Dealing with Summations (cont.)
• The Sigma notation indicates that we are summing the value of f(i) over some range of integer values.
• The parameter to the expression and its initial value are indicated below the symbol.
– In this case the notation i = 1 indicates that the parameter (summation variable) is i and its initial value is 1.
• On top of the symbol is the expression which indicates the maximum value for the summation variable.
– In this case, this expression is simply n.
n
1i
)i(f
COP 3502: Computer Science I (Day 9) Page 4 © Mark Llewellyn
Dealing with Summations (cont.)
• Note that in many textbooks that the Sigma notation is often typeset inline as:
• Given a summation, it is often replaced with an equation that directly computes the value of the summation.
• Such an equation is known as a closed-form solution, and the process of replacing the summation with its closed-form solution is known as solving the summation.
)n(f)1n(f)2(f)1(f)i(fn
1i
n
1i)i(f
COP 3502: Computer Science I (Day 9) Page 5 © Mark Llewellyn
Common Summations and Their Closed-Form
Solution• Below are the some very common summations and their closed-form.
n
1i
n11
n
1i
n
0i 2
1nnn)1n(321ii3
6
nn3n2n941i4
232
n
1i
2
nlognn5nlog
1i
n
0i
1n12
nn
n
1ii 2
2n2
2
n
8
3
4
2
2
1
2
i6
COP 3502: Computer Science I (Day 9) Page 6 © Mark Llewellyn
Generating Closed-Form Solutions for
Summations• The question is, how do you generate the closed-form solution for a summation?
• The answer is: by solving the summation.
• The technique used to solve the summation varies slightly depending on the summation. There are a few general cases to consider:
– summations which begin at either 0 or 1 and have a constant upper limit.
– summations which begin at either 0 or 1 and have a variable upper limit. If the upper summation limit is a variable, then the summation can be expressed in a closed-form.
– summations which begin at a value other than 0 or 1 and have either a constant or variable upper limit.
• We’ll look at all these cases separately.
COP 3502: Computer Science I (Day 9) Page 7 © Mark Llewellyn
Constant Upper Limit to the Summation
• Examples: Given the following summations, evaluate the summations.
Note: we could have evaluated the summations using the closed-form solution of the summation. 7111111111
6
0i
6
0i
6111111116
1i
6
1i
7161n1n
0i
6nin
1i
COP 3502: Computer Science I (Day 9) Page 8 © Mark Llewellyn
Constant Upper Limit to the Summation
• Example: Given the following summation, evaluate the summation.
Note: we could have evaluated the summation using the closed-form solution of the summation.
422126543212i2i26
1i
6
1i
42636
2
6262
2
1nn2i2i2
2n
0i
n
0i
constants are not part of the summation and can be removed from it.
COP 3502: Computer Science I (Day 9) Page 9 © Mark Llewellyn
Variable Upper Limit to the Summation
• Example: Given the following summation, produce the closed-form solution.
Note: the following summation produces the same result!
Once the closed form is found, we can evaluate the summation for some given upper limit on the summation. In this example, if we assume that n = 5, then we have:
2
n5n5
2
1nn5i5i5
2n
1i
n
1i
2
n5n5
2
1nn5i5i5
2n
0i
n
0i
The first term in this summation adds nothing to the result since 50 = 0
75
2
150
2
25255
2
n5n5i5
2n
1i
COP 3502: Computer Science I (Day 9) Page 10 © Mark Llewellyn
Generating Closed-Forms for Complex
Functions• If the summation function involves additional terms, then the summation is broken-up into separate summations as shown in the example below:
• Examples:
n
1i
n
1i
22n
1i
n10n2n82
n4n4n8
2
1nn418i48i4
n
0i
n
0i
22n
0i
8n10n28n82
n4n41n8
2
1nn418i48i4
initial function (4i+8) issplit into two pieces.
COP 3502: Computer Science I (Day 9) Page 11 © Mark Llewellyn
Generating Closed-Forms for Complex
Functions• A couple more examples:
n
1i
n
1i
22n
1i 2
n7n3
2
n10
2
n3n3n5
2
1nn315i35i3
6
12nn21n8
6
12n12n9n9n4n12n8
2n22
n3n3
6
n4n12n81n2
2
nn3
6
nn3n24
12i3i42i3i4
23223
223223
n
0i
n
0i
n
0i
n
0i
22
COP 3502: Computer Science I (Day 9) Page 12 © Mark Llewellyn
Closed-form for Summations with Initial Value
> 1• There are two basic techniques that can be used to determine the closed-form
solution for summations in which the initial value of the summation is greater than 1.
1. Shift the range of the summation.
• In this technique you shift the summation range down until the starting value is equal to 1 and add into the function all of the values that are omitted because of the range shift.
2. Subtract the difference between two summations.
• In this technique you actually produce the summation over the entire range from 1 to n and then subtract out the value of the summation over the range from 1 to m-1.
• We’ll look at some examples using both techniques and then you can decide for yourself which technique you prefer.
n
mi
)i(f
COP 3502: Computer Science I (Day 9) Page 13 © Mark Llewellyn
Shifting the Range of the Summation
• The following example illustrates the range shifting technique for dealing with summations where the initial value of the summation range is greater than 1.
• Example:
• Notice that there are 7 terms in:
• the difference between those seven numbers and the summation
is that the numbers in (4+5+6+7+8+9+10) are each 3 larger than the numbers in (1+2+3+4+5+6+7) .
• Thus, (4+5+6+7+8+9+10) is equal to
(1+3)+(2+3)+(3+3)+(4+3)+(5+3)+(6+3)+(7+3)
• We can write (1+3)+(2+3)+(3+3)+(4+3)+(5+3)+(6+3)+(7+3) as
10987654i10
4i
7654321i7
1i
COP 3502: Computer Science I (Day 9) Page 14 © Mark Llewellyn
Shifting the Range of the Summation
)37()36()35()34()33()32()31(3i7
1i
493ii7
1i
10
4i
This means that:
Let’s check this to be certain:
4910987654i10
4i
4910987654
)37()36()35()34()33()32()31(3i7
1i
4921
2
5621
2
749)n(3
2
nn13i3i
7
1i
27
1i
7
1i
COP 3502: Computer Science I (Day 9) Page 15 © Mark Llewellyn
Subtracting Partial Summation from Total
Summation• The following example illustrates the subtraction technique for dealing with summations where the initial value of the summation range is greater than 1.
• Example:
• Notice that:
• is equal to: (1+2+3+4+5+6+7+8+9+10) – (1+2+3) = 55 – 6 = 49.
• Another way of stating this is:
• Let’s check to see if this is really correct.
4910987654i10
4i
10
1
3
1
10
4i
iii
496552
12
2
110
2
33
2
1010iii
2210
1
3
1
10
4i
COP 3502: Computer Science I (Day 9) Page 16 © Mark Llewellyn
Another Example – Range Shifting Technique
• The following example illustrates range shifting technique for dealing with summations where the initial value of the summation range is greater than 1.
• Example: Find the value of the summation
To convert into a summation which starts at 1, shift the index range down by 22.
This means that we have to add 22 to each i term in the summation. But, we don't add 22 to the 5's because the shifted summation will still have the same number of 5's (45 of them). Shifting the range we have:
Solving this summation we have:
67
23i
5i3
67
23i
5i3
45
1i
45
1i
45
1i
67
23i
71i3566i3522i35i3
63003195
2
62104571
2
45453171i371i3
45
1i
45
1i
245
1i
COP 3502: Computer Science I (Day 9) Page 17 © Mark Llewellyn
Another Example – Summation Subtraction
Technique• The following example illustrates the subtraction technique for dealing with summations where the initial value of the summation range is greater than 1.
• Example: Find the value of the summation
To convert into a summation which starts at 1, and subtract
Solving this expression we have:
67
23i
5i3
67
23i
5i3
22
1i
22
1i
22
1i
67
1i
67
1i
67
1i
67
23i
15i315i35i35i35i3
22
1i
5i3
63001107593356834)22(523113)67(534673
)22(52
)23(223675
2
6867315i315i3
22
1i
22
1i
67
1i
67
1i
COP 3502: Computer Science I (Day 9) Page 18 © Mark Llewellyn
Permutations
• A permutation of a set of n objects is simply an ordered arrangement of those objects.
• A common example of permutations deals with words. How many “words” can you form using the letters A, B, C, and D, without repeating any letters.
• Essentially, the question boils down to how many ways you can order a set of objects.
• In this case we know that we have four choices for the first letter. Once that choice is made we have three choices for the second letter, and so on.
– In particular we have: 4 3 2 1 = 24 possible permutations
– ABCD, ABDC, ACBD, ACDB, ADBC, ADCB
– BACD, BADC, BCDA, BCAD, BDAC, BDCA
– CABD, CADB, CBAD, CBDA, CDAB, CDBA
– DABC, DACB, DBAC, DBCA, DCAB, DCBA
COP 3502: Computer Science I (Day 9) Page 19 © Mark Llewellyn
Permutations (cont.)
• In general, the number of permutations of n objects is:
– n (n-1) (n-2) 2 1 = n! permutations
• Now consider the following problem: Given n distinct objects, how many permutation are there of r of those objects, where 1 r n?
– n (n-1) (n-2) (n-r+1) = n!/(n-r)! permutations
COP 3502: Computer Science I (Day 9) Page 20 © Mark Llewellyn
Combinations
• Notice that when dealing with permutations, the order of the chosen objects matters.
• If the order of the objects does matter, then we are dealing not with a permutation but rather a combination of the objects.
• A combination of r objects from a set of n objects is a selection of r objects without regard to order.
– For example, if we have five objects: {a, b, c, d, e}, we can choose three of these objects in 10 different ways if order is not taken into account:
abc abd abe acd ace ade bcd bce bde cde
• In general:
!rn!r
!n
r
n
COP 3502: Computer Science I (Day 9) Page 21 © Mark Llewellyn
Combinations (cont.)
• The values are called the binomial coefficients because of the role they play in what is commonly referred to as Newton’s Theorem.
• Newton’s Theorem
• Note: if x = 1 we have:
• A table displaying the values of is often called Pascal’s triangle an is shown on the next page.
r
n
n1n2n xx1n
nx
2
nx
1
n1x1
r
n
nn
0r
2r
n
COP 3502: Computer Science I (Day 9) Page 22 © Mark Llewellyn
Pascal’s Triangle
n/r 0 1 2 3 4 5 6 7 8 9
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1
35144
5040
!3!4
5040
!47!4
!7
4
7
Recommended