Despite the many attempts to draw this simple flowchart, I didn't really know how to do the following:
-
Drawing a start arrow (as the one before A1)
-
Drawing an end arrow (as the one after A2 and A7)
-
Having the structure of the flow between nodes of a diamond shape (as in the arrows connecting A2, A3, A4, and A5)
-
Drawing a "smart" returning arrow that doesn't penetrate other nodes (as in between A6 and A3)
-
Adjusting the length of the arrow (or the distance between nodes)
-
Adjusting the vertical distance between nodes
-
Putting the diagram in the center
Here is my code:
\documentclass{article}
\usepackage{tikz} % For drawing circles around numbers
\usetikzlibrary{shapes,arrows}
\begin{document}
\tikzstyle{decision} = [draw, text width=6.0em, text badly centered, node distance=3cm, inner sep=0pt, minimum height=2.0em]
\tikzstyle{block} = [rectangle, draw, text centered, rounded corners, minimum height=2.0em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=3cm, minimum height=2em]
\begin{center}
\begin{figure}[H]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [decision] (init) {Initialize};
\node [block, below of=init] (trivial) {$N = 1$?};
\node [decision, right of=trivial] (divide) {Divide};
\node [decision, below of=divide] (factorFound) {Factor found};
\node [block, right of=divide, node distance=3cm] (zeroRem){Zero remainder?};
\node [block, right of=zeroRem, node distance=3cm] (lowQuot)
{Low quotient?};
\node [decision, below of=lowQuot] (prime) {$N$ is prime};
% Draw edges
\path [line] (init) -- (trivial);
\path [line] (trivial) -- node {No}(divide);
\path [line] (divide) -- (zeroRem);
\path [line] (zeroRem) -- node {No}(lowQuot);
\path [line] (zeroRem) -- node {Yes}(factorFound);
\path [line] (factorFound) -- (trivial);
\path [line] (lowQuot) -- (prime);
\path [line] (lowQuot) |- node {No}(divide);
\end{tikzpicture}
\label{Figure:TrialDivFlowChart}
\caption{Trial division as described in [Knuth 2004].}
\end{figure}
\end{center}
\end{document}
Best Answer
You can use positioning library and control the positioning as you wish.