LaTeX

Graph Edges + Weight

ForceCore 2009. 5. 22. 14:51
Edge와 weight가 있는 그래프를 그리는 방법...
이런 때는 edge weight를 넣는 방법과, 그 위치를 결정하는 방법을 잘 알아야 한다.
안 그러면 그림이 구리게 나온다.



어려워보이진 않지만 edge weight를 넣는게 어려움.

코멘트는 영어로 되어 있지만 다 직접 코딩한거임 ㅡ,.ㅡ...;;

tikz로 코딩하는 인간도 드물어서, 혹시 영어권 사람이 코드는 적어도 베껴갈 수 있도록 배려한 것이다.

\usetikzlibrary{positioning}

\tikzstyle{c} = [draw, shape=circle,minimum size=0.9cm] % minimum size of each node
\begin{tikzpicture}[node distance=3cm,auto,on grid] % assign node distance. on grid positions nodes according to center, not dist betwen the "edge" of each node. "Auto" puts edge labels automatically. Try removing this and see what happens without it.

\node[c,fill=black!10] (s) {0};
\node [left] at (s.west) {$s$}; % node label like this

\node[c,fill=black!10] (t) [above right of=s] {$\infty$};
\node [above] at (t.north) {$t$};

\node[c,fill=black!10] (y) [below right of=s] {$\infty$};
\node [below] at (y.south) {$y$};

\node[c,fill=black!10] (z) [right of=y]  {$\infty$};
\node [below] at (z.south) {$z$};

\node[c,fill=black!10] (x) [right of=t]   {$\infty$};
\node [above] at (x.north) {$x$};

% edges
\draw [-stealth] (s) to node {6} (t);
\draw [-stealth] (s) to node[swap] {7} (y); % swap determines left/right position of the label.
\draw [-stealth] (y) to node[swap] {9} (z);
\draw [-stealth] (z) to node[swap] {7} (x);
\draw [-stealth] (t) to node [pos=0.8] {-4} (z); % pos determines the label position, along the length direction on the edge.
\draw [-stealth] (y) to node[swap] [pos=0.8] {-3} (x);
\draw [-stealth] (t) to node[swap] {8} (y);
\draw [-stealth] (z) to node[swap] [pos=0.3] {2} (s);
\draw [-stealth,bend left] (t) to node {5} (x); % bent edges like this
\draw [-stealth,bend left] (x) to node {-2} (t);

\end{tikzpicture}


참 쉽죠?

이걸 그리기 위해 코딩한 것임. (d)의 코드는 이렇다:


\usetikzlibrary{positioning}

\tikzstyle{c} = [draw, shape=circle,minimum size=0.9cm]
\begin{tikzpicture}[node distance=2cm,auto]

\node[c,fill=black,text=white] (s) {$0$};
\node [left] at (s.west) {$s$};

\node[c] (t) [above right of=s,fill=black,text=white] {$3$};
\node [above] at (t.north) {$t$};

\node[c] (y) [below right of=s,fill=black,text=white] {$5$};
\node [below] at (y.south) {$y$};

\node[c] (z) [right of=y]  {$11$};
\node [below] at (z.south) {$z$};

\node[c] (x) [right of=t,fill=black!20]   {$9$};
\node [above] at (x.north) {$x$};

% predecessors
\draw [line width=5pt,red!20] (s) to (t);
\draw [line width=5pt,red!20] (s) to (y);
\draw [line width=5pt,red!20] (t) to (x);
\draw [line width=5pt,blue!20,bend right] (y) to (t);
\draw [line width=5pt,blue!20] (y) to (x);
\draw [line width=5pt,blue!20] (y) to (z);

% edges
\draw [-stealth] (s) to node {3} (t);
\draw [-stealth] (s) to node [swap] {5} (y);
\draw [-stealth] (t) to node {6} (x);
\draw [-stealth] (y) to node [swap] {6} (z);
\draw [-stealth] (y) to node [pos=0.8] {4} (x);
\draw [-stealth] (z) to node [swap]  [pos=0.3] {3} (s);
\draw [-stealth,bend right] (x) to node {2} (z);
\draw [-stealth,bend right] (z) to node {7} (x);
\draw [-stealth,bend right] (t) to node {2} (y);
\draw [-stealth,bend right] (y) to node {1} (t);

\end{tikzpicture}

참 쉽죠?;;