<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Uzair Akbar</title>
    <subtitle>A place to dump my study notes.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://uzairakbar.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://uzairakbar.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-12-23T00:00:00+00:00</updated>
    <id>https://uzairakbar.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Orthogonal linear representation</title>
        <published>2024-12-23T00:00:00+00:00</published>
        <updated>2024-12-23T00:00:00+00:00</updated>
        
        <author>
          <name>Uzair Akbar</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://uzairakbar.com/notes/orthogonal-linear-representation/"/>
        <id>https://uzairakbar.com/notes/orthogonal-linear-representation/</id>
        
        <content type="html" xml:base="https://uzairakbar.com/notes/orthogonal-linear-representation/">&lt;p&gt;Let’s say that we have a bunch of $&lt;code&gt;d&lt;/code&gt;$-dimensional vectors
$&lt;code&gt;\mathbf{x}_i\in\mathbb{R}^d&lt;/code&gt;$, $&lt;code&gt;i\in \{1, \dots, n\}&lt;/code&gt;$ (throughout this
text we will assume $&lt;code&gt;n\ge d&lt;/code&gt;$, i.e. the number of vectors is at least
equal to the size of the vectors). We can put all of these vectors
side-by-side in the form of a matrix
$&lt;code&gt;\mathbf{X} = [\mathbf{x}_1, \dots, \mathbf{x}_n]\in \mathbb{R}^{d\times n}&lt;/code&gt;$.
We call this matrix $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ as the “data matrix,” and the vectors
$&lt;code&gt;\mathbf{x}_i&lt;/code&gt;$ as “data samples” or just “samples.” We represent the
“labels” with the symbol $&lt;code&gt;y_i\in \{1, 0\},&lt;/code&gt;$ for $&lt;code&gt;i\in \{1, \dots, n\}&lt;/code&gt;$.
Also stacking these together in the form of a vector
$&lt;code&gt;\mathbf{y} = [y_1, \dots, y_n]^\top \in\mathbb{R}^n&lt;/code&gt;$, we call this the
“label vector”.&lt;/p&gt;
&lt;p&gt;The “dimension” of a vector is the number of elements in it. For
example, the dimension of all the vectors $&lt;code&gt;\mathbf{x}_i\in\mathbb{R}^d&lt;/code&gt;$
in the above section is $&lt;code&gt;d&lt;/code&gt;$. Sometimes, we might want to reduce the
dimension of a vector, but still retain the “essence” of the vector. We
can represent these smaller vectors as $&lt;code&gt;\mathbf{z}_i\in\mathbb{R}^p&lt;/code&gt;$,
$&lt;code&gt;p &amp;lt; d&lt;/code&gt;$. And we call these smaller vectors as “representations/features”
of $&lt;code&gt;\mathbf{x}_i&lt;/code&gt;$. We will discuss later how we can get $&lt;code&gt;\mathbf{z}_i&lt;/code&gt;$
given some $&lt;code&gt;\mathbf{x}_i&lt;/code&gt;$ (and maybe also $&lt;code&gt;y_i&lt;/code&gt;$). The definition of
“essence” can pretty much be anything, but we will discuss 2 main ideas
in Sections &lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#pca&quot;&gt;4&lt;/a&gt; and &lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#lda&quot;&gt;5&lt;/a&gt; that correspond to &lt;em&gt;orthogonal linear representations&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In the context of &lt;em&gt;orthogonal linear representations&lt;/em&gt;, we might also be
interested in a representation that separates out a mixture of signals
into its ‘independent’ components. We discuss this in Section &lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#ica&quot;&gt;6&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;evd&quot;&gt;Eigen Value Decomposition (EVD)&lt;/h2&gt;
&lt;p&gt;For any square matrix $&lt;code&gt;\mathbf{X}\in\mathbb{R}^{n\times n}&lt;/code&gt;$, we have $&lt;code&gt;n&lt;/code&gt;$
“eigenvalues” $&lt;code&gt;\lambda_i\in\mathbb{R}&lt;/code&gt;$ and corresponding “eigenvectors”
$&lt;code&gt;\mathbf{u}_i\in\mathbb{R}^{n}&lt;/code&gt;$. Eigenvalues and eigenvectors are
special in the sense that for each pair the following equation holds&lt;/p&gt;
&lt;div id=&quot;eq:evd1&quot;&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{X}\mathbf{u}_i = \lambda_i\mathbf{u}_i\;. \tag{1}&lt;/code&gt;$$&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To get an intuitive understanding of the equation (&lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#eq:evd1&quot;&gt;1&lt;/a&gt;),
watch &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.youtube.com/watch?v=PFDu9oVAE-g&quot;&gt;this video&lt;/a&gt;. It turns out that
matrix $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ can be “decomposed” into the product of (square)
matrices of its eigenvalues and eigenvectors.&lt;/p&gt;
&lt;div id=&quot;eq:evd2&quot;&gt;
&lt;p&gt;$$&lt;code&gt;\begin{aligned} \mathbf{X} &amp;amp;= \begin{bmatrix} \mathbf{u}_1 &amp;amp; \dots &amp;amp; \mathbf{u}_n\end{bmatrix} \begin{bmatrix} \lambda_1&amp;amp; &amp;amp; \\ &amp;amp; \ddots &amp;amp;\\ &amp;amp; &amp;amp; \lambda_n \end{bmatrix} \begin{bmatrix} \mathbf{u}_1 &amp;amp; \dots &amp;amp; \mathbf{u}_n\end{bmatrix}^{-1}\\ &amp;amp;= \mathbf{U} \mathbf{\Lambda} \mathbf{U}^{-1} \tag{2} \end{aligned}&lt;/code&gt;$$&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This is what we call “Eigen Value Decomposition” (EVD). Note that these
eigenvalues and eigenvectors in $&lt;code&gt;\mathbf{\Lambda}&lt;/code&gt;$ and $&lt;code&gt;\mathbf{U}&lt;/code&gt;$
don’t have any particular order.&lt;/p&gt;
&lt;h2 id=&quot;svd&quot;&gt;Singular Value Decomposition (SVD)&lt;/h2&gt;
&lt;p&gt;It turns out that any (not necessarily square) matrix
$&lt;code&gt;\mathbf{X}\in \mathbb{R}^{d\times n}&lt;/code&gt;$ can be “decomposed” into the
product of three matrices as&lt;/p&gt;
&lt;div id=&quot;eq:svd1&quot;&gt;
&lt;p&gt;$$&lt;code&gt;\begin{aligned} \mathbf{X} &amp;amp;= \overbrace{\begin{bmatrix} \mathbf{u}_1 &amp;amp; \dots &amp;amp; \mathbf{u}_d\end{bmatrix} }^{\in\mathbb{R}^{d\times d} } \overbrace{\left[   \begin{matrix}     \sigma_1 &amp;amp;  &amp;amp;  \\      &amp;amp; \ddots &amp;amp;  \\      &amp;amp;  &amp;amp; \sigma_d \\   \end{matrix}   \left|     \,     \begin{matrix}         \\       \mathbf{0}_{d\times(n-d)}  \\         \\     \end{matrix}   \right. \right]}^{\in\mathbb{R}^{d\times n} } \overbrace{\begin{bmatrix} \mathbf{v}_1 &amp;amp; \dots &amp;amp; \mathbf{v}_n\end{bmatrix}^\top}^{\in\mathbb{R}^{n\times n} } \\ &amp;amp;= \mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top\;, \tag{3} \end{aligned}&lt;/code&gt;$$&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;where $&lt;code&gt;\mathbf{U}&lt;/code&gt;$ and $&lt;code&gt;\mathbf{V}&lt;/code&gt;$ are “orthonormal” matrices, which
just means that $&lt;code&gt;\mathbf{U}^\top\mathbf{U} = \mathbf{I}_d&lt;/code&gt;$, or in other
words $&lt;code&gt;\mathbf{U}^\top=\mathbf{U}^{-1}&lt;/code&gt;$ and $&lt;code&gt;\mathbf{\Sigma}&lt;/code&gt;$ is a
diagonal matrix (not necessarily square). $&lt;code&gt;\sigma_i&lt;/code&gt;$ are called
“singular values” and $&lt;code&gt;\mathbf{u}_i\in\mathbb{R}^d&lt;/code&gt;$ and
$&lt;code&gt;\mathbf{v}_i\in\mathbb{R}^n&lt;/code&gt;$ are called left and right “singular
vectors” respectively. And, interestingly, there is a descending order
to the singular values in $&lt;code&gt;\mathbf{\Sigma}&lt;/code&gt;$ and they are also all
non-negative, i.e. $&lt;code&gt;\sigma_1 \ge \dots \ge \sigma_d \ge 0&lt;/code&gt;$.&lt;/p&gt;
&lt;h3 id=&quot;svdVsEvd&quot;&gt;SVD vs. EVD&lt;/h3&gt;
&lt;p&gt;There is a very close relationship between EVD and SVD. This can be seen
by substituting the SVD of $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ from
(&lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#eq:svd1&quot;&gt;3&lt;/a&gt;) in
$&lt;code&gt;\mathbf{X}\mathbf{X}^\top&lt;/code&gt;$ and $&lt;code&gt;\mathbf{X}^\top\mathbf{X}&lt;/code&gt;$.&lt;/p&gt;
&lt;div id=&quot;eq:svdVsEvd1&quot;&gt;
&lt;p&gt;$$&lt;code&gt;\begin{aligned} \mathbf{X}\mathbf{X}^\top &amp;amp;= \big(\mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top\big) \big(\mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top\big)^\top \\   &amp;amp;= \big(\mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top\big)\big(\mathbf{V} \mathbf{\Sigma} \mathbf{U}^\top\big) \\   &amp;amp;= \mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top \mathbf{V} \mathbf{\Sigma} \mathbf{U}^\top \\     &amp;amp;= \mathbf{U} \mathbf{\Sigma} \mathbf{I}_n \mathbf{\Sigma} \mathbf{U}^\top \\   &amp;amp;= \mathbf{U} \mathbf{\Sigma} \mathbf{\Sigma} \mathbf{U}^\top \\   &amp;amp;= \mathbf{U} \mathbf{\Sigma}^2 \mathbf{U}^\top \\   &amp;amp;= \mathbf{U} \mathbf{\Sigma}^2 \mathbf{U}^{-1} \tag{4} \end{aligned}&lt;/code&gt;$$&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here, we have used the fact that
$&lt;code&gt;(\mathbf{A}\mathbf{B})^\top = \mathbf{B}^\top\mathbf{A}^\top&lt;/code&gt;$.
Equations (&lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#eq:svdVsEvd1&quot;&gt;4&lt;/a&gt;) and
(&lt;a href=&quot;https://uzairakbar.com/notes/orthogonal-linear-representation/#eq:evd2&quot;&gt;2&lt;/a&gt;) are similar. Therefore, $&lt;code&gt;\mathbf{U}&lt;/code&gt;$ is the matrix of eigenvectors of
$&lt;code&gt;\mathbf{X}\mathbf{X}^\top&lt;/code&gt;$ and, similarly, $&lt;code&gt;\mathbf{V}&lt;/code&gt;$ is the matrix
of eigenvectors of $&lt;code&gt;\mathbf{X}^\top\mathbf{X}&lt;/code&gt;$. In each case,
$&lt;code&gt;\mathbf{\Sigma}^2&lt;/code&gt;$ is the eigenvalue matrix.&lt;/p&gt;
&lt;h2 id=&quot;pca&quot;&gt;Principal Component Analysis (PCA)&lt;/h2&gt;
&lt;p&gt;Principal Component Analysis (PCA) is a special (and arguably the most
famous) dimensionality reduction technique. Let’s first go over the
steps of PCA and we shall discuss the intuition and motivation behind it
shortly after.&lt;/p&gt;
&lt;p&gt;Given a data matrix
$&lt;code&gt;\mathbf{X} = [\mathbf{x}_1, \dots, \mathbf{x}_n]\in \mathbb{R}^{d\times n}&lt;/code&gt;$,
we wish to reduce the dimension of the data from $&lt;code&gt;d&lt;/code&gt;$ to $&lt;code&gt;k&amp;lt;d&lt;/code&gt;$ to get
smaller versions $&lt;code&gt;\mathbf{z}_i&lt;/code&gt;$ of each sample $&lt;code&gt;\mathbf{x}_i&lt;/code&gt;$. We carry
out the following steps to get the so called “principal components&quot; and
“score matrix&quot; of the data.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Calculate the “mean&quot; of the data&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\boldsymbol{\mu} = \frac{1}{n}\sum_{i=1}^n\mathbf{x}_i = \frac{1}{n}\mathbf{X}\mathbf{1}_n\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Calculate the “centered&quot; data matrix&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\begin{aligned} \bar{\mathbf{X} } &amp;amp;= \mathbf{X} - \boldsymbol{\mu}\otimes\mathbf{1}_n^\top\\ &amp;amp;=\begin{bmatrix} (\mathbf{x}_1 - \boldsymbol{\mu})&amp;amp; \dots &amp;amp; (\mathbf{x}_n - \boldsymbol{\mu})\end{bmatrix}\\ &amp;amp;=\begin{bmatrix} {\bar{\mathbf{x} } }_1 &amp;amp; \dots &amp;amp; {\bar{\mathbf{x} } }_n\end{bmatrix}\in\mathbb{R}^{d\times n}\;. \end{aligned}&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;Beyond this point, it would be better for the reader
to consider $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ and $&lt;code&gt;\bar{\mathbf{X} }&lt;/code&gt;$ as essentially the
same for ease of understanding.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Perform the SVD on
$&lt;code&gt;\bar{\mathbf{X} } = \mathbf{U} \mathbf{\Sigma} \mathbf{V}^\top&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Throw away all but the first $&lt;code&gt;k&lt;/code&gt;$ “singular vectors” (columns) from
$&lt;code&gt;\mathbf{U}=\begin{bmatrix} \mathbf{u}_1 &amp;amp; \dots &amp;amp; \mathbf{u}_d\end{bmatrix}&lt;/code&gt;$ to get a smaller
matrix $&lt;code&gt;\mathbf{U}_k=\begin{bmatrix} \mathbf{u}_1 &amp;amp; \dots &amp;amp; \mathbf{u}_k\end{bmatrix}\in\mathbb{R}^{d\times k}&lt;/code&gt;$.
This matrix is called the matrix of $&lt;code&gt;k&lt;/code&gt;$ “principal components” of
$&lt;code&gt;\mathbf{X}&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The matrix
$&lt;code&gt;\mathbf{Z}=\mathbf{U}_k^\top\bar{\mathbf{X} }=\begin{bmatrix} \mathbf{z}_1 &amp;amp; \dots &amp;amp; \mathbf{z}_n\end{bmatrix}\in\mathbb{R}^{k\times n}&lt;/code&gt;$
is called the “score matrix” of $&lt;code&gt;\mathbf{X}&lt;/code&gt;$, where each “score
vector” $&lt;code&gt;\mathbf{z}_i\in\mathbb{R}^{k}&lt;/code&gt;$ is a low-dimensional version
of the corresponding data sample $&lt;code&gt;\mathbf{x}_i\in\mathbb{R}^{d}&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;One interesting thing to note here is that given $&lt;code&gt;\mathbf{Z}&lt;/code&gt;$ and
$&lt;code&gt;\mathbf{U}_k&lt;/code&gt;$ that were obtained from PCA, we can “reconstruct” an
“approximate” version of the original (centered) data matrix
$&lt;code&gt;\bar{\mathbf{X} }&lt;/code&gt;$ as
$&lt;code&gt;\widehat{\bar{\mathbf{X} } } = \mathbf{U}_k\mathbf{Z} = \mathbf{U}_k\mathbf{U}_k^\top\bar{\mathbf{X} }=\begin{bmatrix} \widehat{\bar{\mathbf{x} } }_1 &amp;amp; \dots &amp;amp; \widehat{\bar{\mathbf{x} } }_n\end{bmatrix}\in\mathbb{R}^{d\times n}&lt;/code&gt;$.
These “approximations” $&lt;code&gt;\widehat{\bar{\mathbf{x} } }_i&lt;/code&gt;$ of samples
$&lt;code&gt;{\bar{\mathbf{x} } }_i&lt;/code&gt;$ are also called the “projections” of
$&lt;code&gt;{\bar{\mathbf{x} } }_i&lt;/code&gt;$ onto the “space spanned by the principal
components.” For a new, previously unseen sample
$&lt;code&gt;\mathbf{x}_\text{new}&lt;/code&gt;$, we perform only steps 2 and 5 to get
$&lt;code&gt;\mathbf{z}_\text{new}&lt;/code&gt;$.&lt;/p&gt;
&lt;p&gt;Now that we have discussed the precise steps of PCA, we can discuss the
intuition behind it.&lt;/p&gt;
&lt;h3 id=&quot;minimum-residual-view-of-pca&quot;&gt;Minimum residual view of PCA&lt;/h3&gt;
&lt;p&gt;Imagine we have the (centered) data samples in $&lt;code&gt;\mathbb{R}^d&lt;/code&gt;$ (e.g.,
$&lt;code&gt;d=2&lt;/code&gt;$). Our goal is to find a different version of this data that
instead lies in $&lt;code&gt;\mathbb{R}^k&lt;/code&gt;$, which is a lower-dimensional space for
$&lt;code&gt;k&amp;lt;d&lt;/code&gt;$ (e.g., $&lt;code&gt;k=1&lt;/code&gt;$). Then, PCA can be thought of as “finding” this
low-dimensional “linear”&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; space such that, on average, the difference
(also called “residual”) between any sample $&lt;code&gt;\bar{\mathbf{x} }_i&lt;/code&gt;$ and its
projection $&lt;code&gt;\widehat{\bar{\mathbf{x} } }_i&lt;/code&gt;$ onto the low-dimensional space
is as small as possible. The unit vectors (also called “basis vectors”
or just “basis”) of this low-dimensional space are precisely the
principal components $&lt;code&gt;\mathbf{u}_i&lt;/code&gt;$ (therefore there are $&lt;code&gt;k&lt;/code&gt;$ unit
vectors for a $&lt;code&gt;k&lt;/code&gt;$-dimensional space).&lt;/p&gt;
&lt;h3 id=&quot;maximum-variance-view-of-pca&quot;&gt;Maximum variance view of PCA&lt;/h3&gt;
&lt;p&gt;The principal components of the data are vectors along the maximum
variance directions of the data. The first principal component lies
along the direction of highest variance. The second principal component
lies along the direction of second highest variance, and so on. The
score vectors $&lt;code&gt;\mathbf{z}_i&lt;/code&gt;$ are then the “projections”&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;
$&lt;code&gt;\widehat{\bar{\mathbf{x} } }_i&lt;/code&gt;$ of the original (centered) samples
$&lt;code&gt;\bar{\mathbf{x} }_i&lt;/code&gt;$ onto a linear-subspace with unit-vectors
$&lt;code&gt;{\mathbf{u} }_j&lt;/code&gt;$, but viewed in the sub-space instead of the original
space.&lt;/p&gt;
&lt;h2 id=&quot;lda&quot;&gt;Linear Discriminant Analysis (LDA)&lt;/h2&gt;
&lt;p&gt;Linear Discriminant Analysis (LDA) is a dimensionality reduction
technique that utilizes class labels to find directions that maximize
the separability between classes. Given the data matrix
$&lt;code&gt;\mathbf{X} = [\mathbf{x}_1, \dots, \mathbf{x}_n] \in \mathbb{R}^{d \times n}&lt;/code&gt;$
and the label vector
$&lt;code&gt;\mathbf{y} = [y_1, \dots, y_n]^\top \in \{0, 1\}^n&lt;/code&gt;$, LDA aims to
project the data into a lower-dimensional space such that the projected
samples $&lt;code&gt;\mathbf{z}_i \in \mathbb{R}^k&lt;/code&gt;$ (with $&lt;code&gt;k &amp;lt; d&lt;/code&gt;$) maximize the
class separability.&lt;/p&gt;
&lt;p&gt;The steps to perform LDA are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Compute the mean vector of the entire dataset:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\boldsymbol{\mu} = \frac{1}{n} \sum_{i=1}^n \mathbf{x}_i = \frac{1}{n} \mathbf{X} \mathbf{1}_n \in \mathbb{R}^d\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Compute the mean vector for each class $&lt;code&gt;c \in \{0, 1\}&lt;/code&gt;$:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\boldsymbol{\mu}_c = \frac{1}{n_c} \sum_{i \in \mathcal{C}_c} \mathbf{x}_i = \frac{1}{n_c} \mathbf{X}_c \mathbf{1}_{n_c} \in \mathbb{R}^d\;,&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;where $&lt;code&gt;\mathcal{C}_c = \{i : y_i = c\}&lt;/code&gt;$,
$&lt;code&gt;\mathbf{X}_c \in \mathbb{R}^{d \times n_c}&lt;/code&gt;$ contains the samples
from class $&lt;code&gt;c&lt;/code&gt;$, and $&lt;code&gt;n_c = |\mathcal{C}_c|&lt;/code&gt;$ is the number of samples
in class $&lt;code&gt;c&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Compute the within-class scatter matrix:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\begin{aligned} \mathbf{S}_\text{W} &amp;amp;= \sum_{c=0}^1 \sum_{i \in \mathcal{C}_c} (\mathbf{x}_i - \boldsymbol{\mu}_c)(\mathbf{x}_i - \boldsymbol{\mu}_c)^\top\\ &amp;amp;= \sum_{c=0}^1 (\mathbf{X}_c - \boldsymbol{\mu}_c \otimes \mathbf{1}_{n_c}^\top)(\mathbf{X}_c - \boldsymbol{\mu}_c \otimes \mathbf{1}_{n_c}^\top)^\top \in \mathbb{R}^{d \times d}\;. \end{aligned}&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Compute the between-class scatter matrix:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{S}_\text{B} = \sum_{c=0}^1 n_c (\boldsymbol{\mu}_c - \boldsymbol{\mu})(\boldsymbol{\mu}_c - \boldsymbol{\mu})^\top \in \mathbb{R}^{d \times d}\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Solve the generalized eigenvalue problem:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{S}_\text{W}^{-1} \mathbf{S}_\text{B} \mathbf{w}_i = \lambda_i \mathbf{w}_i\;,&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;where $&lt;code&gt;\mathbf{w}_i \in \mathbb{R}^d&lt;/code&gt;$ are the
eigenvectors and $&lt;code&gt;\lambda_i \in \mathbb{R}&lt;/code&gt;$ are the eigenvalues.
Sort the eigenvectors by decreasing eigenvalue magnitude.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Select the top $&lt;code&gt;k&lt;/code&gt;$ eigenvectors $&lt;code&gt;\mathbf{w}_1, \dots, \mathbf{w}_k&lt;/code&gt;$
to form the projection matrix
$&lt;code&gt;\mathbf{W} = \begin{bmatrix} \mathbf{w}_1 &amp;amp; \dots &amp;amp; \mathbf{w}_k \end{bmatrix} \in \mathbb{R}^{d \times k}&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Project the data into the lower-dimensional space:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{Z} = \mathbf{W}^\top \mathbf{X} \in \mathbb{R}^{k \times n}\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;LDA ensures that the projected data $&lt;code&gt;\mathbf{Z}&lt;/code&gt;$ maximizes the ratio of
between-class variance to within-class variance in the lower-dimensional
space.&lt;/p&gt;
&lt;h2 id=&quot;ica&quot;&gt;Independent Component Analysis (ICA)&lt;/h2&gt;
&lt;p&gt;Independent Component Analysis (ICA) is a dimensionality reduction
technique aimed at finding a representation of the data where the
components are statistically independent. Unlike PCA or LDA, ICA does
not rely on variance or class labels but instead focuses on higher-order
statistical independence.&lt;/p&gt;
&lt;p&gt;Given the data matrix
$&lt;code&gt;\mathbf{X} = [\mathbf{x}_1, \dots, \mathbf{x}_n] \in \mathbb{R}^{d \times n}&lt;/code&gt;$,
ICA assumes that $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ is a linear mixture of independent
components. This means:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{X} = \mathbf{A} \mathbf{S}\;,&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;where $&lt;code&gt;\mathbf{A} \in \mathbb{R}^{d \times d}&lt;/code&gt;$ is the
mixing matrix, and
$&lt;code&gt;\mathbf{S} = [\mathbf{s}_1, \dots, \mathbf{s}_n] \in \mathbb{R}^{d \times n}&lt;/code&gt;$
contains the independent components $&lt;code&gt;\mathbf{s}_i \in \mathbb{R}^d&lt;/code&gt;$. The
goal of ICA is to find an unmixing matrix
$&lt;code&gt;\mathbf{W} \in \mathbb{R}^{d \times d}&lt;/code&gt;$ such that:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{S} = \mathbf{W} \mathbf{X}\;,&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;where the rows of $&lt;code&gt;\mathbf{S}&lt;/code&gt;$ are as statistically independent as possible.&lt;/p&gt;
&lt;p&gt;The steps to perform ICA are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Center the data matrix $&lt;code&gt;\mathbf{X}&lt;/code&gt;$ by subtracting the mean vector
$&lt;code&gt;\boldsymbol{\mu} = \frac{1}{n} \mathbf{X} \mathbf{1}_n&lt;/code&gt;$:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\bar{\mathbf{X} } = \mathbf{X} - \boldsymbol{\mu} \otimes \mathbf{1}_n^\top\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Whiten the data matrix $&lt;code&gt;\bar{\mathbf{X} }&lt;/code&gt;$ to remove correlations
between features:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{X}_\text{white} = \mathbf{D}^{-\frac{1}{2} } \mathbf{E}^\top \bar{\mathbf{X} }\;,&lt;/code&gt;$$&lt;/p&gt;
&lt;p&gt;where $&lt;code&gt;\mathbf{D}&lt;/code&gt;$ and $&lt;code&gt;\mathbf{E}&lt;/code&gt;$ are obtained
from the eigenvalue decomposition
$&lt;code&gt;\mathbf{C} = \bar{\mathbf{X} } \bar{\mathbf{X} }^\top = \mathbf{E} \mathbf{D} \mathbf{E}^\top&lt;/code&gt;$.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use an iterative algorithm (e.g., FastICA) to estimate the unmixing
matrix $&lt;code&gt;\mathbf{W}&lt;/code&gt;$ such that the components of
$&lt;code&gt;\mathbf{S} = \mathbf{W} \mathbf{X}_\text{white}&lt;/code&gt;$ are statistically
independent (not within the scope of this text!).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The independent components are given by:&lt;/p&gt;
&lt;p&gt;$$&lt;code&gt;\mathbf{S} = \mathbf{W} \mathbf{X}\;.&lt;/code&gt;$$&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;ICA is commonly used in applications such as blind source separation,
where the goal is to recover original signals from mixed observations
(e.g., separating audio sources recorded by multiple microphones).&lt;/p&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;/sup&gt;
&lt;p&gt;The “Kronecker product” of matrices $&lt;code&gt;\mathbf{A} = \begin{bmatrix} a_{1, 1}&amp;amp; a_{1, 2} \\ a_{2, 1} &amp;amp; a_{2, 2} \end{bmatrix}&lt;/code&gt;$ and $&lt;code&gt;\mathbf{B} = \begin{bmatrix} b_{1, 1}&amp;amp; b_{1, 2} \\ b_{2, 1} &amp;amp; b_{2, 2} \end{bmatrix}&lt;/code&gt;$ is defined as
$&lt;code&gt;\mathbf{A}\otimes\mathbf{B} := \begin{bmatrix} a_{1, 1}\mathbf{B}&amp;amp; a_{1, 2}\mathbf{B} \\ a_{2, 1}\mathbf{B} &amp;amp; a_{2, 2}\mathbf{B} \end{bmatrix}&lt;/code&gt;$.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;/sup&gt;
&lt;p&gt;A linear space means just what it sounds like — something that
looks like a line. For example, a line drawn on a piece of paper is
a $&lt;code&gt;1&lt;/code&gt;$-dimensional linear “sub-space” that lies in a higher
$&lt;code&gt;2&lt;/code&gt;$-dimensional space, the paper.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;/sup&gt;
&lt;p&gt;the orthogonal projection of some $&lt;code&gt;\mathbf{a}&lt;/code&gt;$ onto a “linear
sub-space” is basically the closest point $&lt;code&gt;\hat{\mathbf{a} }&lt;/code&gt;$ to
$&lt;code&gt;{\mathbf{a} }&lt;/code&gt;$ that lies on that linear sub-space&lt;/p&gt;
&lt;/div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Basic data structures &amp; algorithms in python</title>
        <published>2024-08-04T00:00:00+00:00</published>
        <updated>2024-08-04T00:00:00+00:00</updated>
        
        <author>
          <name>Uzair Akbar</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://uzairakbar.com/notes/dsa-templates/"/>
        <id>https://uzairakbar.com/notes/dsa-templates/</id>
        
        <content type="html" xml:base="https://uzairakbar.com/notes/dsa-templates/">&lt;h3 id=&quot;data-structures&quot;&gt;Data Structures&lt;/h3&gt;
&lt;h4 id=&quot;linked-list&quot;&gt;Linked List&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; ListNode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; __init__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; val&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; next&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;ListNode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;next&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; next&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; nextNode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;node&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;ListNode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;ListNode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    return&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;next&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; if&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; else&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pre&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; cur&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; nxt&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; head&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; nextNode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;head&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;stack&quot;&gt;Stack&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; Stack&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; __init__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; n&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; peek&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;peek&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note, this implementation uses &lt;code&gt;list&lt;/code&gt;, which is a python implementation of a dynamic array. Using a linked-list based &lt;code&gt;deque&lt;/code&gt; would be (slightly) better for memory (&lt;code&gt;O(2*N)&lt;/code&gt; worst-case vs. &lt;code&gt;O(N)&lt;/code&gt;) and time (&lt;code&gt;O(1)&lt;/code&gt; amortized vs. &lt;code&gt;O(1)&lt;/code&gt;).&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;from&lt;/span&gt;&lt;span&gt; collections&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; import&lt;/span&gt;&lt;span&gt; deque&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; deque&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;some_list&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; initialization, also accepts maxlen parameter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; stack&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;           #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; pop val from RIGHT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;         #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; append val to RIGHT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;stack&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;           #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; equivalent to above&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;queue&quot;&gt;Queue&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;from&lt;/span&gt;&lt;span&gt; collections&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; import&lt;/span&gt;&lt;span&gt; deque&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;queue&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; deque&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;some_list&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; initialization, also accepts maxlen parameter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;popleft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;       #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; pop val from LEFT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;         #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; append val to RIGHT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;           #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; equivalent to above&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;binary-tree&quot;&gt;Binary Tree&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; TreeNode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; __init__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;            self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;            val&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;            left&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;TreeNode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;None&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;            right&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Optional&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;TreeNode&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;left&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; left&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;right&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; right&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;heap&quot;&gt;Heap&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; min-heap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;heapq&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;heappush&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; heapq&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;heappop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; max-heap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;heapq&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;heappush&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; *&lt;/span&gt;&lt;span&gt; heapq&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;heappop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;prefix-tree-trie&quot;&gt;Prefix Tree/ Trie&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; TreeNode&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; __init__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;val&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;END&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; Trie&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; __init__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;        self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; TreeNode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; insert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; word&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; None&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        for&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; word&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            if&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; TreeNode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        if&lt;/span&gt;&lt;span&gt; END&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;END&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; TreeNode&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;END&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; search&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; word&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; bool&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        for&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; word&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            if&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;                return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        if&lt;/span&gt;&lt;span&gt; END&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; startsWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; prefix&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; bool&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        for&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; prefix&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            if&lt;/span&gt;&lt;span&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                node&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; node&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;children&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;                return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;algorithms&quot;&gt;Algorithms&lt;/h3&gt;
&lt;h4 id=&quot;array-search&quot;&gt;Array Search&lt;/h4&gt;
&lt;h5 id=&quot;binary-search&quot;&gt;Binary Search&lt;/h5&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; binarySearch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;number&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; arr&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt;int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; int&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    Time Complexity:    O(log(N))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    Space Complexity:   O(1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    left&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; right&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    while&lt;/span&gt;&lt;span&gt; left&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; right&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        mid&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;left&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span&gt; right&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        if&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;mid&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            right&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        elif&lt;/span&gt;&lt;span&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;mid&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; number&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            left&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            return&lt;/span&gt;&lt;span&gt; mid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;                               #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; not found&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;graph-search&quot;&gt;Graph Search&lt;/h4&gt;
&lt;h5 id=&quot;breadth-first-search&quot;&gt;Breadth First Search&lt;/h5&gt;
&lt;p&gt;Using the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://leetcode.com/problems/number-of-islands/description/&quot;&gt;number of islands problem&lt;/a&gt; as an example.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; numIslands&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; grid&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt;str&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; int&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        Time Complexity:    O(N)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        Space Complexity:   O(N)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        M&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;grid&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;grid&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        visited&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt; set&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;        #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; BFS to explore/visit current island&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; exploreIsland&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt; y&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            queue&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; deque&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            visited&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            while&lt;/span&gt;&lt;span&gt; queue&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;                            #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; until visited all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;                #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; x, y = queue.pop()                # use for DFS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;popleft&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                STEPS&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                ]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                neighbors&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span&gt; dx&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span&gt; dy&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; for&lt;/span&gt;&lt;span&gt; dx&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; dy&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; STEPS&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;                for&lt;/span&gt;&lt;span&gt; xn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; yn&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; neighbors&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;                    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; xn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; M&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; and&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;             #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; x_ in range&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;                        0&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; yn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; and&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;             #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; y_ in range&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        grid&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;xn&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;yn&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; and&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;     #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; land coordinate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;/span&gt;&lt;span&gt;xn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; yn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; visited&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;   #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; not visited&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        queue&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;xn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; yn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; make sure to visit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        visited&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;xn&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; yn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; )&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;     #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; mark when queued&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        islands&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        for&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; range&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;M&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            for&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; range&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;                if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;grid&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; not&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; in&lt;/span&gt;&lt;span&gt; visited&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    islands&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;                    exploreIsland&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span&gt; islands&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h5 id=&quot;depth-first-search&quot;&gt;Depth First Search&lt;/h5&gt;
&lt;p&gt;Use &lt;code&gt;stack&lt;/code&gt; instead of &lt;code&gt;queue&lt;/code&gt; (i.e. use &lt;code&gt;x, y = queue.pop()&lt;/code&gt; instead of &lt;code&gt;x, y = queue.popleft()&lt;/code&gt;) in BFS.&lt;/p&gt;
&lt;h4 id=&quot;sorting&quot;&gt;Sorting&lt;/h4&gt;
&lt;h5 id=&quot;merge-sort&quot;&gt;Merge Sort&lt;/h5&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; mergeSort&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-9&quot;&gt;arr&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt;int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-4&quot;&gt;int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    Time Complexity:    O(N*log(N))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    Space Complexity:   O(N)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-7&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    if&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span&gt; arr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;    else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        left_sorted&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; mergeSort&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;     #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; sort left&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        right_sorted&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; mergeSort&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;    #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; sort right&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        all_sorted&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt;                         #&lt;/span&gt;&lt;span class=&quot;z-l-2 z-l-i z-d-2 z-d-i&quot;&gt; merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        i&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        while&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;left_sorted&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt; and&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt; len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;right_sorted&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            if&lt;/span&gt;&lt;span&gt; left_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; right_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                all_sorted&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;left_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                i&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;            else&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                all_sorted&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-8&quot;&gt;append&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;right_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                j&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +=&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        all_sorted&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +=&lt;/span&gt;&lt;span&gt; left_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-3&quot;&gt; +&lt;/span&gt;&lt;span&gt; right_sorted&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-1&quot;&gt;        return&lt;/span&gt;&lt;span&gt; all_sorted&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The one sick child</title>
        <published>2023-08-04T00:00:00+00:00</published>
        <updated>2023-08-04T00:00:00+00:00</updated>
        
        <author>
          <name>Uzair Akbar</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://uzairakbar.com/notes/sick-child/"/>
        <id>https://uzairakbar.com/notes/sick-child/</id>
        
        <content type="html" xml:base="https://uzairakbar.com/notes/sick-child/">&lt;p&gt;It would have been unimaginable in the past that people would already be able to tell the gender of their children while still being in pregnancy. In a similar sense let us imagine for a moment a future where your family physician would be able to analyze your complete genetic makeup and tell you with absolute certainty whether the child(ren) you are about to conceive (before the egg has even been fertilized) would suffer from some terrible affliction or not (e.g. Hallopeau–Siemens syndrome). In this fictional world of ours, the doctor can also tell you the number of children you will conceive (twins, triplets, etc.).&lt;/p&gt;
&lt;p&gt;Now imagine you and your spouse are considering to have a baby. You visit the doctor to see if the baby you will eventually birth would be healthy or not. And to your dismay the doctor informs you that the baby would not be healthy.&lt;/p&gt;
&lt;p&gt;Now you must decide – given this piece of information would you have the child or not?&lt;/p&gt;
&lt;p&gt;Now imagine another scenario where when you visit the doctor the doctor tells you that you will be having twins! But unfortunately again, only one of them will be healthy.&lt;/p&gt;
&lt;p&gt;Again, would you have the twins or not?&lt;/p&gt;
&lt;p&gt;Now imagine a third case where the doctor tells you that you will be having triplets. But again, only two of them would be healthy.&lt;/p&gt;
&lt;p&gt;Would you have the triplets?&lt;/p&gt;
&lt;p&gt;You might have guessed where this is going. It doesn’t matter if people conceive one, three, or a hundred children, the decision of conception doesn’t change for most when one of them is guaranteed to be unhealthy.&lt;/p&gt;
&lt;p&gt;The people who find themselves saying no in all cases out of empathy for the one sick child may be surprised by the idea that there is virtually no difference between this fantasy world and the real world when amortized over their progeny – even if one’s children are perfectly healthy, the percentage of their progeny that will suffer from horrifying chronic conditions would always be non-zero in the limit.&lt;/p&gt;
&lt;p&gt;The question remains – would you conceive your progeny?&lt;/p&gt;
</content>
        
    </entry>
</feed>
