Topic: | Dynamic Programming (Subarray, DNA) |
---|---|
Author: | Peter Bui <pbui@cse.nd.edu> |
Date: | October 14, 2009 |
Two main characteristics:
Given an array, describe an algorithm to identify the subarray with the maximum sum:
Input:
[1, -3, 5, -2, 9, -8, -6, 4]
Output:
[5, -2, 9]
static int
find_maximal_subarray_brute_force(int a[], int n, int *s, int*e) {
int i, j, cur_sum, max_sum = INT_MIN;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cur_sum = compute_sum(a, i, j);
if (cur_sum > max_sum) {
max_sum = cur_sum;
*s = i;
*e = j;
}
}
}
return (max_sum);
}
static int
find_maximal_subarray_dynamic(int a[], int n, int *s, int*e) {
int i, j = 0, cur_sum = 0, max_sum = INT_MIN;
*s = *e = 0;
for (i = 0; i < n; i++) {
cur_sum = cur_sum + a[i];
if (cur_sum > max_sum) {
max_sum = cur_sum; *s = j; *e = i;
}
if (cur_sum < 0) {
cur_sum = 0; j = i + 1;
}
}
return (max_sum);
}
Global sequence alignment using Needleman/Wunsch technique.
Input:
G A A T T C A G T T A G G A T C G A
Output:
G A A T T C A G T T A | | | | | | G _ A _ T C _ G _ _ A
Table keeps track the number of alignments/matches:
G A A T T C A G T T A G G M(i, j) = Max( A M(i - 1, j - 1) + S(i, j), T M(i, j - 1) + w C M(i - 1, j) + w G A S(i, j) = D[i] == D[j] w = 0
Dynamic programming can be used to efficient solve certain types of search or optimization problems.
Requires identifying overlapping subproblems and optimal substructure.
Once you know the recursive relationships, it is generally easy to form the table...
Table may have multiple solutions to the global optimal.