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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityAll 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.
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
Needs["Combinatorica`"]
before you can useFunctionalGraph
. $\endgroup$Graph[(# <-> f[#]) & /@ v]
, wheref
is your function andv
are the vertices. $\endgroup$