我想要绘制如下的二分图:
我尝试了以下代码:
\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}
它给出的结果不一样。此外,我该如何为这个二分图添加权重?我还需要在箭头右侧添加另一个图。如何聚合所有内容?
答案1
chains
这里有一个建议。您可能fit
希望使用库来代替matrix
。这允许您定义一种样式,您只需指定列数即可。这样做的好处是更容易垂直居中方案。连接可以绘制为边。
\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}