5

enter image description here

So I'm trying to plot a graph with routes. I have two questions:

  1. How can I bend the lines? I would like the 9->4 to go around node 7.
  2. How can I change the names of the nodes? I want node 9 to be called "0".

Is the method I chose to simple to make these changes?

\begin{tikzpicture}[scale=.1,vertex/.style={draw,circle}, arc/.style={draw,thick,->}]
\foreach [count=\i] \coord in {(40,30),(90,10),(20,70),(100,20),(90,20),(70,30),(70,40),(10,70),(50,50)}{
    \node[vertex] (p\i) at \coord {\i};
}
\foreach [count=\r] \row in {{0,0,0,0,0,0,0,0,1},{0,0,0,0,1,0,0,0,0},{1,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1},{0,0,1,0,0,0,0,0,0},{0,0,0,1,0,0,0,1,0}}{
    \foreach [count=\c] \cell in \row{
        \ifnum\cell=1%
            \draw[arc] (p\r) edge (p\c);
        \fi
    }
} 
\end{tikzpicture}
Anon
  • 53

2 Answers2

6

Edit : Added an even simpler syntax with the graphs library

It should be noted that there is a typographical error in the manual 3.0.1a, the use existing nodes key must be written in the plural (with an s to node) and not in the singular as in the manual. I just created a ticket for this: #494 typographical error use existing node

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[scale=1,every node/.style={draw,circle}]
\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node (p\i) at \coord {\i};
}
\graph [use existing nodes,edges={thick}]
{
p0->p8->p3->p1->p0->[bend left]p4->p2->p5->p6->p7->p0;
};
\end{tikzpicture}
\end{document}

Old answer:

I coded your figure in a simpler way using the native possibilities of the foreach loop and the chains library.

It is possible to make only one loop: the one that defines the nodes. To make node 9 (5,5) named 0, simply place it at the beginning of the foreach and start the count at zero.

\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node[vertex] (p\i) at \coord {\i};
}

Then with the chains library to connect the nodes with arrows.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain,scale=1,every node/.style={draw,circle}, every join/.style={draw,thick,->}]
\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node (p\i) at \coord {\i};
}
\chainin (p0);
\chainin (p8)[join];
\chainin (p3)[join];
\chainin (p1)[join];
\chainin (p0)[join];
\chainin (p4)[join=with p0 by {bend left}];
\chainin (p2)[join];
\chainin (p5)[join];
\chainin (p6)[join];
\chainin (p7)[join];
\chainin (p0)[join];
\end{tikzpicture}
\end{document}

chaine

AndréC
  • 24,137
5

Welcome to TeX.SE! I had to rescale your coordinates to avoid dimension too large errors, yet the graph looks as if I had not done that.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[vertex/.style={draw,circle}, arc/.style={draw,thick,->}]
\foreach [count=\i] \coord in {(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7),(5,5)}{
    \ifnum\i=9
    \node[vertex,alias=p0] (p\i) at \coord {0};
    \else
    \node[vertex] (p\i) at \coord {\i};
    \fi
}
\foreach [count=\r] \row in {{0,0,0,0,0,0,0,0,1},{0,0,0,0,1,0,0,0,0},{1,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1},{0,0,1,0,0,0,0,0,0},{0,0,0,1,0,0,0,1,0}}{
    \foreach [count=\c] \cell in \row{
        \ifnum\cell=1%
            \ifnum\r>\c
             \pgfmathtruncatemacro{\itest}{\r+\c*10}
             \ifnum\itest=49
              \draw[arc] (p\r) edge[bend left] (p\c);
             \else
              \draw[arc] (p\r) edge (p\c);
             \fi
            \else
             \draw[arc] (p\r) edge (p\c);
            \fi
        \fi
    }
} 
\end{tikzpicture}
\end{document}

enter image description here