12
$\begingroup$

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.

$\endgroup$
6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ didn't know Show@Table syntax :) $\endgroup$ – Hrant Khachatrian Apr 28 '12 at 20:56
  • 3
    $\begingroup$ @HrantKhachatrian Please be aware that you are building your Graph 3 times. Not a big deal for simple cases, but could be a burden if your Graph is time consuming to build up $\endgroup$ – Dr. belisarius Apr 28 '12 at 21:00
  • $\begingroup$ Yes, I guessed that. Thank you. In fact constructing the coloring takes much-much more time than drawing the graph, so it's not a problem at all. $\endgroup$ – Hrant Khachatrian Apr 28 '12 at 22:06
14
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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