Message Boards Message Boards


Graphs with weight in 2D and 3D

Posted 11 years ago
3 Replies
7 Total Likes
Is there a Method for GraphPlot and GraphPlot3D which takes the Weight of the edges of the graph into account for positioning the vertices?

I understand that things get quickly over constrained, but isn't that what some of the method are supposed to do in the first place?

Thank you.
POSTED BY: Luc Barthelet
3 Replies
Yes, there are built in methods for this. Here is direct Documentation example for "EdgeWeighted" -> True oprion.

Consider a random graph which due to its tree nature will produce a tree-like layout with automatic settings. The edges are randomly weighted with weights shown as edge labels.

edge = RandomInteger[#] \[UndirectedEdge] # + 1 & /@ Range[0, 30];
weight = RandomInteger[{1, 5}, 31];
g = Graph[edge, EdgeWeight -> weight, GraphStyle -> "ThickEdge", EdgeLabels -> "EdgeWeight"]

SpringElectricalEmbedding and SpringEmbedding can take an additional option for to specify whether edge weight should be taken in account or not. Here is a comparison table of weighted versus not weighted layouts. The edges are labeled by their weight. In the right column larger numbers correspond to longer edges.
Grid[{{"Not Weighted", "Weighted"}}~Join~Partition[
  SetProperty[g, GraphLayout -> #] & /@
   {"SpringElectricalEmbedding", {"SpringElectricalEmbedding", "EdgeWeighted" -> True},
    "SpringEmbedding", {"SpringEmbedding", "EdgeWeighted" -> True}}
, 2], Spacings -> 0, Frame -> All, Background -> {None, {Orange}}]

Now to go to 3D we need to use the same embedding but specify dimensionality of space 3 to for the GraphEmbedding function:

 g = Graph[edge, EdgeWeight -> weight, GraphLayout -> {"SpringEmbedding", "EdgeWeighted" -> True}];
 coord = GraphEmbedding[g, Automatic, 3];
 nedges = Thread[{EdgeRules[g], PropertyValue[g, EdgeWeight]}];
  VertexCoordinateRules -> coord,
  EdgeRenderingFunction -> ({{Opacity[.4], Lighter[Blue, .5], Cylinder[#1, .05]},
                              Text[#3, (#1[[1]] + #1[[2]])/2]} &),
VertexRenderingFunction -> ({Lighter[Red, .5], Sphere[#1, .1]} &),
PlotStyle -> Directive[Specularity[White, 20]],
Lighting -> "Neutral",
Boxed -> False]

POSTED BY: Vitaliy Kaurov
Like the infix ~Join~ in your code.
POSTED BY: Seth Chandler
This is exactly what I was looking for. Perfect answer.
THANK YOU Vitaliy!
POSTED BY: Luc Barthelet
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract