5

I need to create a cost matrix and a trace like in the image below. I found nothing that seemed up to the task in the matrix package. Is this possible in LaTeX?

Example of cost matrix

11
  • What is a cost matrix?
    – AndréC
    Aug 13, 2020 at 7:33
  • 2
    You can try to use nicematrix that allows to easily use TikZ to draw the arrows. What would be the way to type the left table ? Make LaTeX calculate the path for the arrows will be more challenging but feasible (but not by me for the moment).
    – projetmbc
    Aug 13, 2020 at 7:40
  • Look at the matrix on the left. You have two strings "ABABBB" and "BABAAA". The distance between the two strings can be described using the Levenshtein distance. The value (n,m) in a cost matrix with the size n*m is the minimum cost to transform string one to string two. You can then get an optimal edit sequence by tracing the route towards (0,0) using the smallest values on the way.
    – A-wels
    Aug 13, 2020 at 7:42
  • If I understand correctly, you want this matrix to be built automatically when we give any two strings of characters according to the algorithm indicated in the link.
    – AndréC
    Aug 13, 2020 at 8:01
  • 1

3 Answers 3

7

There are a lot of different packages and therefore a lot of possibilities to do this. Here is one with the matrix library of the TikZ package.

Update 1 : problem on a arrow

The first code reverses an arrow and I haven't figured out why yet. To correct this, the code becomes heavier.

screenshot

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{matrix,arrows.meta}
\begin{document}
\begin{tikzpicture}[>={Stealth[length=4pt]}]
\matrix (cost)[matrix of nodes,nodes in empty cells]
{ & & B & A & B & A & A & A \\
  & 0 & 1 & 2 & 3 & 4 & 5 & 6\\
A & 1 & 2 & 1 & 2 & 3 & 4 & 5\\
B & 2 & 1 & 2 & 1 & 2 & 3 & 4 \\
A & 3 & 2 & 1 & 2 & 1 & 2 & 3\\
B & 4 & 3 & 2 & 1 & 2 & 3 & 4\\
B & 5 & 4 & 3 & 2 & 3 & 4 & 5\\
B & 6 & 5 & 4 & 3 & 4 & 5 & 6\\  
};
\draw (cost-1-8.south east) -| (cost-8-1.south east)-| cycle;
\draw (cost-2-8.south east) -| (cost-8-2.south east);
\foreach \x/\y [remember =\x as \lastx (initially 8),remember=\y as \lasty (initially 8)] in {7/7,6/6}{
 \draw[->,shorten <=-3pt,shorten >=-3pt](cost-\lastx-\lasty)--(cost-\x-\y);
 }
 
 \draw[<-,shorten <=-3pt,shorten >=-3pt] (cost-6-6)--(cost-5-6);
\foreach \x/\y [remember =\x as \lastx (initially 5),remember=\y as \lasty (initially 6)] in {4/5,3/4,2/3,2/2}{
 \draw[->,shorten <=-3pt,shorten >=-3pt](cost-\lastx-\lasty)--(cost-\x-\y);
 }
\end{tikzpicture}

\end{document}

screenshot

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{matrix,arrows.meta}
\begin{document}
\begin{tikzpicture}[>={Stealth[length=4pt]}]
\matrix (cost)[matrix of nodes,nodes in empty cells]
{ & & B & A & B & A & A & A \\
  & 0 & 1 & 2 & 3 & 4 & 5 & 6\\
A & 1 & 2 & 1 & 2 & 3 & 4 & 5\\
B & 2 & 1 & 2 & 1 & 2 & 3 & 4 \\
A & 3 & 2 & 1 & 2 & 1 & 2 & 3\\
B & 4 & 3 & 2 & 1 & 2 & 3 & 4\\
B & 5 & 4 & 3 & 2 & 3 & 4 & 5\\
B & 6 & 5 & 4 & 3 & 4 & 5 & 6\\  
};
\draw (cost-1-8.south east) -| (cost-8-1.south east)-| cycle;
\draw (cost-2-8.south east) -| (cost-8-2.south east);
\foreach \x/\y [remember =\x as \lastx (initially 8),remember=\y as \lasty (initially 8)] in {7/7,6/6,5/6,4/5,3/4,2/3,2/2}{
 \draw[->,shorten <=-3pt,shorten >=-3pt](cost-\lastx-\lasty)--(cost-\x-\y);
 }

