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
Attachments:
POSTED BY: Szabolcs Horvát

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

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