# Drawing a Grid/Tikz image

I need to draw something like this (forgive me, it is very crude), to denote the dependencies in my algorithm. Basically, I want to say that the circle (l,r) has incoming arrows from the 3 other circles (l,r-1), (l-1,r-1) and (l-1,r). I could only come up with a grid, using Tikz. how does one draw the dependency arrows and nodes as in the below image?

\documentclass[24point]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\foreach \x/\y/\m in {+0.75/+0.75/,-0.75/-0.75/} % etc
\node at (\x,\y) {\m};
\end{tikzpicture}%

\end{document}


• Here it would probably make sense to use a matrix of nodes and interconnecting those rather than a foreach approach. – Claudio Fiandrino Apr 4 at 12:27
• @ClaudioFiandrino Any references are welcome. – GermanShepherd Apr 4 at 12:29
• @ClaudioFiandrino Hiiiiiiiiiii :-). My affectionate regards. – Sebastiano Apr 4 at 12:30

I would draw your diagram like this

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth]
\foreach \i in {0,1,2,3.5,4.5,5.5,7,8} {
\draw[gray,very thin] (0,\i) -- (8.5,\i);
\draw[gray,very thin] (\i,0) -- (\i,8.5);
}
\draw[<->] (8.5,0) node[below] {$n$} -- (0,0) node[below left] {$(1,1)$} -- (0,8.5) node[left] {$n$};
\draw[->] (2,-.5) -- (3,-.5) node[above,near start] {$r$};
\foreach \i in {2.75,6.25} {
\foreach \j in {.5,1.5,4,5,7.5} {
\node at (\i,\j) {$\cdots$};
\node at (\j,\i) {$\vdots$};
}
\foreach \j in {2.75,6.25}
\node at (\i,\j) {\reflectbox{$\ddots$}};
}
\begin{scope}[every node/.style={
circle,draw,
minimum size=.8cm,
align=center,
font=\footnotesize,
inner sep=0pt
}]
\node at (4,4) (11) {$\scriptstyle l-1$\\[-1ex]$\scriptstyle r-1$};
\node at (5,4) (21) {$\scriptstyle l-1$\\[-1ex]$\scriptstyle r$};
\node at (4,5) (12) {$\scriptstyle l$\\[-1ex]$\scriptstyle r-1$};
\node at (5,5) (22) {$\scriptstyle l$\\[-1ex]$\scriptstyle r$};
\end{scope}
\draw[thick,red,->] (11) -- (22);
\draw[thick,red,->] (12) to[bend left=45] (22);
\draw[thick,red,->] (21) to[bend right=45] (22);
\end{tikzpicture}
\end{document}


Using a loop here is probably over-killing, but anyway, here is a modification of your code. Single draw command to do this ;)

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\tikz
\draw[nodes={inner sep=1pt},color=gray]
(0,0) grid[shift={(-.5,-.5)}] (4,4)
foreach[count=\i] \x/\y/\m in {2/3/a,2/2/b,3/3/c,}{(\x,\y) node(n\i) {\m}}
(n1) edge[red,-latex] (n3)
(n2) edge[red,-latex] (n3)
;
\end{document}