1
$\begingroup$

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?

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

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.

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

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]

enter image description here

|improve this answer|||||
$\endgroup$
0
$\begingroup$

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

|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.