Creating Recursive VIs

Publish Date: Nov 26, 2018 | 8 Ratings | 4.38 out of 5 | Print

Overview

A recursive function calls itself as a part of its own definition. Recursion is an advanced programming concept that can be used to reduce the complexity and size of code required to implement iterative algorithms. This article will explain how VIs can be called recursively in LabVIEW 2009 or later.

Table of Contents

  1. Other Examples
  2. Related Links

Using a VI Recursively

There are several well-known algorithms that are commonly implemented using recursion, including a binary tree, a fibonacci sequence, or the calculation of a factorial, as shown below. A reentrant VI can be placed onto its own block diagram for use in a recursive algorithm by dragging the icon of a VI to its own block diagram.

Figure 1: Click and drag the icon of a VI to its own block diagram to call it recursively

A copy of the VI is loaded into memory when it calls itself.  Additional memory for new copies of the VI is allocated dynamically until the base case is reached.  Successful use of recursion requires a base case exist that discontinues the recursive call.  LabVIEW imposes an limit of 15,000 recursive calls on a 32-bit machine to prevent large memory leaks.

Recursion Example

The method to insert a value into a binary tree can be implemented using recursion, as shown below. Binary trees are common ways of storing and sorting data to increase the efficiency of traversing the tree to find a value.  Each node of the tree has two children, each of which have their own children, and so on. In this implementation, items to the left of the current node have a lesser value and items on the right of the current node have a greater value.

Figure 2: The value of the current node is compared with the new value until an empty child is found and it is inserted to the tree.

The process of inserting a new, unique element into the tree requires the following sequence of steps, which are called recursively.

  1. Compare the new value with the current node
  2. If the current node is empty, insert new value and stop.
  3. If less than current value, retrieve left child
  4. Else, retrieve value of right child
  5. Call step 1.

Note that step two is the base case for this algorithm.  The resulting tree is shown below.

Figure 3: Note that a child on the left is always less than the parent and the child on the right is always greater than the parent

Configuring a VI for Re-Entrant Execution

Reentrancy must be enabled for an individual VI before it can be called recursively.  To do this, right-click on the VI icon and go to 'Properties.'  Select 'Execution' from the drop down and make sure that 'Reentrant execution' is checked and Share clones between instances is selected as shown below.

Figure 4: Ensure that the VI Properties for Reentrant execution are configured as shown to call a VI recursively

Shared reentrancy uses a cache of VI instances to handle a dynamically determined number of simultaneous calls.

1. Other Examples

The following demonstrate the implementation of common recursive algorithms using LabVIEW.

Fibonacci Sequence

Math - Recursion:

               

 

Block diagram:

Ackerman

Math - Recursion:

               

Block diagram:

 

Note: In order to create the third branch of the function (), there are 2 cascaded VIs, embedded in the initial one. The Formula Node preceding Case Structure has to generate 0,1 and 2 values according to m and n values.

Greatest Common Divisor

Math - Recursion:

               

 

Note: The Formula Node preceding the Case Structure produces 0 and 1 values (used for select specific cases) according x and y values. 

Back to Top

2. Related Links

To see everything that's new in LabVIEW, visit ni.com/labview/whatsnew

To try this and other new features, download LabVIEW to evaluate it for free

 

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit