mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-25 22:38:04 +02:00
added Pseudocode example
This commit is contained in:
parent
b84cd8bbdc
commit
a9a8042403
3 changed files with 239 additions and 0 deletions
|
@ -0,0 +1,120 @@
|
|||
\usepackage{tikz}
|
||||
\usetikzlibrary{calc}
|
||||
\usepackage{xcolor}
|
||||
\makeatletter
|
||||
|
||||
% to change colors
|
||||
\newcommand{\fillcol}{green!20}
|
||||
\newcommand{\bordercol}{black}
|
||||
|
||||
% code from Andrew Stacey (with small adjustment to the border color)
|
||||
% http://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package
|
||||
\newcounter{jumping}
|
||||
\resetcounteronoverlays{jumping}
|
||||
|
||||
\def\jump@setbb#1#2#3{%
|
||||
\@ifundefined{jump@#1@maxbb}{%
|
||||
\expandafter\gdef\csname jump@#1@maxbb\endcsname{#3}%
|
||||
}{%
|
||||
\csname jump@#1@maxbb\endcsname
|
||||
\pgf@xa=\pgf@x
|
||||
\pgf@ya=\pgf@y
|
||||
#3
|
||||
\pgfmathsetlength\pgf@x{max(\pgf@x,\pgf@xa)}%
|
||||
\pgfmathsetlength\pgf@y{max(\pgf@y,\pgf@ya)}%
|
||||
\expandafter\xdef\csname jump@#1@maxbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
|
||||
}
|
||||
\@ifundefined{jump@#1@minbb}{%
|
||||
\expandafter\gdef\csname jump@#1@minbb\endcsname{#2}%
|
||||
}{%
|
||||
\csname jump@#1@minbb\endcsname
|
||||
\pgf@xa=\pgf@x
|
||||
\pgf@ya=\pgf@y
|
||||
#2
|
||||
\pgfmathsetlength\pgf@x{min(\pgf@x,\pgf@xa)}%
|
||||
\pgfmathsetlength\pgf@y{min(\pgf@y,\pgf@ya)}%
|
||||
\expandafter\xdef\csname jump@#1@minbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
|
||||
}
|
||||
}
|
||||
|
||||
\tikzset{%
|
||||
remember picture with id/.style={%
|
||||
remember picture,
|
||||
overlay,
|
||||
draw=\bordercol,
|
||||
save picture id=#1,
|
||||
},
|
||||
save picture id/.code={%
|
||||
\edef\pgf@temp{#1}%
|
||||
\immediate\write\pgfutil@auxout{%
|
||||
\noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
|
||||
},
|
||||
if picture id/.code args={#1#2#3}{%
|
||||
\@ifundefined{save@pt@#1}{%
|
||||
\pgfkeysalso{#3}%
|
||||
}{
|
||||
\pgfkeysalso{#2}%
|
||||
}
|
||||
},
|
||||
onslide/.code args={<#1>#2}{%
|
||||
\only<#1>{\pgfkeysalso{#2}}%
|
||||
},
|
||||
alt/.code args={<#1>#2#3}{%
|
||||
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
|
||||
},
|
||||
stop jumping/.style={
|
||||
execute at end picture={%
|
||||
\stepcounter{jumping}%
|
||||
\immediate\write\pgfutil@auxout{%
|
||||
\noexpand\jump@setbb{\the\value{jumping}}{\noexpand\pgfpoint{\the\pgf@picminx}{\the\pgf@picminy}}{\noexpand\pgfpoint{\the\pgf@picmaxx}{\the\pgf@picmaxy}}
|
||||
},
|
||||
\csname jump@\the\value{jumping}@maxbb\endcsname
|
||||
\path (\the\pgf@x,\the\pgf@y);
|
||||
\csname jump@\the\value{jumping}@minbb\endcsname
|
||||
\path (\the\pgf@x,\the\pgf@y);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
\def\savepointas#1#2{%
|
||||
\expandafter\gdef\csname save@pt@#1\endcsname{#2}%
|
||||
}
|
||||
|
||||
\def\tmk@labeldef#1,#2\@nil{%
|
||||
\def\tmk@label{#1}%
|
||||
\def\tmk@def{#2}%
|
||||
}
|
||||
|
||||
\tikzdeclarecoordinatesystem{pic}{%
|
||||
\pgfutil@in@,{#1}%
|
||||
\ifpgfutil@in@%
|
||||
\tmk@labeldef#1\@nil
|
||||
\else
|
||||
\tmk@labeldef#1,\pgfpointorigin\@nil
|
||||
\fi
|
||||
\@ifundefined{save@pt@\tmk@label}{%
|
||||
\tikz@scan@one@point\pgfutil@firstofone\tmk@def
|
||||
}{%
|
||||
\pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
|
||||
\pgfsys@getposition{\pgfpictureid}\save@this@pic%
|
||||
\pgf@process{\pgfpointorigin\save@this@pic}%
|
||||
\pgf@xa=\pgf@x
|
||||
\pgf@ya=\pgf@y
|
||||
\pgf@process{\pgfpointorigin\save@orig@pic}%
|
||||
\advance\pgf@x by -\pgf@xa
|
||||
\advance\pgf@y by -\pgf@ya
|
||||
}%
|
||||
}
|
||||
\newcommand\tikzmark[2][]{%
|
||||
\tikz[remember picture with id=#2] #1;}
|
||||
\makeatother
|
||||
|
||||
\resetcounteronoverlays{algocf}
|
||||
|
||||
\newcommand<>{\boxto}[1]{%
|
||||
\only#2{\tikz[remember picture with id=#1]
|
||||
\draw[line width=1pt,fill=\fillcol,rectangle,rounded corners]
|
||||
(pic cs:#1) ++(5.2,-.1) rectangle (-0.4,0)
|
||||
;\tikz\node [anchor=base] (#1){};}% <= insertion to store the anchor to be used as based for the annotation
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
\documentclass{article}
|
||||
\usepackage[pdftex,active,tightpage]{preview}
|
||||
\setlength\PreviewBorder{2mm}
|
||||
|
||||
\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
|
||||
\usepackage{braket} % needed for \Set
|
||||
\usepackage{algorithm,algpseudocode}
|
||||
|
||||
\begin{document}
|
||||
\begin{preview}
|
||||
\begin{itemize}
|
||||
\item $c:E \rightarrow \mathbb{R}_0^+$: capacity of an edge
|
||||
\item $e: V \rightarrow \mathbb{R}_0^+$: excess (too much flow in one node)
|
||||
\item $r_f: V \times V \rightarrow \mathbb{R}, \; r_f(u,v) := c(u,v) - f(u,v) $: remaining capacity
|
||||
\item $dist: V \rightarrow \mathbb{N}$: the label (imagine this as height)
|
||||
\end{itemize}
|
||||
|
||||
\begin{algorithm}[H]
|
||||
\begin{algorithmic}
|
||||
\Function{PushRelabel}{Network $N(D, s, t, c)$}
|
||||
\ForAll{$(v,w) \in (V \times V \setminus E)$} \Comment{If an edge is not in $D=(V,E)$,}
|
||||
\State $c(v,w) \gets 0$ \Comment{then its capacity is 0}
|
||||
\EndFor
|
||||
\\
|
||||
\ForAll{$(v,w) \in V \times V$} \Comment{At the beginning, every edge}
|
||||
\State $f(v,w) \gets 0$ \Comment{has flow=0}
|
||||
\State $r_f(v,w) \gets c(v,w)$ \Comment{flow=max in the residualgraph}
|
||||
\EndFor
|
||||
\\
|
||||
\State $dist(s) \gets |V|$
|
||||
|
||||
\ForAll{$v \in V \setminus \Set{s}$}
|
||||
\State $f(s,v) \gets c(s,v)$ \Comment{Push maximum flow out at the beginning}
|
||||
\State $r(v,s) \gets c(v,s) - f(v,s)$
|
||||
\State $dist(v) \gets 0$
|
||||
\State $e(v) \gets c(s,v)$ \Comment{$v$ has to much flow}
|
||||
\EndFor
|
||||
\\
|
||||
\While{$\exists v \in V:$ \Call{isActive}{v}}
|
||||
\If{\Call{isPushOk}{v}}
|
||||
\State \Call{Push}{v}
|
||||
\ElsIf{\Call{isRelabelOk}{v}}
|
||||
\State \Call{Relabel}{v}
|
||||
\EndIf
|
||||
\EndWhile
|
||||
\\
|
||||
\State \Return $f$ \Comment{Maximaler Fluss}
|
||||
\EndFunction
|
||||
\\
|
||||
\Function{Push}{Graph $D$, Flow $f$, Node $v$, Node $w$}
|
||||
\State $\Delta \gets \min\Set{e(v), r_f(v,w)}$
|
||||
\State $f(v,w) \gets f(v,w) + \Delta$
|
||||
\State $f(w,v) \gets f(w,v) - \Delta$
|
||||
\State $r_f(v,w) \gets r_f(v,w) - \Delta$
|
||||
\State $r_f(w,v) \gets r_f(w,v) + \Delta$
|
||||
\State $e(v) \gets e(v) - \Delta$
|
||||
\State $e(w) \gets e(w) + \Delta$
|
||||
\EndFunction
|
||||
\\
|
||||
\Function{Relabel}{Node $v$}
|
||||
\If{$\Set{w|r_f(v,w) > 0} == \emptyset$}
|
||||
\State $dist(v) \gets \infty$
|
||||
\Else
|
||||
\State $dist(v) \gets \min\Set{dist(w)+1|w \in V: r_f(v,w) > 0}$
|
||||
\EndIf
|
||||
\EndFunction
|
||||
\\
|
||||
\Function{isActive}{Node v}
|
||||
\State\Return $(e(v) > 0) \land (dist(v) < \infty)$
|
||||
\EndFunction
|
||||
\\
|
||||
\Function{isRelabelOk}{Node v}
|
||||
\State\Return \Call{isActive}{v} $\land (\forall w \in \Set{r_f(v,w) >0}: dist(v) \leq dist(w))$
|
||||
\EndFunction
|
||||
\\
|
||||
\Function{isPushOk}{Node v}
|
||||
\State\Return \Call{isActive}{v} $\land (r_f > 0) \land (dist(v) == dist(w)+1)$
|
||||
\EndFunction
|
||||
\end{algorithmic}
|
||||
\caption{Algorithm of Goldberg and Tarjan}
|
||||
\label{alg:seq1}
|
||||
\end{algorithm}
|
||||
\end{preview}
|
||||
\end{document}
|
35
source-code/Pseudocode/Goldberg-Tarjan-Push-Relabel/Makefile
Normal file
35
source-code/Pseudocode/Goldberg-Tarjan-Push-Relabel/Makefile
Normal file
|
@ -0,0 +1,35 @@
|
|||
SOURCE = Goldberg-Tarjan-Push-Relabel
|
||||
DELAY = 80
|
||||
DENSITY = 300
|
||||
WIDTH = 500
|
||||
|
||||
make:
|
||||
pdflatex $(SOURCE).tex -output-format=pdf
|
||||
make clean
|
||||
|
||||
clean:
|
||||
rm -rf $(TARGET) *.class *.html *.log *.aux *.data *.gnuplot
|
||||
|
||||
gif:
|
||||
pdfcrop $(SOURCE).pdf
|
||||
convert -verbose -delay $(DELAY) -loop 0 -density $(DENSITY) $(SOURCE)-crop.pdf $(SOURCE).gif
|
||||
make clean
|
||||
|
||||
png:
|
||||
make
|
||||
make svg
|
||||
inkscape $(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png
|
||||
|
||||
transparentGif:
|
||||
convert $(SOURCE).pdf -transparent white result.gif
|
||||
make clean
|
||||
|
||||
svg:
|
||||
make
|
||||
#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
|
||||
pdf2svg $(SOURCE).pdf $(SOURCE).svg
|
||||
# Necessary, as pdf2svg does not always create valid svgs:
|
||||
inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
|
||||
rsvg-convert -a -w $(WIDTH) -f svg $(SOURCE).svg -o $(SOURCE)2.svg
|
||||
inkscape $(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg
|
||||
rm $(SOURCE)2.svg
|
Loading…
Add table
Add a link
Reference in a new issue