I have a directed graph each of whose nodes belongs to exactly one of three groups: "sources", "sinks", and "both". As these names suggest, nodes in the "sources" group have only outgoing edges; those in the "sinks" group have only incoming edges; and those in the "both" group can have both outgoing and incoming edges.

I want to lay out this graph using a radial embedding, with the nodes arranged in three concentric circles depending on which group they belong to: "sinks" in the outermost circle, "sources" in the innermost circle, and "both" in the circle in-between.

If I use the option GraphLayout → "RadialEmbedding" I indeed get a radial layout for the graph, but I don't get to specify which nodes go where (nor, for that matter, the number of possible radii from the center). Is there a way to do this?

(Note, as long as the nodes get placed along the appropriate circles, I'd like to leave it to the layout algorithm to optimize the relative position of the nodes so as to minimize the number of edge-crossings.)

  • 1
    $\begingroup$ You can choose the root node as a suboption as in GraphLayout -> {"RadialEmbedding", "RootVertex" -> "a"}. See, for example, this answer to a related question. $\endgroup$
    – kglr
    Commented Oct 28, 2014 at 14:00
  • $\begingroup$ if you can add some examples, that will be very helpful... $\endgroup$
    – halmir
    Commented Oct 28, 2014 at 14:48

3 Answers 3

circularKPartiteF = With[{rl = Range[Length@{##}]}, 
  Join @@ (2^rl (GraphComputation`CircularEmbedding /@ {##}))] &;

Example 1: a complete graph

Graph[Range[25], EdgeList[CompleteGraph[{3, 7, 15}]],
 VertexSize -> Large, 
 VertexStyle -> Join[Thread[Range@3 -> Red], Thread[Range[4, 10] -> Green],
  Thread[Range[11, 25] -> Blue]],
 VertexCoordinates -> circularKPartiteF[3, 7, 15]]

enter image description here

Example 2: David's example

Graph[Range@40, DeleteDuplicates[Join[myFirstRules, mySecondRules]],
 VertexSize -> Large, 
 VertexStyle -> Join[Thread[mySources -> Red], 
  Thread[myBoth -> Green], Thread[mySinks -> Blue]], 
 VertexCoordinates -> circularKPartiteF[10, 10, 20]]

enter image description here


It appears to be very difficult to use GraphLayout -> RadialEmbedding in your case but you can place your Vertexes using VertexCoordinates.

mySources = Range[1, 10];

myBoth = Range[11, 20];

mySinks = Range[21, 40];

myFirstRules = 
  Table[Rule[RandomChoice[mySources], RandomChoice[myBoth]], {30}];

mySecondRules = 
  Table[Rule[RandomChoice[myBoth], RandomChoice[mySinks]], {60}];

myVertexCoordinates = 
  Join[Table[{Cos[\[Theta]], Sin[\[Theta]]}, {\[Theta], 0, 
     2 \[Pi] - (2 \[Pi] )/Length[mySources], (2 \[Pi] )/
   Table[2 {Cos[\[Theta]], Sin[\[Theta]]}, {\[Theta], 0, 
     2 \[Pi] - (2 \[Pi] )/Length[myBoth], (2 \[Pi] )/Length[myBoth]}],
    Table[3 {Cos[\[Theta]], Sin[\[Theta]]}, {\[Theta], 0, 
     2 \[Pi] - (2 \[Pi] )/Length[mySinks], (2 \[Pi] )/

Graph[mySources \[Union] myBoth \[Union] mySinks, myFirstRules \[Union] mySecondRules,
 VertexCoordinates -> myVertexCoordinates]

enter image description here


This only work when graph is well structured like David's example and know source, sink, and both:

g = Graph[Range@40, DeleteDuplicates[Join[myFirstRules, mySecondRules]]];
add1 = Thread["s" -> Select[mySources, VertexOutDegree[g, #] > 0 &]];
add2 = Thread[
   Select[myBoth, (VertexOutDegree[g, #] == 0 && 
        VertexInDegree[g, #] != 0) &] -> "out"];
add3 = Thread[
   "ss" -> Select[
     myBoth, (VertexInDegree[g, #] == 0 && 
        VertexOutDegree[g, #] != 0) &]];
If[add3 =!= {}, AppendTo[add3, "s" -> "ss"]];

vcoord = GraphEmbedding[
    Graph[Join[VertexList[g], {"s", "ss", "out"}], 
     Join[EdgeList[g], add1, add2, add3]], {"RadialEmbedding", 
     "RootVertex" -> "s"}][[;; VertexCount[g]]];

HighlightGraph[g, {mySources, myBoth, mySinks}, 
 VertexCoordinates -> vcoord]

enter image description here


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.