CS140-lecture-20211117

Some more dynamic programming examples #

Segmented least squares #

image_2021-11-17-13-12-24 image_2021-11-17-13-12-28

  • if we connected all points between each other, there would be zero error
  • we want a trade off between accuracy and number of segments

image_2021-11-17-13-13-13 image_2021-11-17-13-14-56

  • imagine connecting the last 2 points, and then attaching it to the rest of the optimal solution
  • or connecting the last 3 points and attaching it to the rest of the optimal solutions
  • and so on…

image_2021-11-17-13-17-48

Weighted activity selection #

image_2021-11-17-13-21-36 image_2021-11-17-13-23-22 image_2021-11-17-13-24-58 image_2021-11-17-13-26-12 image_2021-11-17-13-26-37 image_2021-11-17-13-27-15 image_2021-11-17-13-28-47 image_2021-11-17-13-29-38

  • \( O(n \lg n) \) complexity (sort)

image_2021-11-17-13-34-16 image_2021-11-17-13-35-50 image_2021-11-17-13-36-07 image_2021-11-17-13-38-51

The knapsack problem revisited #

image_2021-11-17-13-41-37 image_2021-11-17-13-57-48 image_2021-11-17-13-59-29 image_2021-11-17-14-00-15 image_2021-11-17-14-03-07

Automated memoization #

image_2021-11-17-13-34-27

Flow network #

image_2021-11-17-16-04-13 image_2021-11-17-16-04-48 image_2021-11-17-16-05-53 image_2021-11-17-16-08-29

  • notation is “capacity / flow”

image_2021-11-17-16-10-22

Max flow #

image_2021-11-17-16-10-30 image_2021-11-17-16-41-01

Ford-Fulkerson method #

image_2021-11-17-16-42-53 image_2021-11-17-16-43-03

  • can increase flow by 5

image_2021-11-17-16-43-15

  • lower path is not an augmenting edge, flow cannot increase

image_2021-11-17-16-44-48 image_2021-11-17-16-45-59 image_2021-11-17-16-47-32 image_2021-11-17-16-48-44 image_2021-11-17-16-50-37

  • there is no augmenting path left (can’t find path from source to sink), so the max flow of the previous graph is the answer

image_2021-11-17-16-50-58 image_2021-11-17-16-52-55 image_2021-11-17-16-53-07 image_2021-11-17-16-54-02 image_2021-11-17-17-04-49 image_2021-11-17-17-05-18

  • the max flow of the network will be less than or equal to a cut

image_2021-11-17-17-06-19

  • the max flow is the min cut

image_2021-11-17-17-09-04 image_2021-11-17-17-09-13 image_2021-11-17-17-09-45 image_2021-11-17-17-12-42 image_2021-11-17-17-13-39

Edmonds-Karp algorithm #

image_2021-11-17-17-17-03

Multiple sources or sinks #

image_2021-11-17-17-18-23

Bipartite matching #

image_2021-11-17-17-19-33 image_2021-11-17-17-20-24 image_2021-11-17-17-21-01 image_2021-11-17-17-21-50