User:Realazthat/Notes/Scrap: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 161: Line 161:
W_{2}(q) & \text{if } b(E,C_{p-1},C_{q+1}) = 1 \wedge f(E,F,C_{p-1},C_{q+1}) = 1
W_{2}(q) & \text{if } b(E,C_{p-1},C_{q+1}) = 1 \wedge f(E,F,C_{p-1},C_{q+1}) = 1
\end{cases}\\
\end{cases}\\
W_{0} \in \left\{0,1,2,\infty\right\}
W_{0}\left(q\right) \in \left\{0,1,2,\infty\right\}
& = &
& = &
\begin{cases}
\begin{cases}

Revision as of 22:38, 4 January 2011

Du


Naive algorithm


newedgdes(C,p,q,r,s):
  return { (C[p-1],C[q+1]), (C[q],C[s]), (C[p], C[r]) }

countbps(E,NE):
  newbps = 0
  for ( ne in NE )
    if ( ne !in E )
      ++newbps
  return newbps


countmainbps(F,E,NE):
  newmainbps = 0
  for ( ne in NE )
    if ( ne !in E && ne in F )
      ++newmainbps
  return newmainbps



m(C,F,E,p):
  min = infinity
  n = |C|
  for ( q in range(p,p-2) )
    for ( s in range( q + 2, p ) )
      r = s - 1
      for ( i in range(2) )
        NE = newedges(C,p,q,r,s)
        if ( |NE| != 0 )
          newbps = countbps(F,E,NE)
          newmainbps = countmainbps(F,E,NE)
          
          if ( newbps == 0 || newmainbps > 0 )
            min = newbps < min ? newbps : min
       
        swap(r,s)
  return min

Better algorithm

As we iterate over the different possible values for , there are incrementally more possibilities for . Each selection of implies a possible breakpoint for .

For each q, for all the valid (r,s) for that q, will track those that result in no breakpoint for (p,r).

will track those s that result in a breakpoint that is a former main breakpoint.