How to remove vertex coordinates from a graph?

GROUPS:
 Szabolcs Horvát 2 Votes Given a Graph with hard-coded vertex coordinates, how can we remove those coordinates without affecting any other graph properties? I am looking for a performant solution that uses only documented functionality.Here's a simple example. This is a graph with hard-coded vertex coordinates. It also has other properties such as edge weights and styling. g = NearestNeighborGraph[Tuples[Range[5], {2}], VertexStyle -> {{3, 4} -> Red}, VertexSize -> {{4, 3} -> Medium}, EdgeWeight -> {v1_ \[UndirectedEdge] v2_ :> EuclideanDistance[v1, v2]}] I can't use Graph[g, GraphLayout -> "SpectralEmbedding"] because the coordinates are hard-coded. So how do I remove them without affecting anything else?Simple tasks should have simple solutions. What is a simple solution here?
20 days ago
4 Replies
 Marco Thiel 1 Vote Hi Szabolcs,not elegant but if I understand you correctly this might work: removeVertexCoordinates[g_Graph] := ToExpression[StringReplace[ToString[InputForm[g]], ", VertexCoordinates ->" ~~ ___ ~~ "}" -> "}" ]] So for example: Graph[removeVertexCoordinates[g], GraphLayout -> "SpectralEmbedding"] Not very elegant and probably not fast (not performant), but....Cheers,Marco
 Szabolcs Horvát 1 Vote I believe I could do Graph[VertexList[g], EdgeList[g], FilterRules[Options[g], Except[VertexCoordinates]]] but I am not sure that this qualifies as either simple, performant or documented. I do not think I have seen such a use of Options with Graph in the documentation. Can I be sure that all options are extracted, and that they are extracted in a format that can be fed back to Graph? Performance is not very good on large graphs with properties.This is basically the same problem as removing EdgeWeights to make a graph unweighted, which I asked about in the past.I am aware of some undocumented means to change graph options with good performance, e.g. the ideas shown here. But I am not really happy with these. Graph expressions were clearly not meant to be accessed this way, so if I use such hacks, they might break in the next Mathematica version. If they break, I obviously can't complain to Wolfram Support.Removing edge weights or vertex coordinates is one of the most basic tasks one might want to do with a network.I would like to know what solution Wolfram recommends for these simple tasks. Then I can use it confidently without worrying about breakage, or incorrect results due to misuse.
 Charles Pooh 3 Votes RemoveProperty removes the property associated with a vertex or edge in a graph. Options[RemoveProperty[g, VertexStyle], VertexStyle] Some properties such as VertexCoordinates, VertexWeight and EdgeWeight are not modified by RemoveProperty due to a known issue. This issue will be fixed in the coming release. Your workaround is correct: Graph[VertexList[g], EdgeList[g], FilterRules[Options[g], Except[VertexCoordinates]]]