3

I want to draw a bipartite graph like the following:

enter image description here

I tried the following code:

\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}

\begin{tikzpicture}[thick,
  every node/.style={draw,circle},
  fsnode/.style={fill=myblue},
  ssnode/.style={fill=mygreen},
  every fit/.style={rounded corners,rectangle,draw,inner sep=-2pt,text width=2cm, dashed},
  ->,shorten >= 3pt,shorten <= 3pt
]

% the vertices of U
\begin{scope}[start chain=going below,node distance=7mm]
\foreach \i in {1,2}
  \node[fsnode,on chain] (f\i)  {$v_{\i}$};
\end{scope}

% the vertices of V
\begin{scope}[xshift=4cm,yshift=0.6cm,start chain=going below,node distance=7mm]
\foreach \i in {6,7,8,9}
  \node[ssnode,on chain] (s\i)  {$v_{\i}$};
\end{scope}

% the set U
\node [myblue,fit=(f1) (f2),label=above:$G^{P}$] {};
% the set V
\node [mygreen,fit=(s6) (s9),label=above:$G^{Q}$] {};

% the edges
\draw (f1) -- (s6);
\draw (f1) -- (s7);
\draw (f2) -- (s6);
\draw (f2) -- (s7);
\end{tikzpicture}

It not give the same. In addition, how can I add a weight to this bipartite graph? The idea that I need also to add another graph on the right of the arrow. How can aggregate all?

1 Answer 1

4

Here is a proposal. Instead of chains and fit you may want to use the matrix library. This allows one to define a style with which you only need to specify the number of columns. This has the advantage that it is easier to vertically center the schemes. The connections can be drawn as edges.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,positioning,quotes}
\begin{document}
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}

\begin{tikzpicture}[thick,amat/.style={matrix of nodes,nodes in empty cells,
  row sep=1em,draw,dashed,rounded corners,
  nodes={draw,solid,circle,execute at begin node={$\nu_{\the\pgfmatrixcurrentrow}$}}},
  fsnode/.style={fill=myblue},
  ssnode/.style={fill=mygreen}]

 \matrix[amat,nodes=fsnode,label=above:$G^{P}$] (mat1) {\\
 \\};

 \matrix[amat,right=2cm of mat1,nodes=ssnode,label=above:$G^{Q}$] (mat2) {\\
 \\ 
 \\};

 \draw  (mat1-1-1) edge["$A$"] (mat2-1-1)
  (mat1-1-1) edge["$B$"] (mat2-2-1);

 \draw[line width=0.5em,stealth-stealth] ([xshift=1cm]mat2.east) -- ++ (1.5,0);

 \matrix[amat,right=3.5cm of mat2,nodes=fsnode,label=above:$G^{P}$] (mat3) {\\
 \\};

 \matrix[amat,right=2cm of mat3,nodes=ssnode,label=above:$G^{Q}$] (mat4) {\\
 \\ 
 \\};

 \draw  (mat3-1-1) edge["$A$"] (mat4-2-1)
  (mat3-1-1) edge["$B$"] (mat4-3-1);
\end{tikzpicture}
\end{document}

enter image description here

7
  • thank you for your proposal.Rnining your code does not show the name of the node inside the circle. Indeed, how can i specify the the edge between the nodes and how draw weights above the edge? Jul 12, 2019 at 2:48
  • @user2869180 The code produces the above output, so the names are shown. I added examples of labeled edges.
    – user121799
    Jul 12, 2019 at 2:55
  • thank you again but still the name of vertices not dsiplayed. Im working on overleaf Jul 12, 2019 at 3:01
  • 2
    @user2869180 It would be great if you could (1) tell me if you reproduce the above output if you run my code and (2) explain what you mean by "name of vertices". (I am not excited about overleaf, to say the least.)
    – user121799
    Jul 12, 2019 at 3:03
  • 2
    @user2869180 Well, I do not know what is going on here, all I can say is that on an updated TeX installation these issues do not arise. You should contact the overleaf support. There are just too many things that go wrong on overleaf, and I have not even the possibility to debug this.
    – user121799
    Jul 12, 2019 at 3:14

You must log in to answer this question.

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