Message Boards Message Boards

0
|
1880 Views
|
0 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Graphs: mapping properties

Posted 9 years ago

Suppose I import a graph with properties. There's an example at the end of this post in GraphML format, which you can copy, paste and g = ImportString[..., "GraphML"].

How can I map one vertex property or edge property to another, Mathematica compatible one? For example, what is the simplest way to use the "Origin" property from the example below as the VertexLabel of each vertex?

One solution is

Graph[g, VertexLabels -> Table[v -> PropertyValue[{g, v}, "Origin"], {v, VertexList[g]}]]

but this is quite cumbersome. If I tried to make to transfer to another custom property instead of the standard VertexLabels one, it would be even more cumbersome. Is there a better way?


Custom graph properties generally seem quite cumbersome to use in Mathematica. My impression is that they are not really meant to be used while programming in Mathematica. E.g. it is not a good idea to use them to store some information relate to the graph. A separate data structure, such as an association that maps vertices to values, would be better. Instead their main purpose seems to be to allow importing from graph file formats that have properties.

Am I mistaken about this? Any thoughts? While working on IGraph/M I'm often tempted to use custom properties, but I'm not sure this is the right way to go. Instead of having a function that returns a graph with properties, I could instead simply have something like <| "Graph" -> ..., "SomeVertexProperty" -> <| v1 -> value1, ... |> |>, which is easier to handle with the current version of Mathematica. Future versions might improve. Comments and suggestions on this would be most welcome.


<!-- language: lang-xml -->
<?xml version='1.0' encoding='UTF-8'?>
<graphml>
 <key id='nodeKey1'
     for='node'
     attr.name='Origin'
     attr.type='String' />
 <key id='nodeKey2'
     for='node'
     attr.name='VertexCoordinates'
     attr.type='String' />
 <graph id='Graph1'
     edgedefault='directed'>
  <node id='1'>
   <data key='nodeKey1'>a</data>
   <data key='nodeKey2'>List[0.47140452079103173`, 1.885618083164127`]</data>
  </node>
  <node id='2'>
   <data key='nodeKey1'>b</data>
   <data key='nodeKey2'>List[0.`, 0.9428090415820635`]</data>
  </node>
  <node id='3'>
   <data key='nodeKey1'>c</data>
   <data key='nodeKey2'>List[0.9428090415820635`, 0.9428090415820635`]</data>
  </node>
  <node id='4'>
   <data key='nodeKey1'>d</data>
   <data key='nodeKey2'>List[0.9428090415820635`, 0.`]</data>
  </node>
  <node id='5'>
   <data key='nodeKey1'>c</data>
   <data key='nodeKey2'>List[0.`, 0.`]</data>
  </node>
  <edge id='e1'
      source='1'
      target='2' />
  <edge id='e2'
      source='2'
      target='5' />
  <edge id='e3'
      source='1'
      target='3' />
  <edge id='e4'
      source='3'
      target='4' />
 </graph>
</graphml>
POSTED BY: Szabolcs Horvát
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