# Generate a graph from function on the vertices

I have a concrete function $$f:V\rightarrow P(V)$$ that describes the neighborhood of a vertex $$g$$. I tried to use FunctionalGraph[f, V] , but apparently, this function was removed from Mathematica.

So, how to generate a graph from a function?

• You have to load the Combinatorica package with Needs["Combinatorica"] before you can use FunctionalGraph. – Henrik Schumacher Jun 20 '19 at 14:04
• Graph[(# <-> f[#]) & /@ v], where f is your function and v are the vertices. – ktm Jun 20 '19 at 14:05

All you need is something like the code

ClearAll[f, V, a, b, c, d]; V = {a, b, c, d};
f[a] = {b}; f[b] = {c}; f[c] = {a, d}; f[d] = {c};
Graph[Flatten[Function[x, x -> # & /@ f[x]] /@ V]]


which returns a directed graph. A variant is to replace the last line with

Graph[Flatten[(Thread[# -> f[#]]) & /@ V]]


and there are probably a few other ways to do it.

• My graph is directed, and the function returns a set of neighbors of a certain vertex. – user2679290 Jun 20 '19 at 19:28
• @user2679290 Oops! I did not notice that detail. I adjusted my code to work in that case. Thanks for telling. – Somos Jun 20 '19 at 19:55
• Yeh, Thread is essentially what I do in a seperate function – user2679290 Jun 20 '19 at 21:36

You can also use RelationGraph with MemberQ[f@#, #2]& as the first argument:

Using Somos's example:

vl = {a, b, c, d};
f[a] = {b}; f[b] = {c}; f[c] = {a, d}; f[d] = {c};
RelationGraph[MemberQ[f @ #, #2]&, vl] Well, the code is

JJ[a_]:=Map[(a->#)&,f[a]]
j=Graph[Flatten[JJ  /@V]]
`

As the code suggested by @user6014 ignores the nested sets