Docsity

LCS & Greedy Algorithms - Lecture 14 by Jared Saia, Univ. of New Mexico (90 characters) - , Study notes of Computer Science

Partial preview of the text

Download LCS & Greedy Algorithms - Lecture 14 by Jared Saia, Univ. of New Mexico (90 characters) - and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 14

Jared Saia University of New Mexico

Today’s Outline

  • Longest Common Subsequence
  • Intro to Greedy Algs

Subsequence Definition

  • Assume given sequence X = 〈x 1 , x 2 ,... , xm〉
  • Let Z = 〈z 1 , z 2 ,... , zl〉
  • Then Z is a subsequence of X if there exists a strictly in- creasing sequence 〈i 1 , i 2 ,... , ik〉 of indices such that for all j = 1, 2 ,... , k, xij = zj

Example

  • Let X = 〈A, B, C, B, A, B, D, C〉,
  • Z = 〈A, C, A, B, C〉
  • Then, Z is a subsequence of X

Common Subsequence

  • Given two sequences X and Y , we say that Z is a common subsequence of X and Y if Z is a subsequence of X and Z is a subsequence of Y
  • Example: X = 〈A, B, D, C, B, A, B, C〉, Y = 〈A, D, B, C, D, B, A, B〉
  • Then Z = 〈A, B, B, A, B〉 is a common subsequence
  • Z is not a longest common subsequence(LCS) of X and Y though since the common subsequence Z′^ = 〈A, B, C, B, A, B〉 is longer
  • Q: Is Z′^ a longest common subsequence?

LCS Problem

  • We are given two sequences X = 〈x 1 , x 2 ,... , xm〉 and Y = 〈y 1 , y 2 ,... , yn〉
  • Goal: Find a maximum-length common subsequence of X and Y

Brute Force

  • Brute Force approach is to enumerate all possible subse- quences of X, check to see if its a subsequence of Y , and then keep track of the longest common subsequence of both X and Y
  • This is slow.
  • Q: How many subsequences of X are there?

Terminology

  • Given a sequence X = 〈x 1 , x 2 ,... xm〉, for i = 0, 1 ,... , m, let Xi be the i-th prefix of X i.e. Xi = 〈x 1 , x 2 ,... , xi〉
  • Example: if X = 〈A, B, D, C〉, X 0 = 〈〉 and X 3 = 〈A, B, D〉

DP Solution

  • This is already enough to write up a recursive function, how- ever the naive recursive function will take exponential time
  • Instead, we can use dynamic programming and solve from the bottom up
  • Code for doing this is on p. 353 and 355 of the text, basically it uses the same ideas we’ve seen before of filling in entries in a table from the bottom up.

Example

  • Consider X = 〈A, B, D, C, B, A, B, C〉, Y = 〈A, D, B, C, D, B, A, B〉
  • The next slide gives the table constructed by the DP algo- rithm for computing the LCS of X and Y
  • The bold numbers represent one possible path giving a LCS.
  • The arrows keep track of where the minimum is obtained from

Example

A B D C B A B C

A 0 1 → 1 → 1 → 1 → 1 → 1 → 1 → 1

D 0 1 1 2 → 2 → 2 → 2 → 2 → 2

B 0 1 2 → 2 → 2 3 → 3 → 3 → 3

C 0 1 2 → 2 3 → 3 → 3 → 3 → 4

D 0 1 2 3 → 3 → 3 → 3 → 3 4

B 0 1 2 3 → 3 4 → 4 → 4 → 4

A 0 1 2 3 → 3 4 5 → 5 → 5

B 0 1 2 3 → 3 4 5 6 → 6

Reconstruction

  • To find a reconstruction, first find a path of edges leading from the bottom right corner to the top left corner
  • In this path, the target of each diagonal arrow gives a char- acter to include in the LCS
  • In our example, 〈A, B, C, B, A, B〉 is the LCS we get by fol- lowing the edges along the only path from the bottom right to the top left

Take Away

  • We’ve seen four different DP type algorithms
  • In each case, we did the following 1) found a recurrence for the solution 2) built solutions to the recurrence from the bottom up
  • You should be prepared to do this on your own now!

Greedy Algorithms

“Greed is Good” - Michael Douglas- in Wall Street

  • A greedy algorithm always makes the choice that looks best at the moment
  • Greedy algorithms do not always lead to optimal solutions, but for many problems they do
  • In the next week or two, we will see several problems for which greedy algorithms produce optimal solutions including: activity selection, fractional knapsack, and making change
  • When we study graph theory, we will also see that greedy algorithms work well for computing shortest paths and finding minimum spanning trees.

Activity Selection

  • You are given a list of programs to run on a single processor
  • Each program has a start time and a finish time
  • However the processor can only run one program at any given time, and there is no preemption (i.e. once a program is running, it must be completed)

Another Motivating Problem

  • Suppose you are at a film fest, all movies look equally good, and you want to see as many complete movies as possible
  • This problem is also exactly the same as the activity selection problem.

Greedy Scheduling of Activities

  • time

Analysis

  • Let n be the total number of activities
  • The algorithm first sorts the activities by finish time taking O(n log n)
  • Then the algorithm visits each activity exactly once, doing a constant amount of work each time. This takes O(n)
  • Thus total time is O(n log n)

Optimality

  • The big question here is: Does the greedy algorithm give us an optimal solution???
  • Surprisingly, the answer turns out to be yes

Todo

  • Finish Chapter 15
  • Start Chapter 16