Text Segmentation
Explore text segmentation problem more in this lesson to take it to the next level.
Optimizing the text segmentation problem
For our next dynamic programming algorithm, let’s consider the text segmentation problem from the previous chapter. We are given a string and a subroutine that determines whether a given string is a word (whatever that means), and we want to know whether can be partitioned into a sequence of words.
We solved this problem by defining a function that returns True
if and only if the can be partitioned into a sequence of words. We need to compute . This function satisfies the recurrence
where is shorthand for . This recurrence translates directly into a recursive backtracking algorithm that calls the subroutine times in the worst case.
But for any fixed string , there are only different ways to call the recursive function —one for each value of between and —and only different ways to call —one for each pair such that .
Why are we spending exponential time computing only a polynomial amount of stuff?
Each recursive subproblem is specified by an integer between and , so we can memoize the function into an array . Each subproblem depends only on the results of subproblems , where , so the memoized recursive algorithm fills the array in decreasing index order. If we fill the array in this order deliberately, we obtain the dynamic programming algorithm shown below.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy