31.05.2013 Views

think-cell technical report TC2003/01 A GUI-based Interaction ...

think-cell technical report TC2003/01 A GUI-based Interaction ...

think-cell technical report TC2003/01 A GUI-based Interaction ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.2 An Application of Dynamic Programming IMPLEMENTATION<br />

Algorithm: MergeGridlines (shortest path by dynamic programming)<br />

Input: listSource : Array [1..n] of Gridline<br />

listDest : Array [1..m] of Gridline<br />

Output: minimalCost : Accumulated cost for the optimal path<br />

optimalPath : A list of source, dest, identify<br />

Variables: costSource, costDest, costIdentify, minimalCost : Cost<br />

optimalPath : List of Choice<br />

matrixCost : Array [0..n+1][0..m+1] of Cost<br />

matrixChoice : Array [1..n+1][1..m+1] of Choice<br />

iSrc, iDst, iDstStart : Index<br />

Begin<br />

Initialize(matrixCost, ∞)<br />

matrixCost[1][1] ← 0<br />

For iSrc ← 1 To n + 1 Do<br />

If iSrc = 1 Then iDstStart ← 2 Else iDstStart ← 1 // Skip upper left corner<br />

For iDst ← iDstStart To m + 1 Do<br />

costSource ← CostPrev(source, . . . ) + matrixCost[iSrc − 1][iDst]<br />

costDest ← CostPrev(dest, . . . ) + matrixCost[iSrc][iDst − 1]<br />

costIdentify ← CostPrev(identify, . . . ) + matrixCost[iSrc − 1][iDst − 1]<br />

If costDest < costSource Then<br />

If costDest < costIdentify Then<br />

matrixCost[iSrc][iDst] ← costDest<br />

matrixChoice[iSrc][iDst] ← dest<br />

Else<br />

matrixCost[iSrc][iDst] ← costIdentify<br />

matrixChoice[iSrc][iDst] ← identify<br />

End If<br />

Else<br />

If costSource < costIdentify Then<br />

matrixCost[iSrc][iDst] ← costSource<br />

matrixChoice[iSrc][iDst] ← source<br />

Else<br />

matrixCost[iSrc][iDst] ← costIdentify<br />

matrixChoice[iSrc][iDst] ← identify<br />

End If<br />

End If<br />

End For<br />

End For<br />

// Reconstruct optimal solution<br />

i ← n + 1, j ← m + 1<br />

minimalCost ← matrixCost[i][j]<br />

optimalP ath ← ()<br />

While i > 1 Or j > 1 Do<br />

optimalP ath ← matrixChoice[i][j].optimalP ath // a.(b, c) ↦→ (a, b, c)<br />

If matrixChoice[i][j] = source Or matrixChoice[i][j] = identify Then<br />

i ← i − 1<br />

End If<br />

If matrixChoice[i][j] = dest Or matrixChoice[i][j] = identify Then<br />

j ← j − 1<br />

End If<br />

End While<br />

Return (minimalCost, optimalP ath)<br />

End<br />

Figure 45: A dynamic programming algorithm for gridline matching, running in<br />

polynomial-time<br />

81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!