sol8 - CS 170 Algorithms Fall 2014 David Wagner Sol 8 1(20 pts Subsequence Design an efcient algorithm to check whether the string A[1.n is a

# sol8 - CS 170 Algorithms Fall 2014 David Wagner Sol 8 1(20...

• Homework Help
• klyefuelopt
• 11
• 89% (19) 17 out of 19 people found this document helpful

This preview shows page 1 - 3 out of 11 pages.

CS 170 Algorithms Fall 2014 David Wagner Sol 8 1. (20 pts.) Subsequence Design an efficient algorithm to check whether the string A [ 1 .. n ] is a subsequence of B [ 1 .. m ] , for n m . The running time of your solution should be at most O ( nm ) . Note: the elements in a subsequence do not need to be consecutive (subsequence 6 = substring). Solution 1 : We can solve this problem with dynamic programming. Define the predicate f ( i , j ) to be true if the string A [ 1 ... i ] is a subsequence of B [ 1 ... j ] . We obtain a recursive formula for f ( i , j ) : f ( i , j ) = True if i = 0 j 0 False if j < i f ( i - 1 , j - 1 ) if A [ i ] = B [ j ] f ( i , j - 1 ) otherwise Pseudocode: 1. For j : = 0 , 1 ,..., m : 2. Set f [ 0 ][ j ] : = True. 3. For i : = 1 , 2 ,..., n : 4. For j : = 1 , 2 ,..., m : 5. If j < i : 6. Set f [ i ][ j ] = False. 7. else if A [ i ] = B [ j ] : 8. Set f [ i ][ j ] : = f [ i - 1 ][ j - 1 ] . 7. else: 8. Set f [ i ][ j ] : = f [ i ][ j - 1 ] . 9. Return f ( n , m ) . Correctness: The recursive expression for f ( i , j ) follows from a case analysis: • if A [ i ] = B [ j ] : Since B [ j ] matches with A [ i ] , A [ 1 ... i ] is a subsequence of B [ 1 ... j ] iff A [ 1 ... i - 1 ] is a subsequence of B [ 1 ... j - 1 ] . • if A [ i ] 6 = B [ j ] : Since B [ j ] doesn’t match with A [ i ] , A [ 1 ... i ] is a subsequence of B [ 1 ... j ] iff A [ 1 ... i ] is a subsequence of B [ 1 ... j - 1 ] . We justify the base cases as follows: i = 0 j 0: There are no remaining characters within A to match with B . A [ 1 ... 0 ] is trivially a subsequence of B [ 1 ... j ] . Therefore, f ( i , j ) is True. j < i : Since A [ 1 ... i ] is a larger string than B [ 1 ... j ] , A [ 1 ... i ] cannot be a subsequence of B [ 1 ... j ] . Therefore, f ( i , j ) is False. CS 170, Fall 2014, Sol 8 1
Running time: There are mn subproblems, and each problem takes O ( 1 ) time (lines 5–8). Therefore, the total running time is O ( nm ) . Solution 2: A greedy linear scan through the two arrays solves this problem in linear time. Pseudocode: 1. Set i : = 0 and j : = 0. 2. @loop invariant: A [ 1 .. i ] is a subsequence of B [ 1 .. j ] . 3. While j < m and i < n : 4. If B [ j + 1 ] = A [ i + 1 ] : 5. Set i : = i + 1. 6. Set j : = j + 1. 7. Return true if i = n , false otherwise. This algorithm has a running time of O ( m + n ) . The while loop maintains the invariant that A [ 1 .. i ] is a subsequence of B [ 1 .. j ] . 1 2. (20 pts.) Another scheduling problem You’re running a massive physical simulation, which can only be run on a supercomputer. You’ve got access to two (identical) supercomputers, but unfortunately you have a fairly low priority on these machines, so you can only get time slots on them when they’d otherwise be idle. You’ve been given information about how much idle computing power is available on each supercomputer for each of the next n one-hour time slots: you can get a i seconds of computation done on supercomputer A in the i th hour if your job is running on A at that point, or b i seconds of computation if it running on supercomputer B at that point. During each hour your job can be scheduled on only one of the two supercomputers. You can move your job from one supercomputer to another at any point, but it takes an hour to transfer the accumulated data between supercomputers before your job can begin running on the new supercomputer, so a one-hour time slot will be wasted where you make no progress.

#### You've reached the end of your free preview.

Want to read all 11 pages?