Skip to content

Commit

Permalink
#112 4.1-1
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtask committed Nov 1, 2023
1 parent ebe3646 commit 6b9d931
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 57 deletions.
48 changes: 47 additions & 1 deletion chapter4/sections/1/1.tex
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
\workinprogress % TODO
Observe, that for $n\times n$ matrices $A$ and $B$,
\[
\PARENS{
\begin{matrix}
A & 0 \\
0 & 0
\end{matrix}
\,}\PARENS{
\begin{matrix}
B & 0 \\
0 & 0
\end{matrix}
\,} = \PARENS{
\begin{matrix}
AB & 0 \\
0 & 0
\end{matrix}
\,},
\]
where 0 denotes zero matrices.
In other words, when multiplying the square matrices $A$ and $B$, we can pad them with zeros to obtain $m\times m$ matrices $A'=\PARENS{\begin{matrix}A&0\\0&0\end{matrix}\,}$ and $B'=\PARENS{\begin{matrix}B&0\\0&0\end{matrix}\,}$, and multiply $A'$ by $B'$ instead.
The submatrix formed by taking the intersection of the first $n$ rows and the first $n$ columns of $A'B'$ is $AB$.

We implement this trick in the generalized version of the divide-and-conquer algorithm for multiplying $n\times n$ matrices.
First we find $m$\dash the least power of 2 greater than of equal to $n$.
Next we create the $m\times m$ matrices $A'$ and $B'$, by copying the corresponding entries of the matrices $A$ and $B$ to the upper left $n\times n$ submatrices of $A'$ and $B'$, while setting the remaining entries to 0.
For a given $n\times n$ matrix $C$, where the matrix product $AB$ will be added, we similarly pad it with zeros to create the $m\times m$ matrix $C'$.
Then we can call $\proc{Matrix-Multiply-Recursive}(A',B',C',m)$ and copy the relevant entries of the matrix $C'$ to $C$.

Creating each matrix $A'$, $B'$, and $C'$ takes $\Theta(m^2)$ time, and copying the result to the matrix $C$ takes $\Theta(n^2)$ time.
Let $T(n)$ and $T'(n)$ be the worst-case time to multiply two $n\times n$ matrices using the standard \proc{Matrix-Multiply-Recursive} procedure, and its generalized version, respectively.
Then, omitting the base case, we get
\[
T'(n) = T(m)+\Theta(m^2)+\Theta(n^2).
\]
Note that $n\le m<2n$, so
\begin{align*}
T'(n) &\ge T(n)+\Theta(n^2)+\Theta(n^2) \\
&= \Omega(n^3)
\end{align*}
and
\begin{align*}
T'(n) &\le T(2n)+\Theta((2n)^2)+\Theta(n^2) \\
&= O((2n)^3) \\
&= O(n^3).
\end{align*}
Hence, $T'(n)=\Theta(n^3)$.
114 changes: 58 additions & 56 deletions clrs4e-solutions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -343,31 +343,33 @@

\part{Foundations}

\MakeChapter{The Role of Algorithms in Computing}{
Algorithms,
Algorithms as a technology}{}{
Comparison of running times}

\MakeChapter{Getting Started}{
Insertion sort,
Analyzing algorithms,
Designing algorithms}{}{
Insertion sort on small arrays in merge sort,
Correctness of bubblesort,
Correctness of Horner's rule,
Inversions}

\MakeChapter{Characterizing Running Times}{
{$O$-notation, $\Omega$-notation, and $\Theta$-notation},
Asymptotic notation: formal definitions,
Standard notations and common functions}{}{
Asymptotic behavior of polynomials,
Relative asymptotic growths,
Ordering by asymptotic growth rates,
Asymptotic notation properties,
Manipulating asymptotic notation,
Variations on $O$ and $\Omega$,
Iterated functions}
% \MakeChapter{The Role of Algorithms in Computing}{
% Algorithms,
% Algorithms as a technology}{}{
% Comparison of running times}
%
% \MakeChapter{Getting Started}{
% Insertion sort,
% Analyzing algorithms,
% Designing algorithms}{}{
% Insertion sort on small arrays in merge sort,
% Correctness of bubblesort,
% Correctness of Horner's rule,
% Inversions}
%
% \MakeChapter{Characterizing Running Times}{
% {$O$-notation, $\Omega$-notation, and $\Theta$-notation},
% Asymptotic notation: formal definitions,
% Standard notations and common functions}{}{
% Asymptotic behavior of polynomials,
% Relative asymptotic growths,
% Ordering by asymptotic growth rates,
% Asymptotic notation properties,
% Manipulating asymptotic notation,
% Variations on $O$ and $\Omega$,
% Iterated functions}

\setcounter{chapter}{3}

\MakeChapter{Divide-and-Conquer}{
Multiplying square matrices,
Expand All @@ -385,37 +387,37 @@
Chip testing,
Monge arrays}

\appendix

\MakeChapter{Summations}{
Summation formulas and properties,
Bounding summations}{}{
Bounding summations}

\MakeChapter{Sets, Etc.}{
Sets,
Relations,
Functions,
Graphs,
Trees}{}{
Graph coloring,
Friendly graphs,
Bisecting trees}

\MakeChapter{Counting and Probability}{
Counting,
Probability,
Discrete random variables,
The geometric and binomial distributions,
The tails of the binomial distribution}{5}{
The Monty Hall problem,
Balls and bins}

\MakeChapter{Matrices}{
Matrices and matrix operations,
Basic matrix properties}{}{
Vandermonde matrix,
Permutations defined by matrix-vector multiplication over GF(2)}
% \appendix
%
% \MakeChapter{Summations}{
% Summation formulas and properties,
% Bounding summations}{}{
% Bounding summations}
%
% \MakeChapter{Sets, Etc.}{
% Sets,
% Relations,
% Functions,
% Graphs,
% Trees}{}{
% Graph coloring,
% Friendly graphs,
% Bisecting trees}
%
% \MakeChapter{Counting and Probability}{
% Counting,
% Probability,
% Discrete random variables,
% The geometric and binomial distributions,
% The tails of the binomial distribution}{5}{
% The Monty Hall problem,
% Balls and bins}
%
% \MakeChapter{Matrices}{
% Matrices and matrix operations,
% Basic matrix properties}{}{
% Vandermonde matrix,
% Permutations defined by matrix-vector multiplication over GF(2)}

\backmatter

Expand Down

0 comments on commit 6b9d931

Please sign in to comment.