1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.

Post on 13-Jan-2016

216 views 0 download

Tags:

Transcript of 1 CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014.

1

CPSC 320: Intermediate AlgorithmDesign and Analysis

July 21, 2014

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

3

Master Theorem

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 .

5

In-class Exercises

Solve using recursion trees and the master theorem, and prove using substitution

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:

7

Divide-and-Conquer Algorithms

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

9

Binary to Decimal Analysis

• Using recursion trees we can calculate that this is

• Can we do better?

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

11

Faster Algorithm

Algorithm BinaryToDecimalFast(A)

While Do

Multiply()

Return BinaryToDecimalHelper()

12

Faster Algorithm

Algorithm BinaryToDecimalHelper()

If Then

Return String()

FirstHalf Divide()

SecondHalf Modulo()

Return BinaryToDecimalHelper(FirstHalf, , ) .

BinaryToDecimalHelper(SecondHalf, , )

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?

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(,)

15

Time Complexity

• The new algorithm runs in

• From master theorem, we get

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?

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?

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

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)

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)

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