Group Abstract Group Abstract

Message Boards Message Boards

6
|
5.7K Views
|
3 Replies
|
16 Total Likes
View groups...
Share
Share this post:

[FEATURE] Functions for graph property transformations

Posted 9 years ago
POSTED BY: Szabolcs Horvát
3 Replies

I did not realize before that vertex properties such as Tooltip, StatusArea, Button, etc. have a special interpretation. I thought that to create a tooltip over a vertex, we had to use Placed[..., Tooltip] in the VertexLabels property. That is not the case. Thus the tooltip example in the notebook above simplifies to

IGVertexMap[Identity, Tooltip -> IGVProp["FullName"]]@
   ExampleData[{"NetworkGraph", "EastAfricaEmbassyAttacks"}]

Additional question:

Would people prefer a short name such as IGVProp, which is hopefully easy to type, or something more descriptive, such as IGVertexProperties (because auto-completion takes care of input anyway)?

POSTED BY: Szabolcs Horvát

I attached a new implementation which is still not great, but it is practically usable (faster than the above one). I intend to include this in the next version of IGraph/M (update: available now), so any feedback is most welcome.

I am looking for feedback on:

  • The API, i.e. the calling syntax of the functions. Is it convenient? Can it be improved? Is it more convenient for common use cases than rare ones? Is it sufficiently general?

  • The implementation. I am still not satisfied with the performance, and I still do not have a perfect way of setting properties in bulk (especially special ones, like EdgeWeight).

What changed since the above:

  • The argument order is now IGVertexMap[function, propertySpecification, graph]
  • There is an operator form for convenient chaining: IGVertexMap[function, propertySpecification][graph]
  • The property specification is now newProperty -> propertyFunction, i.e. the ordering is now reversed. Do you find this more intuitive, or more in line with what Mathematica already has? The reasoning is that -> is similar to =, i.e. the LHS is "set" to the RHS.
  • The RHS of the property specification must be a function or a list of functions. It cannot be a property name. This is because I cannot distinguish between names and functions, and functions are very useful in this context. Property extractor operators are provided to create a function from a property name: IGVProp[VertexWeight][graph] extracts the vertex weights into a list.

Take a look at the attached notebook, which has a few usage examples.

Attachments:
POSTED BY: Szabolcs Horvát

To make this more concrete, and encourage discussion, here is a proof of concept implementation that is slightly different from and slightly simpler than what I described above.

Its main limitations:

  • bad performance
  • no multigraph support

Here's how it works:

vprop[p][g] will extract the vertex property p of graph g as a list. eprop is similar, but for edges.

We can use it as follows. This is a graph with an EdgeCapacity set.

rg = RandomGraph[{10, 20}, EdgeCapacity -> RandomReal[1, 20]];

Let us transform is as -Log[#]& and transfer it to EdgeWeight:

rg2 = edgeMap[-Log[#] &, rg, eprop[EdgeCapacity] -> EdgeWeight]

PropertyValue[rg2, EdgeWeight]    
(* {1.0623, 1.07053, 0.852909, 0.0794914, 0.223707, 1.11785, \
0.757396, 1.52089, 1.01736, 0.563396, 0.854208, 0.859467, 0.446242, \
0.319857, 1.15751, 0.465928, 1.48121, 1.1758, 1.02566, 0.999962} *)

In this implementation, the LHS of -> is always a function that can be applied to a graph. Thus we cannot use EdgeCapacity -> EdgeWeight directly. We must transform EdgeCapacity to a function by wrapping it with the edge property extractor: eprop[EdgeCapacity] is now a proper function.

Similarly, we can size vertices based on their degree:

vertexMap[
 .3 #^0.5 &,
 RandomGraph@BarabasiAlbertGraphDistribution[50, 1],
 VertexDegree -> VertexSize
 ]

enter image description here


(* no support for multigraphs *)
graphQ = GraphQ[#] && Not@MultigraphQ[#] &;

(* extract vertex properties *)
vprop[prop_][g_?graphQ] := 
 Replace[PropertyValue[{g, #}, prop] & /@ VertexList[g], $Failed -> 
   Missing["Nonexistent"], {1}]

(* extract edge properties *)    
eprop[prop_][g_?graphQ] := 
 Replace[PropertyValue[{g, #}, prop] & /@ EdgeList[g], $Failed -> 
   Missing["Nonexistent"], {1}]

(* set vertex properties *)
setVProp[g_?graphQ, prop_, values_] :=
 If[
  Length[values] =!= VertexCount[g],
  $Failed,
  Fold[SetProperty[{#1, #2[[1]]}, prop -> #2[[2]]] &, g, 
   Transpose[{VertexList[g], values}]]
  ]

(* set edge properties *)
setEProp[g_?graphQ, prop_, values_] :=
 If[
  Length[values] =!= EdgeCount[g],
  $Failed,
  Fold[SetProperty[{#1, #2[[1]]}, prop -> #2[[2]]] &, g, 
   Transpose[{EdgeList[g], values}]]
  ]

ClearAll[vertexMap]
vertexMap[fun_, g_?graphQ, prop_] := 
 vertexMap[fun, g, vprop[prop] -> prop]
vertexMap[fun_, g_?graphQ, gfun_ -> prop_] := 
 setVProp[g, prop, Map[fun, gfun[g]]]
vertexMap[fun_, g_?graphQ, gfuns_List -> prop_] := 
 setVProp[g, prop, MapThread[fun, Through[gfuns[g]]]]

ClearAll[edgeMap]
edgeMap[fun_, g_?graphQ, prop_] := 
 vertexMap[fun, g, eprop[prop] -> prop]
edgeMap[fun_, g_?graphQ, gfun_ -> prop_] := 
 setEProp[g, prop, Map[fun, gfun[g]]]
edgeMap[fun_, g_?graphQ, gfuns_List -> prop_] := 
 setEProp[g, prop, MapThread[fun, Through[gfuns[g]]]]
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