CPSC 320 Intermediate Algorithm Design and Analysis July

  • Slides: 21
Download presentation
CPSC 320: Intermediate Algorithm Design and Analysis July 21, 2014 1

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

Course Outline • • • Introduction and basic concepts Asymptotic notation Greedy algorithms Graph

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 2

Master Theorem 3

Master Theorem 3

Master Theorem • 4

Master Theorem • 4

In-class Exercises • 5

In-class Exercises • 5

Problem • 6

Problem • 6

Divide-and-Conquer Algorithms 7

Divide-and-Conquer Algorithms 7

Binary to Decimal conversion • 8

Binary to Decimal conversion • 8

Binary to Decimal Analysis • 9

Binary to Decimal Analysis • 9

Faster Algorithm • Approach: divide and conquer • Divide the array in two parts,

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 10

Faster Algorithm • 11

Faster Algorithm • 11

Faster Algorithm • 12

Faster Algorithm • 12

Exponential • 13

Exponential • 13

Divide and Conquer • 14

Divide and Conquer • 14

Time Complexity • 15

Time Complexity • 15

Stock trading problem • 16

Stock trading problem • 16

Maximum Subarray • Let’s consider the daily changes (difference from previous day): • 13,

Maximum Subarray • Let’s consider the daily changes (difference from previous day): • 13, -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? 17

Divide and Conquer • If we divide the array in two, a maximum subarray

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 18

Algorithm • 19

Algorithm • 19

Algorithm • 20

Algorithm • 20

Time Complexity • 21

Time Complexity • 21