5

I have this graph:

Digraph

I would like to add nodes for specifying weights to some arrows and undirected edges. Edge (A)(r) should have capacity = \infty, and some undirected edges should have some numbers associated to their nodes.

I coded the edges by separating undirected arrows (using (n1) edge (n2) notation for undirected edges, and using \draw [->] for directed edges). The problem with this is that, for arrows, I can't change the code into something like (n1) edge node[MyStyle]{T} (n2), and I can't write it in the undirected edge set, because this would draw two separate edges between the same nodes.

How could I tweak this code in a simple way, to obtain what I want?

\documentclass{article}

\usepackage{tikz}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{setspace}

\begin{document}
\begin{center}
\begin{tikzpicture}
[scale=1.2,auto=left, every node/.style={ circle, fill=blue!20,   minimum size=2.5mm, inner sep=2pt}
              ]

\tikzset{My Style/.style={scale=0.8, black,fill=white,opacity=.2,text opacity=1, minimum size=0.5cm, }}

\tikzset{>=latex}

\node  [fill = white, draw = black] (n1) at (0, 4){A};
\node  [rectangle] (n2) at (1, 4){r};
\node  [rectangle] (n3) at (2, 5){p};
\node  [rectangle] (n4) at (3.5, 5){q};
\node  [rectangle] (n5) at (3.5, 3.5){s};
\node  [fill = white, draw = black] (n6) at (5, 4){B};
    \path

(n2) edge[bend left = 15] (n3)
(n3) edge (n4)
(n4) edge (n5)
(n2) edge[bend right = 15] (n5);

\draw [->]   (n1) edge (n2) (n4) edge[bend left = 15]  (n6) (n5) edge[bend right = 15] (n6);

\end{tikzpicture}
\captionof{figure}{}
\end{center}
\end{document}

THANKS

1

Addind nodes afterwards with coordinates is definitely not a good idea, because they won't follow any of the changes you would do on your original graph.
Instead, you would use the following structure: \path (n1) edge node{content} (n2); like in the code below.
Note that you didn't have to draw separately the edges without arrows and the ones with. Adding [->] after edge into your path is enough to chose which one should wear an arrow.

I also defined scopes to allow you using different kinds of node styles without having to write them everywhere. Now you should be able to customize the picture as you want.

\documentclass{article}

\usepackage{tikz}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{setspace}

\begin{document}
    \begin{center}
        \begin{tikzpicture}[>=latex,scale=1.5,auto=left]
    
            \begin{scope}[every node/.style=
                            {fill=blue!20,
                             minimum size=2.5mm,
                             inner sep=2pt}]
                \node  [circle,fill = white, draw = black] (n1) at (0, 4){A};
                \node  (n2) at (1, 4){r};
                \node  (n3) at (2, 5){p};
                \node  (n4) at (3.5, 5){q};
                \node  (n5) at (3.5, 3.5){s};
                \node  [circle,fill = white, draw = black] (n6) at (5, 4){B};
            \end{scope}
            
            \begin{scope}[every node/.style={inner sep=1pt}]
                \path
                    (n1) edge[->] node {$\infty$} (n2)
                    (n2) edge[bend left = 15] node{$4$}  (n3) 
                    (n3) edge (n4)
                    (n4) edge (n5)
                    (n2) edge[bend right = 15] node {$-8$}(n5)
                    (n4) edge[->,bend left = 15]  (n6) 
                    (n5) edge[->,bend right = 15] (n6);
            \end{scope}      
        
        \end{tikzpicture}
    \captionof{figure}{}
    \end{center}
\end{document}

nodes on graph edges

1
  • Note that you can customize your edge nodes globally (in the scope declaration) and also locally (behind each node), like this (n2) edge[bend right = 15] node[below] {$-8$}(n5).
    – SebGlav
    Mar 13 '21 at 15:46
1

You could just add new nodes:

\node [fill = white, scale= 0.6] (v1) at (0.5, 4.1){$\infty$};
\node [fill = white, scale= 0.6, rotate = -25] (v2) at (4.25, 4.84){$\infty$};
\node [fill = white, scale= 0.6] (v3) at (4.24, 3.72){$0$};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.