4

I am trying to generate the following graph

enter image description here

Here is the code I wrote in an attempt to generate such graph

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
                    thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

\node[main node] (1) {a}; \node[main node] (2) [below = 1] {d}; \node[main node] (3) [right = 2] {e}; \node[main node] (4) [above left = 3] {c}; \node[main node] (5) [above right = 4] {b}; \node[main node] (6) [below right = 5] {f};

\path[every node/.style={font=\sffamily\small}] (1) edge node [right] {a} (1) (2) edge node [right] {d} (2) (3) edge node [left] {e} (3) (4) edge node [left] {c} (3) edge node [left] {c} (2) edge node [right] {c} (5) (5) edge node [right] {b} (1) (6) edge node [left] {f} (2); \end{tikzpicture} \end{document}

I got the following output: enter image description here

How do I get the intended graph?

CampanIgnis
  • 4,624

3 Answers3

5

You did wrong with the positioning. You could have used left=3cm of <node> but left=3 doesn't do anything. With the positioning library, you can adjust everything with fine tuning.

Below, I did not adjust correctly the node positions on the arrows. It can be done automatically (see, the auto option) or manually, with pos=..., for example.

graph

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm,
                        thick,main node/.style={circle,draw,minimum size=8mm,font=\sffamily\Large\bfseries}]
  \node[main node] (a) {a};
  \node[main node] (c) [below right = of a] {c};
  \node[main node] (d) [below left = of c] {d};
  \node[main node] (e) [below right = of c] {e};  
  \node[main node] (b) [above right = of c] {b};
  \node[main node] (f) [below right = of b] {f};

  \path[every node/.style={font=\sffamily\small}]
    (a) edge node [right] {$1$} (d)
    (c) edge node [right] {$2$} (a)
        edge node [left] {$3$} (d)
        edge node [left] {$5$} (b)
    (d) edge node [below] {$-2$} (e)
    (e) edge node [right] {$3$} (c)
    (b) edge node [right] {$-1$} (f)
    (f) edge node [below] {$-2$} (e);
\end{tikzpicture}

\end{document}

SebGlav
  • 19,186
5

Without positioning library.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\path[nodes={circle,draw,minimum size=5mm}] 
(-1,1)  node (a) {$a$}
(1,1)   node (b) {$b$}
(0,0)   node (c) {$c$}      
(-1,-1) node (d) {$d$}
(1,-1)  node (e) {$e$}
(2,0)   node (f) {$f$}
;
\begin{scope}[nodes={midway,scale=.6,magenta}]
\draw[->] (a)--(d) node[left]{$1$};
\draw[->] (c)--(a) node[above right]{$2$};
\draw[->] (c)--(d) node[above left]{$3$};
\draw[->] (e)--(c) node[above right]{$3$};
\draw[->] (c)--(b) node[above left]{$5$};
\draw[->] (d)--(e) node[above]{$-2$};
\draw[->] (f)--(e) node[below right]{$-2$};
\draw[->] (b)--(f) node[above right]{$-1$};
\end{scope}
\end{tikzpicture}
\end{document}
Black Mild
  • 17,569
5

Your diagram is simple to draw by use of tikz-cd package.

Edit: To arrows are added option math mode, i.e.: consequently labels are in math node without inserting them into $ ... $:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document} [ \begin{tikzcd}[ sep = 2cm, cells = {nodes={circle, draw, thick, minimum size=11mm, inner sep=1pt, anchor=center, font=\Large\sffamily\bfseries} }, math mode = false, arrow style = tikz, arrows = {>={Straight Barb[angle=90:4pt 2]}, semithick, math mode}, labels = {font=\normalsize} ] a \ar[dd,"1"] & & b \ar[dr,"-1"] & \ & c \ar[ul,"2"] \ar[ur,"5"] \ar[dl,"3"] & & f \ar[dl,"-2"] \ d \ar[rr, "-2"] & & e \ar[lu,"3"] &
\end{tikzcd} ] \end{document}

enter image description here

Zarko
  • 296,517