Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Depth first search (recursive)

Subject: Depth first search (recursive)

From: Liana

Date: 14 Oct, 2011 23:45:30

Message: 1 of 2

Hi,

I wrote DFS code that should print all paths from 'start' to 'goal' point. This code works well for small dimensions. However, if the size of E is 4500x2, then it asks to increase the RecursionLimit. I tried to increase it, but then Matlab stopped working. How can I solve this issue?

function dfs(E,start,goal)
visitNode([],E,start,goal)

function visitNode(path,E,node,goal)
path = [path node];
if isequal(node,goal)
   disp(path);
else
   neigh = find(E(:,1)==node);
   len = length(neigh);
   for i=1:len
      visitNode(path,E,E(neigh(i,1),2),goal)
   end
end

Subject: Depth first search (recursive)

From: Liana

Date: 15 Oct, 2011 00:20:30

Message: 2 of 2

I read similar threads in News Group, but the only suggestions were to avoid the recursion or increase recursion limit. The first solution is not fine for my case, because I am looking for all paths from Start to Goal.

"Liana" wrote in message <j7ahiq$fmj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I wrote DFS code that should print all paths from 'start' to 'goal' point. This code works well for small dimensions. However, if the size of E is 4500x2, then it asks to increase the RecursionLimit. I tried to increase it, but then Matlab stopped working. How can I solve this issue?
>
> function dfs(E,start,goal)
> visitNode([],E,start,goal)
>
> function visitNode(path,E,node,goal)
> path = [path node];
> if isequal(node,goal)
> disp(path);
> else
> neigh = find(E(:,1)==node);
> len = length(neigh);
> for i=1:len
> visitNode(path,E,E(neigh(i,1),2),goal)
> end
> end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us