\pgfdeclarelayer{background}
\pgfdeclarelayer{demand}
\pgfdeclarelayer{marked}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,demand,marked,main,foreground}

\tikzstyle{every node} = [font=\normalsize]

\tikzstyle{edge} = [thick]
\tikzstyle{marked edge} = [edge, line width=3pt, tumorange]
\tikzstyle{directed} = [arrows={-latex}, shorten >=1pt]
\tikzstyle{node on edge} = [fill=white, circle, inner sep=1pt, font=\footnotesize]

\tikzstyle{flow graph} = [x=4.5em, y=4em]
\tikzstyle{flow node} = [circle, draw, thick, fill=tumblue!20, minimum size=6pt, inner sep=0pt]
\tikzstyle{flow edge} = [edge]
\tikzstyle{demand edge} = [edge, line width=4pt, arrows={-latex}, dash pattern= on 5pt off 3pt, tumred!50]
\tikzstyle{tree edge} = [marked edge, tumgreen]
\tikzstyle{flow capacity} = [node on edge]
\tikzstyle{flow demand} = [node on edge, text=tumred]

\tikzstyle{highlight} = [draw=tumorange, very thick, rounded corners]

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {};}

\newcommand{\flownodes}{%
    \pgfmathsetseed{42}
    \def\jiggliness{0.2}
    \def\jiggle{++ (rand * \jiggliness, rand * \jiggliness)}

    \path[use as bounding box] (-4, 2.7) rectangle (3.5,6.15);

    \foreach \pos/\name in {
        % {(0,1)/a},
        {(-0.5,2.75)/b},
        {(0.5,3.25)/c},
        {(0.75,4)/d},
        {(-0.5,4.5)/e},
        {(-1.25,3.75)/f},
        {(1.5,4.75)/g},
        {(0,5.5)/h},
        {(2.5,5.5)/i},
        {(3.5,3.5)/j},
        {(2.25,4)/k},
        {(-2,6)/l},
        {(-2.5,5)/m},
        {(-4,5)/n}}
    \draw \pos\jiggle node[flow node] (\name) {};
    % \draw \pos\jiggle node[flow node, label={\name}, font=\footnotesize] (\name) {};
}
\newcommand{\flowedges}[1][]{%
    \foreach \source/\dest in {
        % {a/b}, {a/n}, {a/j},
        {n/b}, {b/j}, {f/b}, {c/b},
        {c/d}, {e/d}, {e/f}, {g/i},
        {h/e}, {h/i}, {i/j}, {k/j},
        {l/h}, {l/m}, {n/m},
        {c/k}, {k/i}, {g/d}, {m/e}, {n/l},
        {m/f}, {f/c}, {d/k}, {e/g}, {c/e}}
    \draw[flow edge, #1] (\source) -- (\dest);
}
\newcommand{\flowcapacities}[1][]{%
    \foreach \source/\dest/\capacity in {
        % {a/b}, {a/n}, {a/j},
        {n/b/2}, {b/j/10}, {c/b/3}, {f/b/4},
        {c/d/4}, {e/d/2}, {e/f/4}, {g/i/2},
        {h/e/1}, {h/i/2}, {i/j/6}, {k/j/4},
        {l/h/3}, {l/m/4}, {n/m/5},
        {c/k/4}, {k/i/3}, {g/d/1}, {m/e/4}, {n/l/6},
        {m/f/1}, {f/c/2}, {d/k/4}, {e/g/1}, {c/e/5}}
    \path (\source) -- node[flow capacity, #1]{\capacity} (\dest);
}
\newcommand{\flowdemands}{%
    \begin{pgfonlayer}{demand}
        \foreach \source/\dest/\demand/\pos/\bendage in {
            {n/j/10/0.2/right},
            {h/j/8/0.2/left},
            {e/g/1/0.7/left},
            {n/h/12/0.7/left},
            {k/m/5/0.8/right}}
        % \draw[demand edge, bend \bendage=15] (\source) edge node[flow demand, pos=\pos]{\demand} (\dest);
        \draw[demand edge, bend \bendage=15] (\source) edge (\dest);
    \end{pgfonlayer}
}
\newcommand{\flowtreeedges}[1][]{%
    \begin{pgfonlayer}{marked}
        \foreach \source/ \dest in {
            % {a/b},
            {c/b},
            {c/d}, {e/d}, {e/f}, {g/i},
            {m/e}, {d/k}, {i/j}, {k/j},
            {h/i}, {l/m}, {n/m}}
        \draw [tree edge, #1] (\source.center) -- (\dest.center);
    \end{pgfonlayer}
}
\newcommand{\flowtreeedgestwo}[1][]{%
    \begin{pgfonlayer}{marked}
        \foreach \source/ \dest in {
            {n/l}, {l/h}, {h/i}, {i/g}, {i/j},
            {j/k}, {g/d}, {d/c}, {g/e}, {c/f},
            {f/m}, {f/b}}
        \draw[tree edge, #1] (\source.center) -- (\dest.center);
    \end{pgfonlayer}
}
