LaTeX中画邻接矩阵、邻接表、排列树

最近的算法分析作业总是要编辑公式和画图, word w o r d 操作的实在是不太友好,于是“杀鸡用牛刀”–便用了 LATEX L A T E X
题目如下:

给出下图的权矩阵和邻接表

这里写图片描述

  • 权矩阵:
\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*}

这里写图片描述


  • 邻接表

在这里想啰嗦几句, TikZ T i k Z 包真的是一个很好的神器,在 LATEX L A T E X 中使用它可以画出很多超级好看的图哟!
接下来分享几个学习 TikZ T i k Z 的网址:
官网学习的example
Data structures in TikZ T i k Z
TikZ T i k Z 快速入门文档
LATEX L A T E X 在线编辑网址
\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}

这里写图片描述

### 回答1: 在LaTeX,要横着放置多个矩阵,可以使用多种方法。 一种方法是使用多个矩阵环境,并使用水平排列它们的方法。可以使用多个\begin{matrix}\end{matrix}来创建矩阵,并使用&符号来分隔矩阵的元素。例如,如下所示的代码可以将两个矩阵横向排列: \[ \begin{matrix} 1 & 2 \\ 3 & 4 \\ \end{matrix} \begin{matrix} 5 & 6 \\ 7 & 8 \\ \end{matrix} \] 另一种方法是使用一个多行格式的环境,例如{bmatrix}、{pmatrix}或{vmatrix}。这些环境可以通过添加额外的&符号来创建多个列。例如,下面的代码将两个矩阵横向放置: \[ \begin{bmatrix} 1 & 2 & 5 & 6 \\ 3 & 4 & 7 & 8 \\ \end{bmatrix} \] 以上两种方法可以根据需要来调整矩阵的大小和内容。同时,也可以使用其他的矩阵环境和布局来实现不同的效果。希望能帮助到您! ### 回答2: 在LaTeX,可以使用`multicols`包提供的`multicols`环境来横向放置多个矩阵。以下是使用LaTeX代码实现的示例: \documentclass{article} \usepackage{amsmath} \usepackage{multicol} \begin{document} \begin{multicols}{2} % 指定将矩阵分为两列 \[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \] \[ \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} \] \end{multicols} \end{document} 在上面的示例,`multicols`环境将矩阵分为两列,并将每个矩阵放置在不同的列。在实际使用时,您可以根据需要调整`multicols`环境的参数,以便可同时横向放置更多的矩阵。 ### 回答3: 在LaTeX,可以使用宏包`amsmath`来处理多个矩阵并使其横向放置。具体的方法是使用`&`符号来分隔矩阵,然后使用`\quad`或`\qquad`来控制矩阵之间的间距。 下面是一个示例代码: \begin{verbatim} \documentclass{article} \usepackage{amsmath} \begin{document} \[ \begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix} \quad \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} \] \end{document} \end{verbatim} 以上代码,我们使用了两个`matrix`环境来定义两个矩阵,它们之间用`\quad`来分隔,生成的效果是两个矩阵横向放置,并且之间有一定的间距。 当然,你也可以根据需要选择使用`\qquad`或其他间距命令。如果需要更复杂的矩阵布局,可以考虑使用`align`环境或`tabular`环境等。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值