12

I'm working on a program that finds special [called "interval"] edge-colorings for graphs. Output is as follows:

Coloring = {1 \[UndirectedEdge] 10 -> 1, 1 \[UndirectedEdge] 2 -> 2,  
            9 \[UndirectedEdge] 10 -> 2, 1 \[UndirectedEdge] 9 -> 3,  
            2 \[UndirectedEdge] 10 -> 3, 1 \[UndirectedEdge] 3 -> 4}

Now I want to visualize the coloring. I use EdgeLabels->Coloring, but it writes the labels in the middle, which is not comfortable, especially in case of symmetric graphs like Complete Graphs. I want to display 2 labels on both ends of each edge.

Right now I can display it only on one end of each edge using the following code:

1 \[UndirectedEdge] 10 -> Placed[1, 1/6]

and I get the following result:

CompleteGraph[10]

Documentation suggests to use Placed[{1,1},{"Start","End"}] to display 2 labels on two ends. That particular example works, but when I change "Start" or "End" to 1/6 or 5/6, no labels are displayed at all.

rcollyer
  • 33,976
  • 7
  • 92
  • 191

2 Answers2

16

Using GraphPlot and a custom EdgeRenderingFunction

ClearAll[erf];
erf[pos_List, lblcolors_, fontsizes_, linecolor_: Blue] := {linecolor, Line[#], 
 Sequence @@ Table[Inset[Style[Text[#3], lblcolors[[i]], FontSize -> fontsizes[[i]]], 
    pos[[i]] First@# + (1 - pos[[i]]) Last@#, Automatic, Automatic, -Subtract @@ Sort[#],
   Background -> White], {i, 1, Length@pos}]} &

Example:

edges = {{4 -> 2}, {5 -> 1}, {3 -> 2}, {2 -> 5}, {3 -> 5}};
labelededges = MapIndexed[{First@#1, ToString@First@#2} &, edges];

GraphPlot[labelededges, 
 EdgeRenderingFunction -> erf[{(5/6), 1/6}, {Green, Red}, {16, 24}],
 PlotStyle -> Thick, VertexLabeling -> True, ImagePadding -> 20]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
8

You may:

Show@Table[
  CompleteGraph[3, 
   EdgeLabels -> {1 \[UndirectedEdge] 3 -> Placed[1, p]}, 
   ImagePadding -> 10], {p, {1/6, 1/2, 5/6}}]

enter image description here

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453