1

How could I create this graph? Assume that there are weights from every node to all directions. Apologies for the badly drawn example and thank you in advance for your help!

graph with five nodes

1 Answer 1

5

The proper way is to use the graph drawing libraries that come with TikZ, the quick and dirty way is to draw it in a loop. If you want more information on the proper way, show us what you have tried. Here is the quick and dirty way (but the arc arrow may be useful for the proper way, too).

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,decorations.markings}
\tikzset{% https://tex.stackexchange.com/a/430239
    arc arrow/.style args={%
    to pos #1 with length #2}{
    decoration={
        markings,
         mark=at position 0 with {\pgfextra{%
         \pgfmathsetmacro{\tmpArrowTime}{#2/(\pgfdecoratedpathlength)}
         \xdef\tmpArrowTime{\tmpArrowTime}}},
        mark=at position {#1-\tmpArrowTime} with {\coordinate(@1);},
        mark=at position {#1-2*\tmpArrowTime/3} with {\coordinate(@2);},
        mark=at position {#1-\tmpArrowTime/3} with {\coordinate(@3);},
        mark=at position {#1} with {\coordinate(@4);
        \draw[-{Stealth[length=#2,bend]}]       
        (@1) .. controls (@2) and (@3) .. (@4);},
        },
     postaction=decorate,
     }
}
\begin{document}
\begin{tikzpicture}[bullet/.style={fill,circle,inner sep=2pt},
bent arrow/.style={arc arrow=to pos #1 with length 2mm}]
 \path foreach \X in {1,...,5} {
 (-72+90+72*\X:3) node[bullet] (p\X){}};
 \foreach \X [evaluate=\X as \NextX using {int(mod(\X,5)+1)},
  evaluate=\X as \NextNextX using {int(mod(\X+1,5)+1)},] in {1,...,5} {
 \draw[bent arrow=0.55] (p\X) to[out=-72+110+72*\X,in=-72+180+72*\X] 
 ++ (-72+90+72*\X:1.25) to[out=-72+72*\X,in=-72+70+72*\X] (p\X);
 \draw[bent arrow=0.55] (p\X) to[out=-72+180+72*\X,in=72*\X,looseness=0.8] (p\NextX);
 \draw[bent arrow=0.55] (p\NextX) to (p\X);
 \draw[bent arrow=0.35] (p\NextNextX) to (p\X);
 \draw[bent arrow=0.35] (p\X) to (p\NextNextX);}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Thank you very muich for your reply. Before asking I attempted to adjust a similar graph but I failed. I think I follow the logic of this method. Is there a way to add node numbers and weights?
    – alpeter
    Commented Sep 7, 2019 at 21:17
  • @alpeter Yes, you can add ‘node[midway]{text}’ between ‘to[...]’ and the terminal node.
    – user194703
    Commented Sep 7, 2019 at 22:25
  • @alpeter And to adjust the label position, in case midway doesn't actually place it in the visual middle, use \node[pos=.4] {text} or some other number between 0 and 1 , instead of 0.4 (midway is the same as pos=.5). Now this may overlap the label and the arrow, so use something like \node[pos=.4,above=10pt] {text} or similar to fine-tune the position of the label. Commented Sep 8, 2019 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .