Skip to content

tkz-graph examples

Shortest path (Dijkstra)

Shortest path (Dijkstra)
View TeX code
% dijkstra.tex -- shortest path from E to S with Dijkstra's algorithm,
% computed by Lua (option lua).  Engine : LuaLaTeX
\documentclass[border=3mm]{standalone}
\usepackage[lua]{tkz-graph}
\begin{document}
\begin{tikzpicture}
  \GraphInit[vstyle=Dijkstra]
  \Vertex{E}
  \Vertex[x=3,y=2]{A}  \Vertex[x=3,y=-2]{B}
  \Vertex[x=7,y=-2]{C} \Vertex[x=7,y=2]{D}
  \Vertex[x=10,y=0]{S}
  \Edge[label=$3$](E)(A)
  \Edge[label=$1$](E)(B)
  \Edge[label=$1$](A)(B)
  \Edge[label=$3$](B)(C)
  \Edge[label=$3$,style={pos=.25}](A)(C)
  \Edge[label=$5$,style={pos=.75}](B)(D)
  \Edge[label=$4$](A)(D)
  \Edge[label=$1$](S)(D)
  \Edge[label=$3$](C)(S)
  \Edge[label=$1$](C)(D)
  \tkzGraphDijkstra[style={opacity=.4}](E)(S)
  \node at (5,-3.3) {Path \tkzGraphPath, length $\tkzGraphPathLength$};
\end{tikzpicture}
\end{document}