Message Boards Message Boards

4
|
7822 Views
|
4 Replies
|
10 Total Likes
View groups...
Share
Share this post:

How to remove vertex coordinates from a graph?

Posted 7 years ago

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?

POSTED BY: Szabolcs Horvát
4 Replies

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]]]
POSTED BY: Charles Pooh

Thanks for confirming that the workaround is appropriate!

POSTED BY: Szabolcs Horvát

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.

POSTED BY: Szabolcs Horvát

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"]

enter image description here

Not very elegant and probably not fast (not performant), but....

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract