mirror of
https://github.com/janishutz/eth-summaries.git
synced 2025-11-25 10:34:23 +00:00
Move A&D summary
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
\newpage
|
||||
\subsubsection{Boruvka's algorithm}
|
||||
\begin{definition}[]{Definition of Borůvka's Algorithm}
|
||||
Borůvka's Algorithm is a greedy algorithm for finding the Minimum Spanning Tree (MST) of a connected, weighted graph. It repeatedly selects the smallest edge from each connected component and adds it to the MST, merging the components until only one component remains.
|
||||
\end{definition}
|
||||
|
||||
\begin{properties}[]{Characteristics and Performance of Borůvka's Algorithm}
|
||||
\begin{itemize}
|
||||
\item \textbf{Graph Type:} Works on undirected, weighted graphs.
|
||||
\item \textbf{Approach:} Greedy, component-centric.
|
||||
\item \textbf{Time Complexity:} \tct{(|V| + |E|) \log(|V|)}.
|
||||
\item \textbf{Space Complexity:} Depends on the graph representation, typically \tct{E + V}.
|
||||
\item \textbf{Limitations:} Efficient for parallel implementations but less commonly used in practice compared to Kruskal's and Prim's.
|
||||
\end{itemize}
|
||||
\end{properties}
|
||||
|
||||
\begin{algorithm}
|
||||
\caption{Borůvka's Algorithm}
|
||||
\begin{algorithmic}[1]
|
||||
\Procedure{Boruvka}{$G = (V, E)$}
|
||||
\State Initialize a forest $F$ with each vertex as a separate component.
|
||||
\State Initialize an empty MST $T$.
|
||||
\While{the number of components in $F > 1$}
|
||||
\State Initialize an empty set $minEdges$.
|
||||
\For{each component $C$ in $F$}
|
||||
\State Find the smallest edge $(u, v)$ such that $u \in C$ and $v \notin C$.
|
||||
\State Add $(u, v)$ to $minEdges$.
|
||||
\EndFor
|
||||
\For{each edge $(u, v)$ in $minEdges$}
|
||||
\If{$u$ and $v$ are in different components in $F$}
|
||||
\State Add $(u, v)$ to $T$.
|
||||
\State Merge the components containing $u$ and $v$ in $F$.
|
||||
\EndIf
|
||||
\EndFor
|
||||
\EndWhile
|
||||
\State \Return $T$.
|
||||
\EndProcedure
|
||||
\end{algorithmic}
|
||||
\end{algorithm}
|
||||
|
||||
\begin{usage}[]{Borůvka's Algorithm}
|
||||
Borůvka's algorithm finds the Minimum Spanning Tree (MST) by repeatedly finding the smallest outgoing edge from each connected component.
|
||||
|
||||
\begin{enumerate}
|
||||
\item \textbf{Initialize:}
|
||||
\begin{itemize}
|
||||
\item Assign each vertex to its own component.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Find Smallest Edges:}
|
||||
\begin{itemize}
|
||||
\item For each component, find the smallest outgoing edge. After combination, the other vertex will only be evaluated if it has an even lower weight edge outgoing from it.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Merge Components:}
|
||||
\begin{itemize}
|
||||
\item Add the selected edges to the MST.
|
||||
\item Merge the connected components of the graph.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Repeat:}
|
||||
\begin{itemize}
|
||||
\item Repeat steps 2-3 until only one component remains (all vertices are connected).
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{End:}
|
||||
\begin{itemize}
|
||||
\item The MST is complete, and all selected edges form a connected acyclic graph.
|
||||
\end{itemize}
|
||||
\end{enumerate}
|
||||
\end{usage}
|
||||
|
||||
|
||||
|
||||
% \newpage
|
||||
% \subsection{Example: Electrification of Möhrens}
|
||||
% \begin{figure*}[ht]
|
||||
% \centering
|
||||
% \begin{tikzpicture}
|
||||
% \tikzset{enclosed/.style={draw, circle, inner sep=0pt, minimum size =.5cm, circle}};
|
||||
% \node[enclosed, label={center: $\lightning$}] (power) at (0, 0) {};
|
||||
% \node[enclosed, label={center: A}] (A) at (-1, 1) {};
|
||||
% \node[enclosed, label={center: B}] (B) at (-1, 0) {};
|
||||
% \node[enclosed, label={center: C}] (C) at (-1, -1) {};
|
||||
% \node[enclosed, label={center: D}] (D) at (1, 1) {};
|
||||
% \node[enclosed, label={center: E}] (E) at (1, 0) {};
|
||||
% \node[enclosed, label={center: F}] (F) at (1, -1) {};
|
||||
%
|
||||
% \draw (A) -- node[left] {\scriptsize 3} ++ (B);
|
||||
% \draw (B) -- node[left] {\scriptsize 7} ++ (C);
|
||||
%
|
||||
% \draw (D) -- node[right] {\scriptsize 30} ++ (E);
|
||||
% \draw (E) -- node[right] {\scriptsize 25} ++ (F);
|
||||
% \draw plot[smooth] coordinates {(C) (1.5, -1.5) (1.5, 0.8) (D)};
|
||||
% \node[label={\scriptsize 20}] at (1.8, -1.8) {};
|
||||
%
|
||||
%
|
||||
% \draw (A) -- node[right] {\scriptsize 10} ++ (power);
|
||||
% \draw (B) -- node[above,yshift=-0.1cm] {\scriptsize 5} ++ (power);
|
||||
% \draw (C) -- node[right] {\scriptsize 15} ++ (power);
|
||||
% \draw (D) -- node[left] {\scriptsize 40} ++ (power);
|
||||
% \draw (E) -- node[above,yshift=-0.1cm] {\scriptsize 0} ++ (power);
|
||||
% \draw (F) -- node[left] {\scriptsize 35} ++ (power);
|
||||
% \end{tikzpicture}
|
||||
% \end{figure*}
|
||||
%
|
||||
% \fhlc{Aquamarine}{Goal:} Find sub-graph for which the weights are minimal.
|
||||
%
|
||||
% $G = (V, E)$ is non-directed, connected.
|
||||
%
|
||||
% \fhlc{lightgray}{Weights of edges:} $\{w(e)\}\rvert_{e \in E}$ (non-negative)\\[0.2cm]
|
||||
% \fhlc{lightgray}{Spanning edges $A\subseteq E$} Graph $(V, A)$ is connected\\[0.2cm]
|
||||
% \fhlc{lightgray}{Weight:} $w(A) := \sum_{e \in A} w(e)$\\[0.2cm]
|
||||
@@ -0,0 +1,134 @@
|
||||
\newpage
|
||||
\subsubsection{Kruskal's algorithm}
|
||||
\begin{definition}[]{Kruskal's Algorithm}
|
||||
Kruskal's Algorithm is a greedy algorithm for finding the Minimum Spanning Tree (MST) of a connected, weighted graph. It sorts all the edges by weight and adds them to the MST in order of increasing weight, provided they do not form a cycle with the edges already included.
|
||||
\end{definition}
|
||||
|
||||
\begin{properties}[]{Characteristics and Performance}
|
||||
\begin{itemize}
|
||||
\item \textbf{Graph Type:} Works on undirected, weighted graphs.
|
||||
\item \textbf{Approach:} Greedy, edge-centric.
|
||||
\item \textbf{Time Complexity:} \tco{|E| \log (|E|)} (for sort), \tco{|V| \log(|V|)} (for union find data structure).\\
|
||||
\timecomplexity \tco{|E| \log(|E|) + |V| \log(|V|)}
|
||||
\item \textbf{Space Complexity:} Depends on the graph representation, typically \tct{E + V}.
|
||||
\item \textbf{Limitations:} Requires sorting of edges, which can dominate runtime.
|
||||
\end{itemize}
|
||||
\end{properties}
|
||||
|
||||
\begin{algorithm}
|
||||
\caption{Kruskal's Algorithm}
|
||||
\begin{algorithmic}[1]
|
||||
\Procedure{Kruskal}{$G = (V, E)$}
|
||||
\State Sort all edges $E$ in non-decreasing order of weight.
|
||||
\State Initialize an empty MST $T$.
|
||||
\State Initialize a disjoint-set data structure for $V$.
|
||||
\For{each edge $(u, v)$ in $E$ (in sorted order)}
|
||||
\If{$u$ and $v$ belong to different components in the disjoint set}
|
||||
\State Add $(u, v)$ to $T$.
|
||||
\State Union the sets containing $u$ and $v$.
|
||||
\EndIf
|
||||
\EndFor
|
||||
\State \Return $T$.
|
||||
\EndProcedure
|
||||
\end{algorithmic}
|
||||
\end{algorithm}
|
||||
|
||||
\begin{usage}[]{Kruskal's Algorithm}
|
||||
Kruskal's algorithm finds the Minimum Spanning Tree (MST) by sorting edges and adding them to the MST, provided they don't form a cycle.
|
||||
|
||||
\begin{enumerate}
|
||||
\item \textbf{Sort Edges:}
|
||||
\begin{itemize}
|
||||
\item Sort all edges in ascending order by their weights.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Initialize Disjoint Sets (union find):}
|
||||
\begin{itemize}
|
||||
\item Assign each vertex to its own disjoint set to track connected components.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Iterate Over Edges:}
|
||||
\begin{itemize}
|
||||
\item For each edge in the sorted list:
|
||||
\begin{itemize}
|
||||
\item Check if the edge connects vertices from different sets.
|
||||
\item If it does, add the edge to the MST and merge the sets.
|
||||
\end{itemize}
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Stop When Done:}
|
||||
\begin{itemize}
|
||||
\item Stop when the MST contains \(n-1\) edges (for a graph with \(n\) vertices).
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{End:}
|
||||
\begin{itemize}
|
||||
\item The MST is complete, and all selected edges form a connected acyclic graph.
|
||||
\end{itemize}
|
||||
\end{enumerate}
|
||||
\end{usage}
|
||||
|
||||
|
||||
\newpage
|
||||
\fhlc{Aquamarine}{Union-Find}
|
||||
\begin{usage}[]{Union-Find Data Structure - Step-by-Step Execution}
|
||||
The Union-Find data structure efficiently supports two primary operations for disjoint sets:
|
||||
\begin{itemize}
|
||||
\item \textbf{Union:} Merge two sets into one.
|
||||
\item \textbf{Find:} Identify the representative (or root) of the set containing a given element.
|
||||
\end{itemize}
|
||||
|
||||
\textbf{Steps for Using the Union-Find Data Structure:}
|
||||
|
||||
\begin{enumerate}
|
||||
\item \textbf{Initialization:}
|
||||
\begin{itemize}
|
||||
\item Create an array \(parent\), where \(parent[i] = i\), indicating that each element is its own parent (a singleton set).
|
||||
\item Optionally, maintain a \(rank\) array to track the rank (or size) of each set for optimization.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Find Operation:}
|
||||
\begin{itemize}
|
||||
\item To find the representative (or root) of a set containing element \(x\):
|
||||
\begin{enumerate}
|
||||
\item Follow the \(parent\) array recursively until \(parent[x] = x\).
|
||||
\item Apply \textbf{path compression} by updating \(parent[x]\) directly to the root to flatten the tree structure:
|
||||
\[
|
||||
parent[x] = \text{Find}(parent[x])
|
||||
\]
|
||||
\end{enumerate}
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Union Operation:}
|
||||
\begin{itemize}
|
||||
\item To merge the sets containing \(x\) and \(y\):
|
||||
\begin{enumerate}
|
||||
\item Find the roots of \(x\) and \(y\) using the Find operation.
|
||||
\item Compare their ranks:
|
||||
\begin{itemize}
|
||||
\item Attach the smaller tree under the larger tree to keep the structure shallow.
|
||||
\item If ranks are equal, arbitrarily choose one as the root and increment its rank.
|
||||
\end{itemize}
|
||||
\end{enumerate}
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Optimization Techniques:}
|
||||
\begin{itemize}
|
||||
\item \textbf{Path Compression:} Flattens the tree during Find operations, reducing the time complexity.
|
||||
\item \textbf{Union by Rank/Size:} Ensures smaller trees are always attached under larger trees, maintaining a logarithmic depth.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{End:}
|
||||
\begin{itemize}
|
||||
\item After performing Find and Union operations, the Union-Find structure can determine connectivity between elements or group them into distinct sets efficiently.
|
||||
\end{itemize}
|
||||
\end{enumerate}
|
||||
\end{usage}
|
||||
|
||||
\begin{properties}[]{Performance}
|
||||
\begin{itemize}
|
||||
\item \textsc{make}$(V)$: Initialize data structure \tco{n}
|
||||
\item \textsc{same}$(u, v)$: Check if two components belong to the same set \tco{1} or \tco{n}, depending on if the representant is stored in an array or not
|
||||
\item \textsc{union}$(u, v)$: Combine two sets, \tco{\log(n)}, in Kruskal we call this \tco{n} times, so total number (amortised) is \tco{n \log(n)}
|
||||
\end{itemize}
|
||||
\end{properties}
|
||||
@@ -0,0 +1,83 @@
|
||||
\newpage
|
||||
\subsection{MST}
|
||||
\subsubsection{Prim's algorithm}
|
||||
\begin{definition}[]{Definition of Prim's Algorithm}
|
||||
Prim's Algorithm is a greedy algorithm for finding the Minimum Spanning Tree (MST) of a connected, weighted graph.
|
||||
It starts with an arbitrary node and iteratively adds the smallest edge connecting a vertex in the MST to a vertex outside the MST until all vertices are included.
|
||||
\end{definition}
|
||||
|
||||
\begin{properties}[]{Characteristics and Performance of Prim's Algorithm}
|
||||
\begin{itemize}
|
||||
\item \textbf{Graph Type:} Works on undirected, weighted graphs.
|
||||
\item \textbf{Approach:} Greedy, vertex-centric.
|
||||
\item \textbf{Time Complexity:}
|
||||
\begin{itemize}
|
||||
\item With an adjacency matrix: \tct{V^2}.
|
||||
\item With a priority queue and adjacency list: \tct{(|V| + |E|) \log(|V|)}.
|
||||
\end{itemize}
|
||||
\item \textbf{Space Complexity:} Depends on the graph representation, typically \tct{E + V}.
|
||||
\item \textbf{Limitations:} Less efficient than Kruskal's for sparse graphs using adjacency matrices.
|
||||
\end{itemize}
|
||||
\end{properties}
|
||||
|
||||
\begin{algorithm}
|
||||
\caption{Prim's Algorithm}
|
||||
\begin{algorithmic}[1]
|
||||
\Procedure{Prim}{$G = (V, E)$, $start$}
|
||||
\State Initialize a priority queue $Q$.
|
||||
\State Initialize $key[v] \gets \infty$ for all $v \in V$, except $key[start] \gets 0$.
|
||||
\State Initialize an empty MST $T$.
|
||||
\State Add all vertices to $Q$ with their key values.
|
||||
\While{$Q$ is not empty}
|
||||
\State $u \gets$ ExtractMin($Q$).
|
||||
\State Add $u$ to $T$.
|
||||
\For{each $(u, v)$ in $E$}
|
||||
\If{$v$ is in $Q$ and weight($u, v$) $< key[v]$}
|
||||
\State $key[v] \gets$ weight($u, v$).
|
||||
\State Update $Q$ with $key[v]$.
|
||||
\EndIf
|
||||
\EndFor
|
||||
\EndWhile
|
||||
\State \Return $T$.
|
||||
\EndProcedure
|
||||
\end{algorithmic}
|
||||
\end{algorithm}
|
||||
|
||||
\begin{usage}[]{Prim's Algorithm}
|
||||
Prim's algorithm is used to find the Minimum Spanning Tree (MST) of a graph. It starts with a single node and grows the MST by adding the smallest edge that connects a vertex in the MST to a vertex outside it.
|
||||
|
||||
\begin{enumerate}
|
||||
\item \textbf{Initialize:}
|
||||
\begin{itemize}
|
||||
\item Pick any starting vertex as part of the MST.
|
||||
\item Mark the vertex as visited and add it to the MST.
|
||||
\item Initialize a priority queue to store edges by their weight.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Explore Edges:}
|
||||
\begin{itemize}
|
||||
\item Add all edges from the visited vertex to the priority queue.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Pick the Smallest Edge:}
|
||||
\begin{itemize}
|
||||
\item Choose the smallest-weight edge from the priority queue that connects a visited vertex to an unvisited vertex.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Add the New Vertex:}
|
||||
\begin{itemize}
|
||||
\item Mark the vertex connected by the chosen edge as visited.
|
||||
\item Add the edge and the vertex to the MST.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{Repeat:}
|
||||
\begin{itemize}
|
||||
\item Repeat steps 2-4 until all vertices are part of the MST or no more edges are available.
|
||||
\end{itemize}
|
||||
|
||||
\item \textbf{End:}
|
||||
\begin{itemize}
|
||||
\item The MST is complete when all vertices are visited, and the selected edges form a connected acyclic graph.
|
||||
\end{itemize}
|
||||
\end{enumerate}
|
||||
\end{usage}
|
||||
Reference in New Issue
Block a user