Download presentation
Presentation is loading. Please wait.
Published byScarlett Mason Modified over 8 years ago
1
D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING (C ASE S TUDIES ) Informatics Department Parahyangan Catholic University
2
M OVING ON A B OARD Example: Unidirectional TSP (uva.onlinejudge.org, problem number 116) Given a grid full of numbers. Find a path from the left most column to the right most column with the least cost. Allowed move is “right”, “top-right”, and “bottom-right” only.
3
M OVING ON A B OARD Variations: Find the largest cost Can/Cannot move past the top/bottom wall Different board shapes
4
M OVING ON A B OARD Recursive solution : What is the smallest cost to reach this cell ?
5
M OVING ON A B OARD Recursive solution : There are 3 ways to reach the green cell: from A, B, or C. Surely the smallest cost is the minimum cost among those three. A B C What is the minimum cost to reach cell A, B, and C ? This can be computed recursively.
6
M OVING ON A B OARD Recursive solution : What is the base case ? smallest cost to reach cells on the first column is the cell’s value itself
7
M OVING ON A B OARD Bottom-up solution : Calculate the cost for each cell starting from the cells on the first column, then second column, and so on.
8
M AXIMUM R ANGE S UM (1D) Given a sequence of numbers (positive and negative), find a range with maximum sum. Example: -1, 3, -1, 4, -2, -3, 7, 7, -4, 2, 1 Answer: -1, 3, -1, 4, -2, -3, 7, 7, -4, 2, 1
9
M AXIMUM R ANGE S UM (1D) Brute Force solution : Find the sum of every possible pairs of start-index and end-index. There are O(N 2 ) pairs, each requires O(N) to calculate the sum, so in total this algorithm runs in O(N 3 ) time.
10
M AXIMUM R ANGE S UM (1D) Dynamic Programming solution : Consider this range, sum of all elements in A is equals to sum of all elements in B + C In other words: A = B + C C = A - B A BC =
11
M AXIMUM R ANGE S UM (1D) Dynamic Programming solution : A prefix sum of a sequence of numbers x 0, x 1, x 2,... is a second sequence of numbers y 0, y 1, y 2,..., the sums of prefixes (running totals) of the input sequence: y 0 = x 0 y 1 = x 0 + x 1 y 2 = x 0 + x 1 + x 2... Example: 1325423 14611151720
12
M AXIMUM R ANGE S UM (1D) Dynamic Programming solution : Observe that A and B are part of prefix sum of the original sequence, so we can compute C in O(1) time if we already have the prefix sum A BC =
13
M AXIMUM R ANGE S UM (1D) Dynamic Programming solution : STEP 1 : compute the prefix sum O(N) STEP 2 : find the sum of every possible pairs of start-index and end-index. O(N 2 ) pairs x O(1) time to compute each sum How to compute prefix sum in O(N) time ? 1325423 14611 original prefix sum
14
M AXIMUM R ANGE S UM (1D) Greedy solution : Observe that for any range A which has a negative prefix B, C is always greater than A A BC = negative
15
M AXIMUM R ANGE S UM (1D) Greedy solution : The same applies for any range A which has a negative suffix B, C must be greater than A A BC = negative
16
M AXIMUM R ANGE S UM (1D) Greedy solution : A greedy solution for Maximum Range Sum (1D) sweeps through the original sequence once, keeping track the current sum and starting point Example: A2-3 startend Any range which has -3 as its prefix must be not the maximum 4375 2 Maximum 4 7 Any range which has -1 as its prefix must be not the maximum 4 And so on…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.