0

The edges of my graph are overlapping, is there a way to have some distance between them and also how to color a specific edge

\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
  every node/.style={draw,circle},
  ssnode/.style={fill=myblue},
  fsnode/.style={fill=mygreen},
  every fit/.style={ellipse,draw,inner sep=-1pt,text width=1.75cm},
  ->,shorten >= 2pt,shorten <= 2pt
]

% the vertices of Slots
\begin{scope}[start chain=going below,node distance=1.5mm]
\foreach \i in {1,2,...,8}
  \node[ssnode,on chain] (s\i) [label=left: \i] {};
\end{scope}

% the vertices of Flights
\begin{scope}[xshift=4cm,yshift=-0.5cm,start chain=going below,node distance=1.5mm]
\foreach \i in {$f_{2}$,$f_{5}$,$f_{6}$,$f_{7}$,$f_{8}$}
  \node[fsnode,on chain] (f\i) [label=right: \i] {};
\end{scope}

% the set Slots
\node [myblue,fit=(s1) (s8),label=above:$Slots$] {};
% the set Flights
\node [mygreen,fit=(f$f_{2}$) (f$f_{8}$),label=above:$Flights$] {};

% the edges

\draw (s2) -- (f$f_{2}$);
\draw (s5) -- (f$f_{5}$);
\draw (s6) -- (f$f_{6}$);
\draw (s7) -- (f$f_{7}$);
\draw (s8) -- (f$f_{8}$);
\draw (f$f_{2}$) -- (s1);
\draw (f$f_{5}$) -- (s3);
\draw (f$f_{6}$) -- (s5);
\draw (f$f_{7}$) -- (s5);
\draw (f$f_{8}$) -- (s7);
\draw (s3) -- (f$f_{5}$);
\draw (s1) -- (f$f_{2}$);

\end{tikzpicture}
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 6, 2022 at 6:29

1 Answer 1

0

There are various techniques to change the position of the \draw lines:

  1. specify the start and end position in degrees on the node
  2. use yshift on the start and end node
  3. use the calc tikzlibrary to add an offset to the start and/or end positions

The first method is to specify the nodes as (nodename.degrees) instead of just (nodename), so instead of (s5) you use (s5.45) for example. In that case you don't start the line at the point closest to the opposing node, but at the position 45 degrees counterclockwise from the start at 0 degrees which is on the right of the node.

The second method specifies a distance to shift the line node in the y position, with the syntax ([yshift=xx unit]nodename), in this case for example ([yshift=-25mm]s6). This only works if you shift the opposing node with exactly the same amount, so it is not really flexible.

The third method uses the calc tikzlibrary to add a coordinate to the node, with the syntax ($(nodename)+(x,y)$). This looks a bit complex but it is the most flexible way. Note that this offsets the shorten = attribute, so you need to add that back by adding or subtracting a bit from the x coordinate.

The MWE below shows all three methods. Additionally, I simplified the aming of the (f) nodes by putting the subscript in the node label instead of in the node names. For the arrows that go both ways from and to a node I simplified by drawing with [<->]. This also looks a bit better in terms of overlap.

Coloring an edge is done by putting the color name as an option to the draw command, for example \draw[red].

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,fit,shapes}
\usetikzlibrary{calc}
\begin{document}
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
  every node/.style={draw,circle},
  ssnode/.style={fill=myblue},
  fsnode/.style={fill=mygreen},
  every fit/.style={ellipse,draw,inner sep=-1pt,text width=1.75cm},
  ->,shorten >= 2pt,shorten <= 2pt
]

% the vertices of Slots
\begin{scope}[start chain=going below,node distance=1.5mm]
\foreach \i in {1,2,...,8}
  \node[ssnode,on chain] (s\i) [label=left: \i] {};
\end{scope}

% the vertices of Flights
\begin{scope}[xshift=4cm,yshift=-0.5cm,start chain=going below,node distance=1.5mm]
\foreach \i in {2,5,6,7,8}
  \node[fsnode,on chain] (f\i) [label=right: $f_{\i}$] {};
\end{scope}

% the set Slots
\node [myblue,fit=(s1) (s8),label=above:$Slots$] {};
% the set Flights
\node [mygreen,fit=(f2) (f8),label=above:$Flights$] {};

% the edges

\draw[red] (s2) -- (f2);
\draw (s5.45) -- (f5.220);
\draw ([yshift=-25mm]s6) -- ([yshift=-25mm]f6);
\draw (s7) -- (f7);
\draw (s8) -- (f8);
\draw[<->] ($(f2)+(-0.15,0.15)$) -- (s1);
\draw[<->] (f5) -- (s3);
\draw (f6) -- (s5);
\draw (f7) -- (s5);
\draw (f8) -- (s7);

\end{tikzpicture}
\end{document}

Result:

enter image description here

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.