CSE 40872 Lecture 017

Topic:Dynamic Programming (Subarray, DNA)
Author: Peter Bui <pbui@cse.nd.edu>
Date: October 14, 2009

Dynamic Programming

Two main characteristics:

  1. Overlapping Subproblems: a problem can be broken down into smaller subproblems that are reused multiple times.
  2. Optimal Substructure: there is a way of constructing the problem such that each subproblem contains the optimal solution, up to that point.

Maximal Subarray

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]

Subarray source code

Maximal Subarray (Brute Force)

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);
}

Maximal Subarray (Dynamic)

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);
}

Common Characteristics

  1. Problem can be divided into stages with a decision at each stage.
  2. Each stage has a number of states associated with it.
  3. Given the current state, the optimal decision for each of the remaining states does not depend on the previous states or decisions.

Common Characteristics

  1. There exists a recursive relationship that identifies the optimal decision for stage j, given that state j+1 has already been solved.
  2. The final stage must be solvable by itself.

DNA Alignment

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

DNA Implementation

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

DNA source code

Summary

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.