[Tex/LaTex] Drawing graphs in LaTeX


Is it possible to draw graphs like in image below in LaTeX?
It doesn't have to look exactly the same. I need arrows with numbers and circles (or dots or other symbol) with text in it (or next to it).

Best Answer

Here's an example, showing how you could do it with TikZ in a short and readable way.

  • Define styles for edges, arrows, and nodes
    • circle style for the main nodes, and font options so we don't need to adjust fonts within the nodes
    • For arrows, we use stealth' which is the name for a kind of arrow tip and shorten to not touch the node
    • The option auto is useful for automatic placement of nodes next to edges, instead of sitting directly on the edge. As we will mostly use left and right options, it will have effect just for one node. But good to have it as general option in the scope.
  • Place the main nodes
  • Draw edges with nodes for description
  • Use options loop and bend for loops and bent edges
  • Specify left and right for bend direction and node placement

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

  \node[main node] (1) {1};
  \node[main node] (2) [below left of=1] {2};
  \node[main node] (3) [below right of=2] {3};
  \node[main node] (4) [below right of=1] {4};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [left] {0.6} (4)
        edge [bend right] node[left] {0.3} (2)
        edge [loop above] node {0.1} (1)
    (2) edge node [right] {0.4} (1)
        edge node {0.3} (4)
        edge [loop left] node {0.4} (2)
        edge [bend right] node[left] {0.1} (3)
    (3) edge node [right] {0.8} (2)
        edge [bend right] node[right] {0.2} (4)
    (4) edge node [left] {0.2} (3)
        edge [loop right] node {0.6} (4)
        edge [bend right] node[right] {0.2} (1);

graph with nodes and edges

Related Question