# How to plot a network flow with tikz?

I would like to produce exactly the same picture shown next.

I started by doing this

\documentclass[twocolumn, 12pt]{article}

\usepackage[latin9]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage[margin=1in]{geometry}
\usetikzlibrary{tikzmark,calc,matrix,positioning}
\usepackage{pgfplots}

\usetikzlibrary{calc,fit,shapes.geometric}
\pgfdeclarelayer{signal}
\pgfsetlayers{signal,main}
\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows}% To get more arrow heads
\tikzstyle{printersafe}=[snake=snake,segment amplitude=0 pt]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\sloppy
\pgfplotsset{compat=1.12}

\begin{document}

\begin{figure}[ht!]
\centering
\resizebox{.5\textwidth}{!}{%
\begin{tikzpicture}%[>=triangle 45]
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c1) at (0,0) {};
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c2) at (1,0) {};
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c3) at (0,1) {};
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c4) at (1,1) {};
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c5) at (-0.5,0.5) {$s$};
\node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
sep=0pt,minimum size=2pt] (c6) at (1.5,0.5) {$t$};

\draw [->] (c1) -- (c2);
\draw [->] (c5) -- (c1);
\draw [->] (c5) -- (c3);
\end{tikzpicture}
}
\caption{Model}
\label{fig:sys}
\end{figure}
\end{document}


but this gives me something weird.

This figure clearly does not produce the desired result. Can you help me?

• Why is it weird? Most of the nodes have no text in them (the $v_n$ nodes). And the inner sep defines the "padding" between the node content and its border. – Torbjørn T. Dec 1 '16 at 22:05
• Because I would like to produce the same thing but I did not succeed. (It is weird compared to what I would like to have not to what I wrote.) – Ribz Dec 1 '16 at 22:12
• The text $s$ and $t$ are not as they are given in the original figure. They are big. How to make them small? – Ribz Dec 1 '16 at 22:13
• Also, the arrows are not the same. How can I get the same shapes? – Ribz Dec 1 '16 at 22:14
• Yes, I see that they are different, I just thought "weird" was a weird choice of word. – Torbjørn T. Dec 1 '16 at 22:15

This is fairly close. Some notes: - I removed everything unnecessary from the preamble, and also the figure environment and the resizebox. ( personally I think resizebox should be the last thing you do for scaling a tikz diagram, even if it is very simple).

• The reason the node text is "large" compared to the node itself in your code is that you have inner sep=0pt,minimum size=2pt. The inner sep defines the distance from the node content to the border, and the node content is always larger than 2pt. I kept the inner sep value, but increased the minimum size to 20pt.

• Nodes are positioned relative to each other. For example, the v_2 node is placed above right=of c1, where c1 is the name of the s node. Benefits of this is that

1. you don't have to worry about absolute coordinates;
2. it becomes very easy to modify the distance between nodes: just change the values for the node distance. In node distance=0.6cm and 1.2cm the first length is the vertical distance, the second is the horizontal.
• the arrows.meta library is a replacement for the arrows library, and gives you a lot of potential for customizing arrow heads if needed (see the manual)

• The fill opacity applied to the text as well. You could remove the fill opacity, and set fill=gray!30 instead of just gray. I instead added text opacity=1, which overrides the opacity value from the fill.

• I defined a mycircle style to avoid repetition.

• Lastly I used a \foreach loop to draw the arrows. You could also have multiple \draw [myarrow] (<node 1>) node[above] {<number>} (<node 2>);, but with the loop you get less repetition.

• To draw the two parallel arrows I used the angle anchor syntax. E.g. nodename.60 is the point at the border of the node that is at an angle of 60 degrees from the horizontal.

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}[
mycircle/.style={
circle,
draw=black,
fill=gray,
fill opacity = 0.3,
text opacity=1,
inner sep=0pt,
minimum size=20pt,
font=\small},
myarrow/.style={-Stealth},
node distance=0.6cm and 1.2cm
]
\node[mycircle] (c1) {$s$};
\node[mycircle,below right=of c1] (c2) {$v_2$};
\node[mycircle,right=of c2] (c3) {$v_4$};
\node[mycircle,above right=of c1] (c4) {$v_1$};
\node[mycircle,right=of c4] (c5) {$v_3$};
\node[mycircle,below right=of c5] (c6) {$t$};

\foreach \i/\j/\txt/\p in {% start node/end node/text/position
c1/c2/8/below,
c1/c4/11/above,
c2/c3/11/below,
c3/c6/4/below,
c4/c5/12/above,
c5/c6/15/above,
c5/c2/4/below,
c3/c5/7/below,
c2.70/c4.290/1/below}
\draw [myarrow] (\i) -- node[sloped,font=\small,\p] {\txt} (\j);

% draw this outside loop to get proper orientation of 10
\draw [myarrow] (c4.250) -- node[sloped,font=\small,above,rotate=180] {10} (c2.110);
\end{tikzpicture}
\end{document}