1. 程式人生 > >latex中畫鄰接矩陣、鄰接表、排列樹

latex中畫鄰接矩陣、鄰接表、排列樹

最近的演算法分析作業總是要編輯公式和畫圖,word操作的實在是不太友好,於是“殺雞用牛刀”–便用了LATEX。 題目如下:

給出下圖的權矩陣和鄰接表

這裡寫圖片描述

  • 權矩陣:
\usepackage{amsmath}
\begin{equation*}
\begin{matrix}
\quad &V_1 &V_2&V_3&V_4&V_5\\
\end{matrix}
\end{equation*}
\begin{equation*}
\begin{matrix}
 V_1& \\
 V_2& \\
 V_3
& \\ V_4& \\ V_5& \end{matrix} \begin{bmatrix} \infty&10&\infty&4&\infty\\ 10&\infty&15&8&5\\ \infty&15&\infty&7&30\\ 4&8&7&\infty&6\\ 12&5&30&6&\infty \end{bmatrix} \end{equation*}

這裡寫圖片描述

  • 鄰接表
\usepackage{tikz}
\usetikzlibrary{calc,matrix,decorations.markings,decorations.pathreplacing} \usetikzlibrary{arrows,shapes,chains}
\begin{tikzpicture}[nodes in empty cells,
      nodes={minimum width=0.7cm, minimum height=0.6cm}]
    \node(0){0};
    \node[draw,rectangle,node distance=0.7cm,right of=0,minimum height=0.8cm](n0
){$V_1$} ; \node[rectangle,draw,right of=n0,node distance=0.7cm,minimum height=0.8cm](n1){}; \node[rectangle,draw,right of=n1](n2){1}; \node[rectangle,draw, right of=n2,node distance=0.7cm](n3){10}; \node[rectangle,draw,right of=n3,node distance=0.7cm](n4){}; \node[rectangle,draw, right of=n4](n5){3}; \node[rectangle,draw, right of=n5,node distance=0.7cm](n6){4}; \node[rectangle,draw,right of=n6,node distance=0.7cm](n7){}; \node[rectangle,draw, right of=n7](n8){4}; \node[rectangle,draw, right of=n8,node distance=0.7cm](n9){12}; \node[rectangle,draw, right of=n9,node distance=0.7cm](n10){$\wedge$}; \draw[-latex] (n1.center) -- (n2.west); \draw[-latex] (n4.center) -- (n5.west); \draw[-latex] (n7.center) -- (n8.west); \node(1)[node distance=0.8cm,below of=0]{1}; \node[draw,rectangle,node distance=0.7cm,right of=1,minimum height=0.8cm](n10){$V_2$} ; \node[rectangle,draw,right of=n10,node distance=0.7cm,minimum height=0.8cm](n11){}; \node[rectangle,draw,right of=n11](n12){0}; \node[rectangle,draw, right of=n12,node distance=0.7cm](n13){10}; \node[rectangle,draw,right of=n13,node distance=0.7cm](n14){}; \node[rectangle,draw, right of=n14](n15){2}; \node[rectangle,draw, right of=n15,node distance=0.7cm](n16){15}; \node[rectangle,draw,right of=n16,node distance=0.7cm](n17){}; \node[rectangle,draw, right of=n17](n18){3}; \node[rectangle,draw, right of=n18,node distance=0.7cm](n19){8}; \node[rectangle,draw,right of=n19,node distance=0.7cm](n110){}; \node[rectangle,draw, right of=n110](n111){4}; \node[rectangle,draw,right of=n111,node distance=0.7cm](n112){5}; \node[rectangle,draw, right of=n112,node distance=0.7cm](n113){$\wedge$}; \draw[-latex] (n11.center) -- (n12.west); \draw[-latex] (n14.center) -- (n15.west); \draw[-latex] (n17.center) -- (n18.west); \draw[-latex] (n110.center) -- (n111.west); \node(2)[node distance=0.8cm,below of=1]{2}; \node[draw,rectangle,node distance=0.7cm,right of=2,minimum height=0.8cm](n20){$V_3$} ; \node[rectangle,draw,right of=n20,node distance=0.7cm,minimum height=0.8cm](n21){}; \node[rectangle,draw,right of=n21](n22){1}; \node[rectangle,draw, right of=n22,node distance=0.7cm](n23){15}; \node[rectangle,draw,right of=n23,node distance=0.7cm](n24){}; \node[rectangle,draw, right of=n24](n25){3}; \node[rectangle,draw, right of=n25,node distance=0.7cm](n26){7}; \node[rectangle,draw,right of=n26,node distance=0.7cm](n27){}; \node[rectangle,draw, right of=n27](n28){4}; \node[rectangle,draw, right of=n28,node distance=0.7cm](n29){30}; \node[rectangle,draw, right of=n29,node distance=0.7cm](n210){$\wedge$}; \draw[-latex] (n21.center) -- (n22.west); \draw[-latex] (n24.center) -- (n25.west); \draw[-latex] (n27.center) -- (n28.west); \node(3)[node distance=0.8cm,below of=2]{3}; \node[draw,rectangle,node distance=0.7cm,right of=3,minimum height=0.8cm](n30){$V_4$} ; \node[rectangle,draw,right of=n30,node distance=0.7cm,minimum height=0.8cm](n31){}; \node[rectangle,draw,right of=n31](n32){0}; \node[rectangle,draw, right of=n32,node distance=0.7cm](n33){4}; \node[rectangle,draw,right of=n33,node distance=0.7cm](n34){}; \node[rectangle,draw, right of=n34](n35){1}; \node[rectangle,draw, right of=n35,node distance=0.7cm](n36){8}; \node[rectangle,draw,right of=n36,node distance=0.7cm](n37){}; \node[rectangle,draw, right of=n37](n38){2}; \node[rectangle,draw, right of=n38,node distance=0.7cm](n39){7}; \node[rectangle,draw,right of=n39,node distance=0.7cm](n310){}; \node[rectangle,draw, right of=n310](n311){4}; \node[rectangle,draw,right of=n311,node distance=0.7cm](n312){6}; \node[rectangle,draw, right of=n312,node distance=0.7cm](n313){$\wedge$}; \draw[-latex] (n31.center) -- (n32.west); \draw[-latex] (n34.center) -- (n35.west); \draw[-latex] (n37.center) -- (n38.west); \draw[-latex] (n310.center) -- (n311.west); \node(4)[node distance=0.8cm,below of=3]{4}; \node[draw,rectangle,node distance=0.7cm,right of=4,minimum height=0.8cm](n40){$V_5$} ; \node[rectangle,draw,right of=n40,node distance=0.7cm,minimum height=0.8cm](n41){}; \node[rectangle,draw,right of=n41](n42){0}; \node[rectangle,draw, right of=n42,node distance=0.7cm](n43){12}; \node[rectangle,draw,right of=n43,node distance=0.7cm](n44){}; \node[rectangle,draw, right of=n44](n45){1}; \node[rectangle,draw, right of=n45,node distance=0.7cm](n46){5}; \node[rectangle,draw,right of=n46,node distance=0.7cm](n47){}; \node[rectangle,draw, right of=n47](n48){2}; \node[rectangle,draw, right of=n48,node distance=0.7cm](n49){30}; \node[rectangle,draw,right of=n49,node distance=0.7cm](n410){}; \node[rectangle,draw, right of=n410](n411){3}; \node[rectangle,draw,right of=n411,node distance=0.7cm](n412){6}; \node[rectangle,draw, right of=n412,node distance=0.7cm](n413){$\wedge$}; \draw[-latex] (n41.center) -- (n42.west); \draw[-latex] (n44.center) -- (n45.west); \draw[-latex] (n47.center) -- (n48.west); \draw[-latex] (n410.center) -- (n411.west); \end{tikzpicture}

這裡寫圖片描述

畫出n=3的排列樹

\begin{tikzpicture}[level/.style={sibling distance=40mm/#1}]
  \node [circle,draw]{$A$}
  child{node[circle,draw]{$B$}child{node[circle,draw]{$E$}child{node[circle,draw]{$K$}}}child{node[circle,draw]{$F$}child{node[circle,draw]{$L$}}}}
  child{node[circle,draw]{$C$}child{node[circle,draw]{$G$}child{node[circle,draw]{$M$}}}child{node[circle,draw]{$H$}child{node[circle,draw]{$N$}}}}
  child{node[circle,draw]{$D$}child{node[circle,draw]{$I$}child{node[circle,draw]{$J$}}}child{node[circle,draw]{$O$}child{node[circle,draw]{$P$}}}};
\end{tikzpicture}

這裡寫圖片描述