89%(19)17 out of 19 people found this document helpful
This preview shows page 1 - 3 out of 11 pages.
CS 170AlgorithmsFall 2014David WagnerSol 81. (20 pts.)SubsequenceDesign an efficient algorithm to check whether the stringA[1..n]is a subsequence ofB[1..m], forn≤m. Therunning time of your solution should be at mostO(nm).Note: the elements in a subsequence do not need to be consecutive (subsequence6=substring).Solution 1: We can solve this problem with dynamic programming. Define the predicatef(i,j)to be true ifthe stringA[1...i]is a subsequence ofB[1...j]. We obtain a recursive formula forf(i,j):f(i,j) =Trueifi=0∧j≥0Falseifj<if(i-1,j-1)ifA[i] =B[j]f(i,j-1)otherwisePseudocode:1.Forj:=0,1,...,m:2.Setf[j]:=True.3.Fori:=1,2,...,n:4.Forj:=1,2,...,m:5.Ifj<i:6.Setf[i][j] =False.7.else ifA[i] =B[j]:8.Setf[i][j]:=f[i-1][j-1].7.else:8.Setf[i][j]:=f[i][j-1].9.Returnf(n,m).Correctness:The recursive expression forf(i,j)follows from a case analysis:• ifA[i] =B[j]: SinceB[j]matches withA[i],A[1...i]is a subsequence ofB[1...j] iffA[1...i-1]is asubsequence ofB[1...j-1].• ifA[i]6=B[j]: SinceB[j]doesn’t match withA[i],A[1...i]is a subsequence ofB[1...j] iffA[1...i]isa subsequence ofB[1...j-1].We justify the base cases as follows:•i=0∧j≥0: There are no remaining characters withinAto match withB.A[1...0]is trivially asubsequence ofB[1...j]. Therefore,f(i,j)is True.•j<i: SinceA[1...i]is a larger string thanB[1...j],A[1...i]cannot be a subsequence ofB[1...j].Therefore,f(i,j)is False.CS 170, Fall 2014, Sol 81
Running time:There aremnsubproblems, and each problem takesO(1)time (lines 5–8). Therefore, thetotal running time isO(nm).Solution 2:A greedy linear scan through the two arrays solves this problem in linear time.Pseudocode:1.Seti:=0 andj:=0.2.@loop invariant:A[1..i]is a subsequence ofB[1..j].3.Whilej<mandi<n:4.IfB[j+1] =A[i+1]:5.Seti:=i+1.6.Setj:=j+1.7.Return true ifi=n, false otherwise.This algorithm has a running time ofO(m+n). The while loop maintains the invariant thatA[1..i]is asubsequence ofB[1..j].12. (20 pts.)Another scheduling problemYou’re running a massive physical simulation, which can only be run on a supercomputer. You’ve got accessto two (identical) supercomputers, but unfortunately you have a fairly low priority on these machines, soyou can only get time slots on them when they’d otherwise be idle. You’ve been given information abouthow much idle computing power is available on each supercomputer for each of the nextnone-hour timeslots: you can getaiseconds of computation done on supercomputer A in theith hour if your job is runningon A at that point, orbiseconds 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 yourjob from one supercomputer to another at any point, but it takes an hour to transfer the accumulated databetween supercomputers before your job can begin running on the new supercomputer, so a one-hour timeslot will be wasted where you make no progress.