\end{tikzpicture}

\end{document}
6

With nicematrix:

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\begin{document}

\renewcommand{\arraystretch}{1.2}

\begin{NiceTabular}{|ccccccc|}[first-row,first-col]
  &   & B & A & B & A & A & A \\
\hline
  & 0 & 1 & 2 & 3 & 4 & 5 & 6\\
A & 1 & 2 & 1 & 2 & 3 & 4 & 5\\
B & 2 & 1 & 2 & 1 & 2 & 3 & 4\\
A & 3 & 2 & 1 & 2 & 1 & 2 & 3\\
B & 4 & 3 & 2 & 1 & 2 & 3 & 4\\
B & 5 & 4 & 3 & 2 & 3 & 4 & 5\\
B & 6 & 5 & 4 & 3 & 4 & 5 & 6\\  
\hline
\CodeAfter
\begin{tikzpicture}
\draw (8-|2) |- (2-|8) ;
\begin{scope}[->,shorten < = 1pt, shorten > = 1pt]
\draw (1-2) -- (1-1) ;
\draw (2-3) -- (1-2) ;
\draw (3-4) -- (2-3) ;
\draw (4-5) -- (3-4) ;
\draw (5-5) -- (4-5) ;
\draw (6-6) -- (5-5) ;
\draw (7-7) -- (6-6) ;
\end{scope}
\end{tikzpicture}
\end{NiceTabular}

\end{document}

Output of the above code

4

A solution for both images, based on ordinary tabulars and the help of pstricks for the connections:

\documentclass[table]{article}
\usepackage{array}
\usepackage{tabularx} 
\usepackage{xcolor}
\usepackage{pst-node, multido} 

\begin{document}

\sffamily\setlength{\extrarowheight}{3pt}
\begin{tabularx}{\linewidth}{XX}
 { \begin{tabular}[t]{c|c|*{6}{c}|}
\multicolumn{3}{c}{}B & A & B & A & A & \multicolumn{1}{c}{A} \\
\cline{2-8}
 & \multicolumn{1}{c}{\rnode{8}{0}} & \rnode{7}{1} & 2 & 3 & 4 & 5 & 6 \\
 \cline{3-8}
 A & 1 & 2 & \rnode{6}{1} & 2 & 3 & 4 & 5 \\[2pt]
 B & 2 & 1 & 2 & \rnode{5}{1} & 2 & 3 & 4 \\[2pt]
 A & 3 & 2 & 1 & 2 & \rnode{4}{1} & 2 & 3 \\[2pt]
 B & 4 & 3 & 2 & 1 & \rnode{3}{2} & 3 & 4 \\[2pt]
 B & 5 & 4 & 3 & 2 & 3 & \rnode{2}{4} & 5 \\[2pt]
 B & 6 & 5 & 4 & 3 & 4 & 5 & \rnode{1}{6} \\
\cline{2-8}
\end{tabular}
\psset{arrows=->, arrowinset=0.12,linejoin=1, nodesep=1pt}
\multido{\is=1+1,\it=2+1}{7}{\ncline{\is}{\it}}}
 &
{\Large Trace\bigskip}\newline
{\setlength{\tabcolsep}{8pt}\begin{tabular}[t]{*{6}{c}}
  A & B & A & B & B & B \\[-1ex]
\pnode{U1} & \pnode{U2} & \pnode{U3} & & \pnode{U4} & \pnode{U5}\\[1em]
 & \pnode{D1} & \pnode{D2} & \pnode{D3} & \pnode{D4} & \pnode{D5}\\
B & A & B & A & A & A
\end{tabular}}
\multido{\i=1+1}{5}{\ncline{D\i}{U\i}}
\end{tabularx}

\end{document} 

enter image description here

2
  • I love PStricks +1....
    – MadyYuvi
    Aug 13, 2020 at 13:50
  • 1
    @MadyYuvi: What I appreciate in pstricks is that pstricks code can be included in ordinary environments without perturbating whatever, and further, contrary to TiKZ, it uses a LaTeX syntax.
    – Bernard
    Aug 13, 2020 at 13:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .