I am trying to write a function that builds a graph, or - better said - adds a layer of leaves to a pre-existing graph.
Basically, I hand to the function three lists. edges
is the "already existing (upper) part of the tree", nodeslvl1
includes all its leaves, nodeslvl2
are the new leaves, which should stem from the ones in nodeslvl1
according to a certain rule.
The function should append the new edges of the tree to the ones already existing in the original edges
. Finally, it should graph the tree.
Here's my code:
graphfrom2lists[edges_List, nodeslvl1_List, nodeslvl2_List] :=
Module[{i, l = Length[nodeslvl1[[1]]], pos},
For[i = 1, i < Length[nodeslvl2] + 1, i++,
pos = Position[nodeslvl1, nodeslvl2[[i,2;;l+1]]][[1,1]];
AppendTo[edges, nodeslvl1[[pos]] -> nodeslvl2[[i]]];
Print["edges becomes ", edges];
];
Graph[edges, VertexLabels -> "Name"]
]
When I try to run the code on an easy example (*), I get the following error:
AppendTo::rvalue: {root->{2},root->{4},root->{6}} is not a variable with a value, so its value cannot be changed.
I cannot understand the meaning of it, since my list of edges has a name, it's not like doing AppendTo[{1,2,3},4]
!
Also, my code doesn't actually seem different from this other (properly working) one, which does the same thing, but building just the first two layers of such a tree.
graphfrom2listsbasic[nodeslvl1_List, nodeslvl2_List] :=
Module[{edges = {}, i, j, pos},
For[i = 1, i < Length[nodeslvl1] + 1, i++,
AppendTo[edges, "root" -> nodeslvl1[[i]]]
];
Print["edges is ", edges];
For[j = 1, j < Length[nodeslvl2] + 1, j++,
pos = Position[nodeslvl1, nodeslvl2[[j, 2 ;; 2]]][[1, 1]];
AppendTo[edges, nodeslvl1[[pos]] -> nodeslvl2[[j]]]
];
Print["edges is ", edges];
Graph[edges, VertexLabels -> "Name"]]
So what am I missing about the AppendTo command?
Thanks in advance!
(*) Here's the example, which is expected to produce the same result with both functions:
nodes1 = {{2}, {4}, {6}};
nodes2 = {{1, 2}, {1, 4}, {3, 4}, {1, 6}, {3, 6}, {5, 6}};
edgespartial = {"root" -> {2}, "root" -> {4}, "root" -> {6}};
graphfrom2listsbasic[nodes1,nodes2]
graphfrom2lists[edgespartial, nodes1, nodes2]
x={1,2}; addone[x]
, the value ofx
will have changed to, say,{1,2,1}
. This is howAppendTo
works. The more usual way of working in Mathematica is to leavex
alone and return a modified list instead, just likeAppend
does. Both solutions are possible though. Do I understand it right that you want to use the first one? $\endgroup$edges
, adding the additional connections for my expanded graph. $\endgroup$e2 = edges
in the first argument insideModule
(Module[{e2=edges, ...},...]
, and changeedges
toe2
in the rest (AppendTo[e2,...]
andGraph[e2,...]
. $\endgroup$AppendTo
needs to have theHoldFirst
attribute whileAppend
doesn't. See also here. Then we can go from there. In short,f[x_]:=AppendTo[x,1]; arr={3,2}; f[arr]
will evaluate asf[arr]
->AppendTo[{3,2}, 1]
, which causes an error. TheHoldFirst
... $\endgroup$