Re: Return a value from function
- To: mathgroup at smc.vnet.net
- Subject: [mg97724] Re: Return a value from function
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 19 Mar 2009 02:14:43 -0500 (EST)
- Organization: Uni Leipzig
- References: <gpqgd4$5tq$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi,
a pure function is not the optimal way for recursive calls
because the function has no name and so it is difficult
to call a nameless function. Only pure functions without
argument list can do that because Slot[0] is the function itself
so that
myfac = (If[0 === #1, 1, #1 #0[#1 - 1]]) &
compute the factorial.
And your pure function make the code unnecessary complicated and
more over, if you plan to use the function recursive you should
create local values with Module[] or Block[].
Regards
Jens
Mite wrote:
> I'm using Mathematica 6. Trying to count the Maximum Independent Vertex Set in a graph g. I'm using recursive calls to a function. I think that when I call a return on a function that is called from its recursive predecesor, the two functions finish. Is that possible, or is it only me thinking wrong. If its possible, how can i solve this. Here is the code:
>
> Needs["Combinatorica`"]
> g = CompleteGraph[5];
> g = DeleteEdge[g, {1, 2}];
> g = DeleteEdge[g, {3, 2}];
> g = DeleteEdge[g, {1, 3}];
> g = DeleteEdge[g, {1, 5}];
> g = DeleteEdge[g, {3, 5}];
> g = DeleteEdge[g, {2, 5}];
>
> ShowLabeledGraph[g]
> n1 = 0;
> n2 = 0;
> func = Function[g1,
> deg = 0;
> degrees = Degrees[g1];
> temp = 0;
> For[i = 1, i <= V[g1], i++,
> If[degrees[[i]] > 0, deg = deg + degrees[[i]];]];
> deg = Divide[deg, 2];
> Print["Degree: ", deg];
> v = V[g1];
> If[deg == 0, Print["true, V[G] = ", v]; Return[v];
> Print["Ret ne rabote"];,
> Print["false, V[G] = ", v];
> v = V[g1];
> Print["Izbrana tocka ", v];
> n1 = func[DeleteVertex[g1, v]];
> Print["N1 = ", n1];
> n2 = func[DeleteVertices[g1, Neighborhood[g1, v, 1]]];
> Print["N2 = ", n2];
> Print["Max = ", Max[n1, n2]]
> Return[Max[n1, n2]]];];
> Print[func[g]]
>