This post can be seen as a follow up to Property assignment on a graph vertex using PropertyValue does not work inside a function.
I have defined a function:
Clear[AddStone];
SetAttributes[AddStone, HoldFirst];
AddStone[board_, v_, s : (white | black | empty)] := (PropertyValue[{board, v}, VertexState] = s)
This function should change the VertexState property of vertex v
in a graph board
to one of white
, black
, or empty
. It works as expected (credits to @Heike), as the execution of:
board = GridGraph[{5, 5}];
AddStone[board, 8, white]
PropertyValue[{board, 8}, VertexState]
demonstrates.
But now suppose that I need to change the property VertexState
of vertices of Graphs in a list. For example:
list = {CycleGraph[5], KaryTree[8]}
The following code doesn't work as expected:
AddStone[list[[1]], 3, white]
It returns the error:
Set::setraw: "Cannot assign to raw object PropertyValue[{list[[1]],3},VertexState]=white."
From the discussion here, I have an idea of why this happens (though a clear explanation would be appreciated). So I need an alternative way to change the properties of the graphs in a list, when all I have is the list (instead of references to the graphs themselves). I know I could apply SetProperty
to the list and obtain a new list of graphs with the updated properties, but SetProperty
is slower than PropertyValue
because it creates a new copy of the graph with the property changed instead of modifying the original graph.
Summing up, here is my question: Is there a way to use PropertyValue
to modify the properties of graphs in a list?