Talk is cheap, show you the code.
MATLAB :
clear all;
global path n;%定义全局变量
%其中path为结构体,y为dyckpath的纵坐标,x为当前路径的指向
path.x=1;path.y=0;path.up=0;%初始化
n=3;%改变n的取值输出对应的dyckpath
dyckpath(1);%开始递归
%绘图
for ii=1:size(path.y,1)
figure(ii)
plot(0:2*n,path.y(ii,:),'k-'); hold on;
set(gca,'xtick',0:2*n,'ytick',0:n,'xlim',[0 2*n],'ylim',[0 n]);
title('dyck path');
grid on;hold off;
end
function []=dyckpath(row)
%输入的row指示当前是y的第几行
%即指向哪一条路
global path n;
if path.up(row)<n && path.y(row,path.x(row))>0
%如果在该点既可以向上也可以向下
%那么再增加一条路径向上
%原先路径继续向下
path.x=[path.x path.x(row)+1];
path.y=[path.y;path.y(row,:)];
path.up=[path.up path.up(row)+1];
path.y(end,path.x(end))=path.y(end,path.x(end)-1)+1;
dyckpath(size(path.x,2));
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)-1;
dyckpath(row);
elseif path.y(row,path.x(row))>0
%如果只能向下
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)-1;
dyckpath(row);
elseif path.up(row)<n
%如果只能向上
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)+1;
path.up(row)=path.up(row)+1;
dyckpath(row);
end
end
这是某院士留给我们的一次作业(明明是离散数学却留画图作业,看不懂,可能这就是院士吧),老师只要求画出n≤4的就可,可以一个一个画,我嫌太麻烦,就编了一个能自动画出图像的代码,只需要改变n的取值就会出来对应的Dyck Path的图,所以n不要取太大哦,n=11的时候卡特兰数就已经是5位数了,你一定不希望你的电脑一下子弹出那么多张图片吧。
当然这个程序也可以用来计算Catalan数,只需要把画图那部分代码注释掉就OK了,运行之后path.y的行数就是Catalan数(当然从程序运行效率上并不建议这么做)
附LaTeX代码:
\documentclass{article}
%\usepackage{ctex}
\usepackage[a4paper,left=30mm,right=30mm,top=20mm,bottom=30mm]{geometry}
\usepackage{graphicx}
\usepackage{subfigure}
\graphicspath{{C:/Users/dell/Desktop/dyckpath/}}
\usepackage{listings}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\begin{document}
\begin{figure}[]
\centering
\subfigure[1-1]{
\includegraphics[scale=0.3]{1_1.eps}}
\hspace{0in}
\caption{Dyckpath n=1}
\end{figure}
\begin{figure}[]
\centering
\subfigure[2-1]{
\includegraphics[scale=0.3]{2_1.eps}}
\hspace{1in}
\subfigure[2-2]{
\includegraphics[scale=0.3]{2_2.eps}}
\caption{Dyckpath n=2}
\end{figure}
\begin{figure}[]
\centering
\subfigure[3-1]{
\includegraphics[scale=0.3]{3_1.eps}}
\hspace{1in}
\subfigure[3-2]{
\includegraphics[scale=0.3]{3_2.eps}}
\subfigure[3-3]{
\includegraphics[scale=0.3]{3_3.eps}}
\subfigure[3-4]{
\includegraphics[scale=0.3]{3_4.eps}}
\subfigure[3-5]{
\includegraphics[scale=0.3]{3_5.eps}}
\caption{Dyckpath n=3}
\end{figure}
\begin{figure}[]
\centering
\subfigure[4-1]{
\includegraphics[scale=0.3]{4_1.eps}}
%\hspace{1in}
\subfigure[4-2]{
\includegraphics[scale=0.3]{4_2.eps}}
\subfigure[4-3]{
\includegraphics[scale=0.3]{4_3.eps}}
\subfigure[4-4]{
\includegraphics[scale=0.3]{4_4.eps}}
\subfigure[4-5]{
\includegraphics[scale=0.3]{4_5.eps}}
\subfigure[4-6]{
\includegraphics[scale=0.3]{4_6.eps}}
\subfigure[4-7]{
\includegraphics[scale=0.3]{4_7.eps}}
\subfigure[4-8]{
\includegraphics[scale=0.3]{4_8.eps}}
\subfigure[4-9]{
\includegraphics[scale=0.3]{4_9.eps}}
\subfigure[4-10]{
\includegraphics[scale=0.3]{4_10.eps}}
\subfigure[4-11]{
\includegraphics[scale=0.3]{4_11.eps}}
\subfigure[4-12]{
\includegraphics[scale=0.3]{4_12.eps}}
\subfigure[4-13]{
\includegraphics[scale=0.3]{4_13.eps}}
\subfigure[4-14]{
\includegraphics[scale=0.3]{4_14.eps}}
\caption{Dyckpath n=3}
\end{figure}
\clearpage
\begin{lstlisting}
clear all;
global path n;
path.x=1;path.y=0;path.up=0;
n=3;
dyckpath(1);
for ii=1:size(path.y,1)
figure(ii)
plot(0:2*n,path.y(ii,:),'k-'); hold on;
set(gca,'xtick',0:2*n,'ytick',0:n,'xlim',[0 2*n],'ylim',[0 n]);
title('dyck path');
grid on;hold off;
end
function []=dyckpath(row)
global path n;
if path.up(row)<n && path.y(row,path.x(row))>0
path.x=[path.x path.x(row)+1];
path.y=[path.y;path.y(row,:)];
path.up=[path.up path.up(row)+1];
path.y(end,path.x(end))=path.y(end,path.x(end)-1)+1;
dyckpath(size(path.x,2));
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)-1;
dyckpath(row);
elseif path.y(row,path.x(row))>0
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)-1;
dyckpath(row);
elseif path.up(row)<n
path.x(row)=path.x(row)+1;
path.y(row,path.x(row))=path.y(row,path.x(row)-1)+1;
path.up(row)=path.up(row)+1;
dyckpath(row);
end
end
\end{lstlisting}
\end{document}
效果图: