MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: mma problem...

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg558] Re: [mg547] mma problem...
  • From: withoff (David Withoff)
  • Date: Thu, 16 Mar 1995 10:59:43 -0600

> If any body would like to give this a try..................
> ---------------------
> >Forwarded message:
> >Subj:    mma problem...
> >Date:    95-03-14 12:35:16 EST
> > From:    Patcavana
> 
> This is a mma notebook with the function StartNet defined below. This
> function is for creating a starting topology for a network design algorithm.
> There seems to be a problem starting with the first For statement. For
> some reason when i = 2, the body of the For is skipped. (see output below).
> Also, it gets stuck on i=5. I don't know really what the problem is.
> I've tried the same loop structures with different bodies and it did work. I
> tried eliminating the parantheses with no difference. I've provided as
> much information to help understand the code. 
> 
> Remove[StartNet]
> 
> StartNet[net1_Graph]:=
> 	Block[{g,m,adj,sublist,mndlst,co,l,len},
> 	   g=MinimumSpanningTree[net1];ShowLabeledGraph[g];
> 	   Print["Edges[g] = ",TableForm[Edges[g]]];
> 	   m=NumberOfNeighbors[g];Print["m = ",m];
> 	   adj=Neighbors[g];Print["adj = ",adj];
> 	   co=Edges[net1];Print["co = ",TableForm[co]];
> 	   len=Length[m];Print["Length[m] = ",len];
> 	   For[i=1,i<=len,++i,
> 		(Print["i = ",i]; sublist=ListSubsets[m,adj,i];
> 		 Print["sublist = ",sublist];
> 		  If[sublist=={},Print["sublist empty, i = ",i],
> 		    (mndlst=MinimumNeighborDistance[co,sublist];
> 		      Print["mndlst = ",mndlst];
> 			l=Length[mndlst];Print["l = ",l];
>		       	If[l<2,(Print["mndlst<2"];g=AddEdge[g,Flatten[mndlst]];
> 			   Print["Edges[g] = ",TableForm[Edges[g]]]),
> 			   (Print["mndlst>=2"];Print["l = ",l];
> 			   For[j=1,j<=Length[mndlst],++j,Print["j = ",j];
> 				g=AddEdge[g,Flatten[mndlst[[j]]]]
> 
> 				])])])];
> 	ShowLabeledGraph[g]
> 	]	
> 		
> MinimumNeighborDistance[co_List, sublist_List] := 
>   Block[{len, minm, minpair, ii, jj}, 
>    minm = 1000; minpair = {{}}; len = Length[sublist]; 
>     For[i = 1, i <= len, ++i, ii = sublist[[i,1]]; jj = sublist[[i,2]]; 
>       If[co[[ii,jj]] == minm, minpair = Append[minpair, {ii, jj}]]; 
>       If[co[[ii,jj]] < minm, minm = co[[ii,jj]]; minpair = {{ii, jj}}]];
> minpair]

The variable i in the For loop in MinimumNeighborDistance conflicts
with the variable i in the For loop in the definition of StartNet.
Try using different names for the two local variables, or better
yet (much better), try localizing both variables.  That is, include
the loop index in the list of localized variables in Block.

For example:

  MinimumNeighborDistance[co_List, sublist_List] :=
    Block[{len, minm, minpair, ii, jj, i},
     minm = 1000; minpair = {{}}; len = Length[sublist];
      For[i = 1, i <= len, ++i, ii = sublist[[i,1]]; jj = sublist[[i,2]];
        If[co[[ii,jj]] == minm, minpair = Append[minpair, {ii, jj}]];
        If[co[[ii,jj]] < minm, minm = co[[ii,jj]];
                               minpair = {{ii, jj}}]]; minpair]

and the same change in StartNet.  This should fix the problem.

In debugging programs like this, it is extremely helpful if you
can first discard everything that you know is not causing the problem,
to get down to the simplest example that behaves in an unexpected way.
It is also helpful, especially if you want others to help with the
debugging, if you can explain in a sequential way exactly what you did.
It wasn't immediately obvious, for example, that you had loaded the
DiscreteMath`Combinatorica` package.

If you are a registered user of a supported version of Mathematica,
questions like this can also be sent to technical support.  Not
surprisingly, questions that are narrowed down before they are sent
to us can be answered more quickly than questions that are a bit raw.

Dave Withoff
Research and Development
Wolfram Research


  • Prev by Date: Re: auto notebook running
  • Next by Date: New Book - MATHEMATICA FOR PHYSICS
  • Previous by thread: mma problem...
  • Next by thread: [VOTE} Moderate news group