Skip to content

Commit

Permalink
backtrack, palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
idf committed Oct 21, 2015
1 parent 3c07c93 commit c1736c1
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
88 changes: 75 additions & 13 deletions chapterBacktracking.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ \chapter{Backtracking}
\section{Introduction}
\rih{Difference between backtracking and dfs.} \textit{Backtracking} is a more general purpose algorithm. \textit{Dfs} is a specific form of backtracking related to searching tree structures.

\rih{Prune.} Backtrack need to think about pruning using \pyinline{predicate}.

\section{Math}
\subsection{Factorization}\label{factorization}
Factorize a number.\\
\subsection{Decomposition}
\subsubsection{Factorize a number}.\label{factorization}
\rih{Core clues}:
\begin{enumerate}
\item Expand the search tree \textbf{horizontally}.
\end{enumerate}
\rih{Search tree}:
\begin{python}
Input: 16
get factors of cur[-1]
[16]
[2, 8]
[2, 2, 4]
[2, 2, 2, 2]

[4, 4]
\end{python}
\rih{Code}
\begin{python}
def dfs(self, cur, ret):
"""
16

get factors of cur[-1]
[16]
[2, 8]
[2, 2, 4]
[2, 2, 2, 2]

[4, 4]
"""
if len(cur) > 1:
ret.append(list(cur))

Expand All @@ -37,3 +43,59 @@ \subsection{Factorization}\label{factorization}

TODO

\section{String}
\subsection{Palindrome}
\subsubsection{Palindrome partition.} Given \pyinline{s = "aab"}, return: \\
\pyinline{[["aa","b"], ["a","a","b"]]}
\\
\rih{Core clues}:
\begin{enumerate}
\item Expand the search tree \textbf{horizontally}.
\end{enumerate}
\rih{Search process}:
\begin{python}
input: "aabbc"

"a", "abbc"
"a", "bbc"
"b", "bc"
"b", "c" (o)
"bc" (x)
"bb", "c" (o)
"bbc" (x)
"ab", "bc" (x)
"abb", "c" (x)
"abbc" (x)
"aa", "bbc"
"b", "bc"
"b", "c" (o)
"bc" (x)
"bb", "c" (o)
"bbc" (x)
"aab", "bc" (x)
"aabb", "c" (x)
\end{python}
\rih{Code}
\begin{python}
def partition(self, s):
ret = []
self.backtrack(s, [], ret)
return ret

def backtrack(self, s, cur_lvl, ret):
"""
Let i be the scanning ptr.
If s[:i] passes predicate, then backtrack s[i:]
"""
if not s:
ret.append(list(cur_lvl))

for i in xrange(1, len(s)+1):
if self.predicate(s[:i]):
cur_lvl.append(s[:i])
self.backtrack(s[i:], cur_lvl, ret)
cur_lvl.pop()

def predicate(self, s):
return s == s[::-1]
\end{python}
44 changes: 44 additions & 0 deletions chapterDynamicProgramming.tex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,51 @@ \section{Sequence}
It can be optimized to use space $O(1)$.
\section{String}
\runinhead{Is palindrome.} Given a string $s$, use an array to determine whether $s[i:j]$.
Let $P_{i,j}$ indicates whether $s[i:j]$ is palindrome.
\begin{eqnarray*}
P_{i. j} = P_{i-1, j+1}\ \&\&\ s[i] = s[j-1]
\end{eqnarray*}
\runinhead{Minimum palindrome cut.} Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.
Let $C_i$ be the min cut for $s[:i]$.
\begin{eqnarray*}
C_{i} = \left\{ \begin{array}{rl}
\min\big(C[k]+1 \cdot \forall k<i \big) &\mbox{// if $s[k:i]$ is palindrome}
\\
0 &\mbox{// otherwise}
\end{array} \right.
\end{eqnarray*}
\begin{python}
def minCut(self, s):
n = len(s)
P = [[False for _ in xrange(n+1)] for _ in xrange(n+1)]
for i in xrange(n+1): # len 0
P[i][i] = True
for i in xrange(n): # len 1
P[i][i+1] = True
for i in xrange(n, -1, -1): # len 2 and above
for j in xrange(i+2, n+1):
P[i][j] = P[i+1][j-1] and s[i] == s[j-1]
C = [i for i in xrange(n+1)] # max is all cut
for i in xrange(n+1):
if P[0][i]:
C[i] = 0
else:
C[i] = min(
C[j] + 1
for j in xrange(i)
if P[j][i]
)
return C[n]
\end{python}
\section{Backpack}
Given $n$ items with weight $w_i$ and value $v_i$, an integer $C$ denotes the size of a backpack. What is the max value you can fill this backpack?
Expand Down
4 changes: 2 additions & 2 deletions chapterTree.tex
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ \subsection{Operations}
self.modify(root.right, idx, val)

val = DEFAULT
if root.left: root.m = f(val, root.left.m)
if root.right: root.m = f(val, root.right.m)
if root.left: val = f(val, root.left.m)
if root.right: val = f(val, root.right.m)

root.m = val
\end{python}
Expand Down

0 comments on commit c1736c1

Please sign in to comment.