如何绘制具有加权边的二分图?

如何绘制具有加权边的二分图?
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc}
\usetikzlibrary{arrows, automata}

\begin{document}

\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={ellipse,draw,inner sep=-2pt,text width=2cm},
 ->,shorten >= 3pt,shorten <= 3pt
]

% the vertices of U
\begin{scope}[start chain=going below,node distance=10mm]
\foreach \i in {1,2,3}
  \node[fsnode,on chain] (S\i) [label=left: S\i] {};
\end{scope}

% the vertices of V
\begin{scope}[xshift=6cm,yshift=-0.5cm,start chain=going below,node distance=7mm]
\foreach \i in {4,5,6,7}
  \node[ssnode,on chain] (D\i) [label=right: D\i] {};
\end{scope}

% the set U
\node [myblue,fit=(S1) (S3),label=above:$Offre$] {};
% the set V
\node [mygreen,fit=(D4) (D7),label=above:$Demande$] {};

% the edges
\draw[->] (S1) -- (D4);
\draw[->] (S1) -- (D5);
\draw[->] (S1) -- (D6);
\draw[->] (S1) -- (D7);
\draw[->] (S2) -- (D4);
\draw[->] (S2) -- (D5);
\draw[->] (S2) -- (D6);
\draw[->] (S2) -- (D7);
\draw[->] (S3) -- (D4);
\draw[->] (S3) -- (D5);
\draw[->] (S3) -- (D6);
\draw[->] (S3) -- (D7);



\end{tikzpicture}


\end{document}

答案1

问题不太精确,所以这只是尝试回答问题。如果这不能回答问题,请扩展您的问题。

此外,pos=<number between 0 and 1>还有中途、开始时、即将开始时等可能性(参见pgf 手册,第 17.8 节“在线或曲线上明确放置节点”)。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit,positioning,shapes}

\definecolor{myBlue}{RGB}{80,80,160}
\definecolor{myGreen}{RGB}{80,160,80}

\colorlet{color1}{orange}
\colorlet{color2}{cyan}
\colorlet{color3}{violet}

\begin{document}
\begin{tikzpicture}[
    node distance=10mm,
    thick,
    graphNode/.style={draw,circle,fill=#1},
    every fit/.style={ellipse,draw,minimum width=2.25cm},
    ->,shorten >= 3pt,shorten <= 3pt
]

% vertices of U
\foreach \i in {1,2,3} {
    \node[graphNode=myBlue] at(0,-\i+.5) (S\i) [label=left: S\i] {};
}

% vertices of V
\foreach \i in {4,5,6,7} {
    \node[graphNode=myGreen] at(6,-\i+4) (D\i) [label=right: D\i] {};
}

% set U
\node at (0,1.3) [rectangle,draw=none] {Offre};
\node[myBlue,fit=(S1) (S3)] {};
% set V
\node  at (6,1.3) [rectangle,draw=none] {Demande};
\node[myGreen,fit=(D4) (D7)] {};

% arrows
\foreach \i in {1,2,3} {
    \foreach \j in {4,5,6,7} {
        \draw[->,color\i!50] (S\i) -- (D\j);
    }
}
% We want to have the weights always in the foreground.
% If this is not important, you can also use the
% node command below in the draw command above.
\foreach \i in {1,2,3} {
    \foreach \j in {4,5,6,7} {
        \path[->,color\i!50] (S\i) -- node[pos=\j/10-.05,above,sloped,text=color\i,font=\small] {$w_{\i,\j}$} (D\j);
    }
}
\end{tikzpicture}
\end{document}

具有加权和彩色边的二分图。

相关内容