I love using Graph as structure to store and visualize my data in Mathematica. Consider the following simple example for creating a Graph object with weighted directed edges. I already laid out the graph in a tree structure using the options {"LayeredEmbedding", "Orientation" -> Top, "RootVertex" -> 1} I found in the documentation of GraphLayout, since this representation is useful for my particular needs.


Graph[vertices, edges, EdgeWeight -> weights, 
  EdgeLabels -> MapThread[Rule, {edges, weights}], 
  GraphLayout -> {"LayeredEmbedding", "Orientation" -> Top, 
    "RootVertex" -> 1}, EdgeLabelStyle -> Directive[Blue, 20]]

The graph that I get with these options looks like the following:

enter image description here

Edge weights are put as labels but all edges have the same length due to the options specified. What I would like to obtain now is a Graph object with edges scaled according to the edge weight while keeping the tree-like layout of the graph. I have read the documentation of GraphLayout and several other documentation pages on EdgeWeight, TreeGraph, etc. The only possibility I found so far is to use the options {"SpringElectricalEmbedding", "EdgeWeighted"->True} for the Graph object. However this destroys the tree-like layout I want to keep:

 Graph[vertices, edges, EdgeWeight -> weights, 
 EdgeLabels -> MapThread[Rule, {edges, weights}], 
 GraphLayout -> {"SpringElectricalEmbedding", "EdgeWeighted" -> True},EdgeLabelStyle -> Directive[Blue, 20]]

And the Graph object looks like this:

enter image description here

Question: Is there a way to combine the tree-like layout of Graph with weight-scaled edges in Mathematica?

I think one would have to change the VertexCoordinates that are embedded in the Graph. It is easy to access the current VertexCoordinates using GraphEmbedding but I can't figure out how to change them appropriately.

  • $\begingroup$ To my knowledge only SpringEmbedding and SpringElectricalEmbedding take EdgeWeighted. More details here: Graphs with weight in 2D and 3D $\endgroup$ Commented Jul 29, 2013 at 2:07
  • $\begingroup$ @VitaliyKaurov: Thanks for the hint. I also found the option {"SpringElectricalEmbedding", EdgeWeighted"->True} for GraphLayout (see second graph above). But, I would like to keep a tree-like structure of the Graph while using the edge weights. $\endgroup$
    – g3kk0
    Commented Jul 29, 2013 at 8:54
  • 2
    $\begingroup$ Nice idea, but this might be a difficult feature to add: suppose your 53->55 was 'length' 1, and 53->68 was 167 - the graph would become unusable almost immediately. $\endgroup$
    – cormullion
    Commented Jul 31, 2013 at 16:52

1 Answer 1


Like you mention, you could try to modify existing coordinates. Since your graph is tree, BreadthFirstScan can be used to search a graph and update coordinates.

What I did is to keep accumulating weights based on dfs and adding or replace those value to only y-coordinates of the existing coordinates (updating x coordinate could mess up tree structure easily, so I didn't try that).

g = Graph[vertices, edges, EdgeWeight -> weights, 
  EdgeLabels -> MapThread[Rule, {edges, weights}], 
  GraphLayout -> {"LayeredEmbedding", "Orientation" -> Top, 
    "RootVertex" -> 1}, EdgeLabelStyle -> Directive[Blue, 20], 
   VertexLabels -> "Name"];

coords = GraphEmbedding[g];
update[1] = 0;
BreadthFirstScan[g, 1,
  "DiscoverVertex" -> (w = PropertyValue[{g, #2 \[DirectedEdge] #1}, EdgeWeight];
   If[NumberQ[w], update[#1] = update[#2] + w]; &)];

add = update /@ VertexList[g];
{x, y} = Transpose[coords];
y = y - add/50;
ncoord1 = Transpose[{x, y}];
ncoord2 = Transpose[{x,-add/25}];

SetProperty[g, VertexCoordinates -> ncoord1]

enter image description here

SetProperty[g, VertexCoordinates -> ncoord2]

enter image description here

  • $\begingroup$ Interesting approach, thanks very much! The problem I see is that the edges are indeed scaled by some factor, but it doesn't directly represent the edge weights that are written on the edges. Look at the lower part (vertices 65,67,61,63 to 66,68,62,64) for example. There you have edges with weights 17 and 7, respectively. But, the edge length looks about the same whereas the difference is 10. The left edges should therefore be at least twice as long. $\endgroup$
    – g3kk0
    Commented Sep 26, 2013 at 7:09
  • $\begingroup$ @g3kk0 That's why I put two different ncoords. Try ncoord2. $\endgroup$
    – halmir
    Commented Sep 26, 2013 at 11:59
  • $\begingroup$ Thanks for that information and the edit. This looks just like what I was looking for ;) $\endgroup$
    – g3kk0
    Commented Sep 26, 2013 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.