Draw Branch and Bound Tree Online

I would use one of the specialised tree-drawing packages. More specifically, I would use forest or, failing that, tikz-qtree. If you do this, you can specify trees very concisely and simply and easily ensure that they are consistently formatted.

In the case of Forest, the package will also do a lot of the layout work for you, although labels are not handled in this way, so this particular advantage may not be of much benefit for trees of this particular kind.

Here's an example which defines a branch and bound style for Forest. When applied to a tree, the following happens:

  • tree node, S and thick are applied to all nodes in the tree and there content is set in maths mode;

  • the edges are also thick;

  • a bit of space is added to the tree to space out the branches and the levels;

  • after the tree is parsed, the content of each node is split at each colon: <left label>:<node content>:<right label>:<edge label>;

    • The part before the first colon becomes the left label (at 170). The next part is set as the main node content (with tree node, in maths mode etc.). The third part is set as the right label (at 10). The final part becomes the label on the branch: left of the midpoint for a left-hand branch and right for a right-hand one.
  • the contents of the edge labels can be set in either text mode (text branch labels) or maths mode (maths branch labels);

    • prefixes and suffixes can be added using one of

      • set branch labels={<left branch prefix>}{<left branch suffix>}{<right branch prefix>}{<right branch suffix>}

      • set maths branch labels={<left branch prefix>}{<left branch suffix>}{<right branch prefix>}{<right branch suffix>}

      • set text branch labels={<left branch prefix>}{<left branch suffix>}{<right branch prefix>}{<right branch suffix>}

  • a horizontal line is drawn under each leaf of the tree.

Then we can specify what I think is your second tree, now edited in light of comments to vary edge labels according to level, with

          \begin{forest}   branch and bound,   where level=1{     set branch labels={x\leq}{}{x\geq}{},   }{     if level=2{       set branch labels={}{\geq y}{}{\leq y},     }{},   }   [1055.56:S:950     [1000:S_1:950:5     ]     [1033:S_2:950:6       [1033:{S_2,1}:950:1]       [950:{S_2,2}:1033:2]     ]   ] \end{forest}                  

which is obviously quite a bit more concise and produces

<code>branch and bound</code> tree

I don't know what the criteria are for this kind of tree, so this might need a little tweaking to work well. For example, perhaps the edge labels aren't always of the form x\leq and x\req, or perhaps there are sometimes only one or more than two children. However, this should at least illustrate the potential power of this approach.

Complete code:

          \documentclass[border=10pt]{standalone} \usepackage{forest} \tikzset{   tree node/.style = {align=center, inner sep=0pt, font = \scriptsize},   S/.style = {draw, circle, minimum size = 8mm, top color=white, bottom color=blue!20},   tree node label/.style={font=\scriptsize}, } \forestset{   declare toks={left branch prefix}{},   declare toks={right branch prefix}{},   declare toks={left branch suffix}{},   declare toks={right branch suffix}{},   tree node left label/.style={     label=170:#1,   },   tree node right label/.style={     label=10:#1,   },   maths branch labels/.style={     branch label/.style={       if n=1{         edge label={node [left, midway] {$\forestoption{left branch prefix}##1\forestoption{left branch suffix}$}},       }{         edge label={node [right, midway] {$\forestoption{right branch prefix}##1\forestoption{right branch suffix}$}},       }     },   },   text branch labels/.style={     branch label/.style={       if n=1{         edge label={node [left, midway] {\foresteoption{left branch prefix}##1\forestoption{left branch suffix}}},       }{         edge label={node [right, midway] {\forestoption{right branch prefix}##1\forestoption{right branch suffix}}},       }     },   },   text branch labels,   set branch labels/.style n args=4{%     left branch prefix={#1},     left branch suffix={#2},     right branch prefix={#3},     right branch suffix={#4},   },   set maths branch labels/.style n args=4{     maths branch labels,     set branch labels={#1}{#2}{#3}{#4},   },   set text branch labels/.style n args=4{     text branch labels,     set branch labels={#1}{#2}{#3}{#4},   },   branch and bound/.style={     /tikz/every label/.append style=tree node label,     maths branch labels,     for tree={       tree node,       S,       math content,       s sep'+=20mm,       l sep'+=5mm,       thick,       edge+={thick},     },     before typesetting nodes={       for tree={         split option={content}{:}{tree node left label,content,tree node right label,branch label},       },     },     where n children=0{       tikz+={         \draw [thick]  ([yshift=-10pt, xshift=-2.5pt].south west) -- ([yshift=-10pt, xshift=2.5pt].south east);       }     }{},   }, } \begin{document} \begin{forest}   branch and bound,   where level=1{     set branch labels={x\leq}{}{x\geq}{},   }{     if level=2{       set branch labels={}{\geq y}{}{\leq y},     }{},   }   [1055.56:S:950     [1000:S_1:950:5     ]     [1033:S_2:950:6       [1033:{S_2,1}:950:1]       [950:{S_2,2}:1033:2]     ]   ] \end{forest} \end{document}                  

morrisonupeopt.blogspot.com

Source: https://tex.stackexchange.com/questions/416359/branch-and-bound-tree-in-tikz

0 Response to "Draw Branch and Bound Tree Online"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel