CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.
-
Upload
bernice-parks -
Category
Documents
-
view
214 -
download
0
Transcript of 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.
![Page 1: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/1.jpg)
1
CPSC 320: Intermediate AlgorithmDesign and Analysis
July 21, 2014
![Page 2: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/2.jpg)
2
Course Outline
• Introduction and basic concepts
• Asymptotic notation
• Greedy algorithms
• Graph theory
• Amortized analysis
• Recursion
• Divide-and-conquer algorithms
• Randomized algorithms
• Dynamic programming algorithms
• NP-completeness
![Page 3: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/3.jpg)
3
Master Theorem
![Page 4: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/4.jpg)
4
Master Theorem
• Let be real constants, , and defined by:
• where could also be or . Then:
1. If for some , then .
2. If for some , then .
3. If for some , and for some and all large enough, then .
![Page 5: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/5.jpg)
5
In-class Exercises
Solve using recursion trees and the master theorem, and prove using substitution
![Page 6: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/6.jpg)
6
Problem
Suppose the following recurring relation:
The relation above is hard to evaluate, but let’s try (so )
If we assume that :
By the master theorem, this results in . So:
![Page 7: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/7.jpg)
7
Divide-and-Conquer Algorithms
![Page 8: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/8.jpg)
8
Binary to Decimal conversion
• Problem: given an n-bit binary integer, calculate its decimal representation
• Naïve Approach (given as an array of bits):
Algorithm BinaryToDecimal1()
If Then
Return “”
Else
Last Modulo(, 10)
Rest Divide(, 10)
Return BinaryToDecimal1(Rest) . Last
![Page 9: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/9.jpg)
9
Binary to Decimal Analysis
• Using recursion trees we can calculate that this is
• Can we do better?
![Page 10: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/10.jpg)
10
Faster Algorithm
• Approach: divide and conquer
• Divide the array in two parts, do each part separately
• Divide using an appropriate power of 10
• Choose power of 10 that uses at least half of the bits
![Page 11: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/11.jpg)
11
Faster Algorithm
Algorithm BinaryToDecimalFast(A)
While Do
Multiply()
Return BinaryToDecimalHelper()
![Page 12: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/12.jpg)
12
Faster Algorithm
Algorithm BinaryToDecimalHelper()
If Then
Return String()
FirstHalf Divide()
SecondHalf Modulo()
Return BinaryToDecimalHelper(FirstHalf, , ) .
BinaryToDecimalHelper(SecondHalf, , )
![Page 13: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/13.jpg)
13
Exponential
• Problem: given two numbers and , calculate
• Simple solution:
Function Exponential(, )
If Then
Return 1
Else
Return Exponential(,)
• This algorithm runs in
• Can we do better?
![Page 14: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/14.jpg)
14
Divide and Conquer
• Strategy: divide the problem in smaller problems, then solve smaller problems and merge the results
• Exponential: we know that , and
Function Exponential2(, )
If Then
Return 1
Else If Is Odd Then
Return * Exponential(,)
Else
Return Exponential(,)
![Page 15: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/15.jpg)
15
Time Complexity
• The new algorithm runs in
• From master theorem, we get
![Page 16: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/16.jpg)
16
Stock trading problem
• Assume a list of trading stock prices in the future:
• 100, 113, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97
• You want to find the maximum profit you can make by buying a stock on one day and selling on another
• Example: buy at 63, sell at 106, profit is 43.
• Greedy algorithms won’t always work
• Lowest or highest prices might not be optimal options
• Brute-force (check every possible pair) runs in
• Can we do better?
![Page 17: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/17.jpg)
17
Maximum Subarray
• Let’s consider the daily changes (difference from previous day):
• 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7
• Now the problem becomes: finding the range with largest sum
• In the example: 18, 20, -7, 12; sum is 43
• How does that help?
![Page 18: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/18.jpg)
18
Divide and Conquer
• If we divide the array in two, a maximum subarray may be in:
• The first half
• The second half
• In between (crossing division)
• We can calculate the best option for each, then select the largest
• First and second half can be solved by recursion
• Crossing subarray can be found in linear time
• Find largest sum that ends at midpoint
• Find largest sum that starts just after midpoint
![Page 19: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/19.jpg)
19
Algorithm
Algorithm FindMaxCrossingSubarray(, , , )
LeftSum
Sum
For DownTo Do
Sum Sum
If Sum > LeftSum Then
LeftSum Sum
MaxLeft
RightSum
Sum
For To Do
Sum Sum
If Sum > RightSum Then
RightSum Sum
MaxRight
Return (MaxLeft, MaxRight, LeftSum+RightSum)
![Page 20: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/20.jpg)
20
Algorithm
Algorithm FindMaxSubarray(, , )
If Then
Return (, , )
(LS, LE, LSum) FindMaxSubarray(, , )
(RS, RE, RSum) FindMaxSubarray(, , )
(CS, CE, CSum) FindMaxCrossingSubarray(, , , )
If LSum > RSum And LSum > CSum Then Return (LS, LE, LSum)
If RSum > LSum And RSum > CSum Then Return (RS, LE, RSum)
Return (CS, CE, CSum)
![Page 21: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f0d5503460f94c210ed/html5/thumbnails/21.jpg)
21
Time Complexity
• Since finding the maximum crossing subarray is and finding the largest among three values is , we have
• From the master theorem, we get that this approach is