\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{varwidth}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{amsthm}
\usepackage{newfloat}
\usepackage{multirow}
\usepackage{indentfirst}
\usepackage{tabularx}
\usepackage{xcolor,colortbl}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc,shapes.multipart,chains,arrows}
\tikzstyle{line} = [draw, -latex']
\tikzstyle{rect10} = [draw, rectangle, fill = red!25, text width = 8em, text centered, minimum height = 2em]
\tikzstyle{rect11} = [draw, rectangle, fill = red!45, text width = 8em, text centered, minimum height = 2em]
\tikzstyle{rect12} = [draw, rectangle, fill = blue!25, text width = 6em, text centered, minimum height = 3em]
\tikzstyle{rect14} = [draw, rectangle, fill = blue!40, text width = 8em, text centered, minimum height = 3em]
\tikzstyle{rect13} = [draw, rectangle, fill = purple!45, text width = 6em, text centered, minimum height = 4em]
\tikzstyle{diam3} = [draw, diamond, fill = green!30,text width=8em,text badly centered, inner sep=0pt]
\begin{figure}[h]
\begin{tikzpicture}
\node [rect11] (step1){Initialize
Distances};
\node [rect10, below of=step1,node distance=2cm] (step2){Initialize
Dictionary};
\node [diam3, rounded corners, below of= step2,node distance=3.5cm] (step3){Dijkstra
(V-1 times)};
\node [diam4, rounded corners, right of= step3,node distance=4cm] (step4){Update
Dictionary};
\path [line] (step1)--(step2);
\path [line] (step2)--(step3);
\path [line,rounded corners] (step3)--++(-3,-2) --++(1,1)(step4);;
\path [line,rounded corners] (step4)--+(-2,2) --(step3);
\end{tikzpicture}
\end{figure}
答案1
Ignasi 在他的评论中提到了一种可能的解决方案。另一种不依赖于两个节点之间距离的解决方案是
\path [line] (step3) to[out=-45,in=-135,looseness=2.2] (step4);
\path [line] (step4) to[out=135,in=45,looseness=2.2] (step3);
它不会产生完全相同的结果,但相当接近。
下面我还对您的代码做了一些其他修改:
将定义样式从 改为
\tikzstyle{<stylename>} = [<settings>]
。\tikzset{<stylename>/.style={<settings>}
关于这一点,请参见应该\tikzset
或被\tikzstyle
用来定义 TikZ 样式吗? 虽然没有记录,\tikzstyle
但它可以工作,并将继续工作,所以最终使用哪种语法取决于你。嵌套样式:您有多个非常相似的矩形样式。但是,您可以将一种样式放在另一种样式中,因此定义一个基本样式可能很有意义
rect
,rect/.style={draw, rectangle, text width = 8em, text centered, minimum height = 2em},
然后在此基础上创建其他样式,例如
rect10/.style={rect, fill = red!25},
利用
positioning
库。使用该库时,您要编写<position>=of <othernode>
而不是<position> of=<othernode>
。您使用的后一种语法实际上已被弃用(请参阅PGF/TikZ 中“right of=”和“right=of”之间的区别)。使用如下代码,距离是在节点边界之间测量的,因此您不必
node distance
在每种情况下指定不同的距离。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc,shapes.multipart,chains}
\tikzset{
line/.style={draw, -latex'},
rect/.style={draw, rectangle, text width = 8em, text centered, minimum height = 2em},
diam/.style={draw, diamond, rounded corners, text width=8em,text badly centered, inner sep=0pt},
rect10/.style={rect, fill = red!25},
rect11/.style={rect, fill = red!45},
rect12/.style={rect, fill = blue!25, text width = 6em},
rect14/.style={rect, fill = blue!40},
rect13/.style={rect, fill = purple!45, text width = 6em},
diam3/.style={diam, fill = green!30},
diam4/.style={diam, fill = blue!30}
}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
\node [rect11] (step1) {Initialize Distances};
\node [rect10, below=of step1] (step2) {Initialize Dictionary};
\node [diam3, below=of step2] (step3) {Dijkstra (V-1 times)};
\node [diam4, right=of step3] (step4) {Update Dictionary};
\path [line] (step1) -- (step2);
\path [line] (step2) -- (step3);
\path [line] (step3) to[out=-45,in=-135,looseness=2.2] (step4);
\path [line] (step4) to[out=135,in=45,looseness=2.2] (step3);
\end{tikzpicture}
\end{figure}
\end{document}