![TikZ で隣接配列を描画するにはどうすればいいですか?](https://rvso.com/image/305902/TikZ%20%E3%81%A7%E9%9A%A3%E6%8E%A5%E9%85%8D%E5%88%97%E3%82%92%E6%8F%8F%E7%94%BB%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%81%84%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%3F.png)
グラフ表現を隣接配列として描画します。次のようなものになります。
どうやって作るのでしょうか?上の写真のような例は見つかりませんでした。
答え1
出発点:
コード:
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,arrows.meta,arrows}
\tikzset{
mymat/.style={
matrix of math nodes,
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
column sep=-\pgflinewidth
},
mymats/.style={
mymat,
nodes={draw,fill=#1}
}
}
\begin{document}
\begin{tikzpicture}[>=latex]
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,0)
(mat1)
{
0 & 1 & 2 & 3 & 4 \\
2 & 1 & 3 & 2 & 1 \\
};
\matrix[mymat,right=of mat1,row 2/.style={nodes={draw,fill=gray!30}}]
(mat2)
{
5 & 6 & 7 & 8 \\
2 & 1 & 2 & 1 \\
};
\matrix[mymats=white,anchor=west]
at (0,-2)
(mat3)
{
1 & 2 & 0 & 0 & 3 & ? & 2 & 4 & 3 \\
};
\matrix[mymats=gray!30,right=of mat3]
(mat4)
{
? & 6 & 7 & 5 & 8 & 7 & ? \\
};
\node[above=0pt of mat1]
(cella) {Cell A};
\node[above=0pt of mat2]
(cellb) {Cell B};
\node at (mat4-1-7.north|-cellb)
(cellx) {Cell X};
\begin{scope}[shorten <= -2pt]
\draw[*->]
(mat1-2-1.south) -- (mat3-1-1.north);
\draw[*->]
(mat1-2-2.south) -- (mat3-1-3.north);
\draw[*->]
(mat1-2-3.south) -- (mat3-1-4.north);
\draw[*->]
(mat1-2-4.south) -- (mat3-1-7.north);
\draw[*->]
(mat1-2-5.south) -- (mat3-1-9.north);
\draw[*->]
(mat2-2-1.south) -- (mat4-1-1.north);
\draw[*->]
(mat2-2-2.south) -- (mat4-1-3.north);
\draw[*->]
(mat2-2-3.south) -- (mat4-1-4.north);
\draw[*->]
(mat2-2-4.south) -- (mat4-1-6.north);
\draw[*->,dashed,line width=0.7pt]
(mat4-1-1.north) to[out=90,in=-60,looseness=0.4] (mat1-2-3.south);
\draw[*->,dashed,line width=0.7pt]
(mat3-1-6.north) to[out=90,in=-90] (mat2-2-1.south);
\draw[*->,dashed,line width=0.7pt]
(mat4-1-7.north) ..
controls ++ (0.5,0.5) and ++(-1,-0.7) ..
(cellx.south);
\end{scope}
\end{tikzpicture}
\end{document}