3
$\begingroup$

I want to draw a graph with some nodes and some half edges attached to them. Basically something like number a) in the following picture

enter image description here

Of course I can just draw each node and each half-edge one by one, but is there a way to do it a bit more efficiently if I have many nodes?

$\endgroup$
  • $\begingroup$ By adding a couple of invisible nodes to a)? $\endgroup$ – Henrik Schumacher Jan 22 at 18:24
4
$\begingroup$

1.

Add invisible vertices (as suggested by Henrik in comments):

numchildren = {3, 2, 2, 1};

ig = MapIndexed[IndexGraph[#, 10 #2[[1]]] &, 
   CompleteKaryTree[2, #, GraphLayout -> "LayeredEmbedding"] & /@ numchildren];

vc = Join @@ MapIndexed[
    Thread[VertexList[#] -> (TranslationTransform[{2 #2[[1]], 0}]@
         Transpose[Rescale /@ Transpose[GraphEmbedding@#]])] &, ig];

SetProperty[GraphUnion @@ ig, 
  {VertexShapeFunction -> {_?(Mod[#, 10] != 0 &) -> None}, 
  VertexSize -> .75, VertexStyle -> Purple, VertexCoordinates -> vc, 
  EdgeStyle -> Directive[Thickness[.01], Green], ImageSize -> Large}]

enter image description here

2.

Alternatively, create a graph with no edges and use a custom VertexShapeFunction to add halfedges:

g0 = Graph[MapThread[Property[#, "children" -> #2] &, {Range[4], numchildren}], {}, 
   VertexCoordinates -> Thread[{Range[4], 0}], VertexStyle -> Purple ];

vSF = {Green, Thickness[.01], 
   Function[x, Line[{#, # + x/3}]] /@ (Through[{Cos, Sin}@#] & /@ 
      MovingAverage[Subdivide[-Pi, 0, PropertyValue[{g0, #2}, "children"]], 2]), 
   PropertyValue[{g0, #2}, VertexStyle], 
   Disk[#, .1]} &; 

 SetProperty[g0, {VertexShapeFunction -> vSF, ImageSize -> Large}]

enter image description here

3.

If you do not need a Graph object you can construct a graphics object more directly:

n = 4;
numchildren = {3, 2, 2, 1};
vc = Thread[{Range[n], 0}];
lc = Through[{Cos, Sin}@#] & /@ MovingAverage[Subdivide[-Pi, 0, #], 2] & /@ numchildren;

Graphics[{Green, Thickness[.01], 
  MapThread[Function[x, Line[{#, # + x/3}]] /@ #2 &, {vc, lc}], 
  Purple, Disk[#, .1] & /@ vc}, ImageSize -> Large] 

enter image description here

| improve this answer | |
$\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.