Skip to content

Commit

Permalink
Added subset stuff to handbook
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoshida28 committed Jan 1, 2022
1 parent df41116 commit a888bb3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
Binary file modified acm_uci_handbook/main.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions acm_uci_handbook/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@


\PartDivider{Algorithms}
\section{Dynamic Programming}
\subsection{Sum over Subsets}
Given an array \verb|A|, sum over subsets fills array \verb|S| such that \verb|S[mask]| is the sum of all values at indices \verb|s| where $s \subseteq mask$ in \verb|A|.
This algorithm is $O(N2^{N})$
\lstinputlisting[language=C++]{snippets/sum_over_subsets.cpp}
\lstinputlisting[language=Python]{snippets/sum_over_subsets.py}

\section{Geometry}
\subsection{Closest Points}
\subsection{Convex Hull}
Expand Down Expand Up @@ -238,6 +245,11 @@
Returns $\sum_{k=0}^{end} k^{3} = \frac{n^{2}(n+1)^{2}}{4}$
\lstinputlisting[language=C++]{snippets/sum_of_cubes.cpp}
\lstinputlisting[language=Python]{snippets/sum_of_cubes.py}
\section{Miscellaneous}
\subsection{Subset Enumeration}
Given a bitmask \verb|m|, the following iterates through all submasks of \verb|m|.
\lstinputlisting[language=C++]{snippets/subset_enumeration.cpp}
\lstinputlisting[language=Python]{snippets/subset_enumeration.py}
\section{Strings}
\subsection{Knuth Morris Pratt: String Matching} \label{kmp}
\subsection{Levinshtein Distance: Edit Distance}
Expand Down
3 changes: 3 additions & 0 deletions acm_uci_handbook/snippets/subset_enumeration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int s=m; s; s=(s-1)&m) {
// ...
}
4 changes: 4 additions & 0 deletions acm_uci_handbook/snippets/subset_enumeration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
s = m
while s:
# ...
s = (s-1) & m
7 changes: 7 additions & 0 deletions acm_uci_handbook/snippets/sum_over_subsets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Initialize S array
for (int i = 0; i < (1<<N); ++i)
S[i] = A[i];

for (int i = 0; i < N; ++i) for (int mask = 0; mask < (1<<N); ++mask)
if (mask & (1<<i))
S[mask] += S[mask^(1<<i)];
8 changes: 8 additions & 0 deletions acm_uci_handbook/snippets/sum_over_subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from copy import copy

S = copy(A)
for i in range(N):
for mask in range(1<<N):
if mask & (1<<i):
S[mask] += S[mask^(1<<i)]

0 comments on commit a888bb3

Please sign in to comment.