# Creating a graph showing mapping from origins to ends in a specific format?

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]


Expectation:

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

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


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]


Replace 4 # & with 8 # & to get

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

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]
`

• Thank you for showing how to space the vertices. Dec 25, 2022 at 16:47