10
$\begingroup$

Apologies if I have not framed my question properly.

I am trying to create graph showing mapping from origins list to ends list. I am unable to get the required structure. I tried all kinds of GraphLayout options but none of them worked for my case. Can someone please point me to how to go about this?

Sample code:

ends = {"a", "b", "c", "d"};
origins = {1, 2, 3, 4, 5, 6, 7, 8, 9};
mps = UndirectedEdge @@@ Tuples[{origins, ends}];
Graph[mps, VertexLabels -> Automatic]

enter image description here

Expectation:

enter image description here

$\endgroup$

2 Answers 2

10
$\begingroup$
vlist = Join[ends[[{2, 1}]], Reverse @ origins, ends[[{4, 3}]]];

g = Graph[vlist,
 mps, 
 GraphStyle -> "NameLabeled", 
 GraphLayout -> {"MultipartiteEmbedding", "VertexPartition" -> {2, 9, 2}}]

enter image description here

Update: Modify the coordinates of first two and last two vertices:

vc = MapAt[4 # &, GraphEmbedding[g], Thread[{{1, 2, -1, -2}, 2}]];

Graph[g, VertexCoordinates -> vc]

enter image description here

Replace 4 # & with 8 # & to get

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks a lot. Exactly what I was looking for. Is there anyway to increase space between vertices? for example between a and b? $\endgroup$ Dec 25, 2022 at 16:23
  • $\begingroup$ @AnjanKumar, please see the update. $\endgroup$
    – kglr
    Dec 25, 2022 at 17:35
  • $\begingroup$ Thank you for the update. $\endgroup$ Dec 26, 2022 at 4:30
10
$\begingroup$

You can specify the VertexCoordinates

$Version

(* "13.2.0 for Mac OS X x86 (64-bit) (November 18, 2022)" *)

Clear["Global`*"]

ends = {"a", "b", "c", "d"};
origins = {1, 2, 3, 4, 5, 6, 7, 8, 9};
mps = UndirectedEdge @@@ Tuples[{origins, ends}];

g = Graph[mps,
  VertexLabels -> Placed[Automatic, Center],
  VertexCoordinates -> {{0, 4},
    {-2, 1}, {-2, -1}, {2, 1}, {2, -1},
    Sequence @@ Thread[{0, Range[3, -4, -1]}]},
  VertexStyle -> White,
  VertexSize -> .5]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you for showing how to space the vertices. $\endgroup$ Dec 25, 2022 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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