Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to visualize the difference between members of a set:

set = {20, 36, 70, 96, 152, 301}

This way I get the diffs:

diffs = Select[#[[2]] - #[[1]] & /@ Tuples[{set, set}], # > 0 &]

{16, 50, 76, 132, 281, 34, 60, 116, 265, 26, 82, 231, 56, 205, 149}

But how can I put the diffs as a caption over graph like lines between members?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Borrowing some code from Kuba's:

set = {20, 36, 70, 96, 152, 301}
Graph[DirectedEdge @@@ #,
   VertexShapeFunction -> "Square",
   VertexSize -> {.2, .1},
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> Directive[FontFamily -> "Arial", 10], 
   GraphLayout -> "CircularEmbedding",
   EdgeLabels -> ((DirectedEdge[##] -> Placed[#2 - #, .85]) & @@@ #)] &@Subsets[set, {2}]

Mathematica graphics

Edit

In fact, you could apply any nice formatting to the edges. The following applies a color proportional to its value:

set = {20, 36, 70, 96, 152, 301};
{min, max} = {Min@#, Max@#} &[Subtract @@@ -Subsets[set, {2}]]
Graph[Property[DirectedEdge @@ #, 
               EdgeStyle -> {Thick, ColorData["Rainbow"][
                             Rescale[#[[2]] - #[[1]], {min, max}]]}] & /@ #,
   VertexShapeFunction -> "Square",
   VertexSize -> {.2, .1},
   VertexLabels -> Placed["Name", Center], 
   VertexLabelStyle -> Directive[FontFamily -> "Arial", 10], 
   GraphLayout -> "CircularEmbedding", 
   EdgeLabels -> ((DirectedEdge[##] -> Placed[#2 - #, .85]) & @@@ #)] 
 &@Subsets[set, {2}]

Mathematica graphics

share|improve this answer
set = {20, 36, 70, 96, 152, 301}

Graph[DirectedEdge @@@ #, EdgeLabels -> ((DirectedEdge[##] -> (#2 - #)) & @@@ #), 
                          VertexLabels -> Thread[set -> set], EdgeLabelStyle -> Bold, 
                          VertexLabelStyle -> Directive[Bold, 20]
     ] &[Tuples[set, {2}]]

enter image description here

 Graph[DirectedEdge @@@ #, EdgeLabels -> ((DirectedEdge[##] -> (#2 - #)) & @@@ #), 
                           VertexLabels -> Thread[set -> set], 
                           EdgeLabelStyle -> Directive[Bold, 15], 
                           GraphLayout -> "StarEmbedding", ImagePadding -> 35, 
                           VertexLabelStyle -> Directive[Bold, 20]
      ] &[Subsets[set, {2}]]

enter image description here

share|improve this answer
    
I just want one path between two nodes. How can I remove the negative ones? –  Mohsen Afshin Dec 20 '13 at 12:43
3  
@MohsenAfshin You could try Subsets[set, {2}] rather than Tuples... –  cormullion Dec 20 '13 at 12:54
    
I did, but the visual output is not as readable as what @Kuba rendered, how can I force the circular rendering? –  Mohsen Afshin Dec 20 '13 at 13:02
    
@MohsenAfshin take a look at GraphLayout, e.g "CircularEmbedding" –  Kuba Dec 20 '13 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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