Skip to main content
added 131 characters in body
Source Link
halmir
  • 15.3k
  • 37
  • 54

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/1025}];

SetProperty[g, VertexCoordinates -> ncoord1]

enter image description here

SetProperty[g, VertexCoordinates -> ncoord2]

enter image description here

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/10}];

SetProperty[g, VertexCoordinates -> ncoord1]

enter image description here

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

Source Link
halmir
  • 15.3k
  • 37
  • 54

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/10}];

SetProperty[g, VertexCoordinates -> ncoord1]

enter image description here