Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursive functions and global variables

Status
Not open for further replies.

antiseptic

Bioengineer
Jan 15, 2003
11
hi all
i am trying to design a recursive function that would "spawn" a child process everytime a condition is met

to keep it simple:

function output = recursive_function(parameters)

if (condition_is_met)
recursive_function(parameters)
end

output=whatever;

i want to "keep track" of the "spawned childs", that is, assign an index to them (lets say "i") that gets incremented with each new child

i thought i could do that defining a "global" variable to the function, but i cant get it done

can anyone help me?

thanx
 
Replies continue below

Recommended for you


Couldn't the "parent" give the "child" its index? For example, the following case for calculating a factorial recursively:

===============================================
function out=fact(N,pid)

if (~exist('pid','var')) %first one
if (~exist('N','var'))
error('usage: x=fact(N)');
end
pid=1;
end

fprintf(1,'Processing "child" #%d...\n',pid);
if (N<2), out=1;
else, out=N*fact(N-1,pid+1);
end
fprintf(1,'Ending &quot;child&quot; #%d...\n',pid);
return;
===============================================
 
i thought about that, but there is a problem with that
(keep in mind mebbe my algorithm design is faulty... :-S)

the fact is i have a top-down tree
_ node 4
node 2 |- node 5
node 1 -|
node 3 |- node 6
- node 7

i want to run through all the possible paths (1-2-4, 1-2-5, etc...)

for that i use a recursive function that looks out for the next node
if there are 2+ nodes, it spawns a child for every node over the first and then the recursive funcion is called from there to keep on looking

pretty straightforward
but, to keep the count of childs...

if the parent passes the child its index, in some trees some indexes take the same value!!

example (same tree as above)

_ node 4
node 2 |- node 5
node 1 -|
node 3 |- node 6
- node 7

node 1 has two descendants (2 and 3)
so one child is spawned
we increment the counter, now i=2
and the recursive function called and i is &quot;given&quot; to the child

node 2 has 2 descendants (4 and 5)
so one child is spawned
now i=3
and again the recursive function is called and i (=3) given

but then node 3 has 2 descendants
now when the child is called, i = 2!!
for he has no way to know (in my algorithm) that node 2 had descendants too...

the fact is i need the program to &quot;remember&quot; that the variable i was increased, and short of passing it as reference (which i think matlab cant do) i dont know how to do it




 
How about:

==============================================
function output=recursive_function(parameters)
global idx;
if (isempty(idx)), idx=1;
else, idx=idx+1;
end

if (condition_is_met)
recursive_function(parameters)
end

output=whatever;
==============================================

Remember that since idx is global, if you don't clear it between calls, it'll retain the same value. You can fix this with the following, but it makes the code a little cruddier:

function output=recursive_function(parameters,firsttime)
if (~exist('firsttime','var')), clear global idx; end
global idx;
.
.
if (condition_is_met)
recursive_function(parameters,0)
end
.
.
==============================================

Am I missing your question, or is this what you're looking for?
 
Hi,
First consider using persistent rather than global.
Second, if you want to keep track of different branches you can do on of two options:
1) Add the node number to a list.
2) Use a structure variable either:
a) A field for each node (to accumulate the number of visits of the node).
Or:
b) Create a new field every time a new process is starts (assuming a new process is always starts at node1).
For option a the function may look like that:
function output=recursive_function(parameters)
persistent BookKeeper
if isempty(BookKeeper)
BookKeeper=struct('Nod1',1,'Node2',0,....);
end
<choose where to go>
switch go_to
case 1
BookKeeper.Node1=BookKeeper.Node1+1;
< do task 1>
recursive_function(parameters)
case 2
BookKeeper.Node2=BookKeeper.Node2+1;
.
.
end

For option b you can create a structure with one field and duplicate it at node1.The main scheme may be:
....

case1
n = length(BookKeeper)
BookKeeper(n+1).Run = 1;
...
case n % for any n>1
BookKeeper.Run(end) =[ BookKeeper.Run(end) n];
....
.
.
end

I hope this helps.

Joe
BSTeX- Equation viewer for Matlab
Joe
BSTeX- Equation viewer for Matlab
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor