Reputation: 16865
This question concerns graphs objects in Mathematica 8 when nodes are sets and not numbers or canonical symbols.
What I try to do is simply the following. I have a set of sets. So consider the following:
S = {{1}, {2}, {1,2}, {2,3}} (* Set of vertices *)
E = {{{1}, {2}}, {{2}, {1,2}}, {{{1,2}, {2,3}}, {{2,3}, {1}}} (* Edges *)
Unfortunately when I do the following:
Graph[S, E]
Mathematica does not create the graph object and cannot understand my intention to create a graph having as nodes those sets.
What can i do to achieve this result? I thought about some substitution just to let understand Mathematica how to create connections, using inverse I can label vertices after creating the graph. But don`t know how to get this.
I tried using replacement rules and operators, but the main difficulty is operating a substitution for an arbitrary length array. I`ll explain myself.
Consider to have a generic list of lists like S
. I would have no problem in substituting it with a string like {1,2,3} -> "123"
or {2,3} -> "23"
.
The problem is that the replacing operator /.
does not provide syntax for specifying a list in the way I want.
Thankyou
Upvotes: 1
Views: 206
Reputation: 24336
Let me address this:
Consider to have a generic list of lists like S. I would have no problem in substituting it with a string like {1,2,3} -> "123" or {2,3} -> "23".
The problem is that the replacing operator /. does not provide syntax for specifying a list in the way I want.
Starting with:
lists = {{{2}, {1, 2}}, {{1, 2}, {2, 3}}, {{1, 2, 3}, {1}}};
Here are several options:
lists /. x : {__Integer} :> StringJoin@Riffle[ToString /@ x, "-"]
{{"2", "1-2"}, {"1-2", "2-3"}, {"1-2-3", "1"}}
lists /. x : {__Integer} :> ToString@x
{{"{2}", "{1, 2}"}, {"{1, 2}", "{2, 3}"}, {"{1, 2, 3}", "{1}"}}
Map[StringJoin[ToString /@ #] &, lists, {-2}]
{{"2", "12"}, {"12", "23"}, {"123", "1"}}
Upvotes: 1
Reputation: 3066
This is arguably a more readable version of @halirutan's approach:
a = {1}; b = {2}; c = {1, 2}; d = {2, 3}
Graph[{a -> b, b -> c, c -> d, d -> a}, VertexLabels -> "Name",
ImagePadding -> 10]
Because it is a connected graph, you don't need to include the vertex list in Graph
; the edge list suffices.
Upvotes: 2
Reputation: 4341
You never want to use capital letters for your variable names, especially E
is evil, since it is the euler number. Additionally, I think you have type in your braces for E
.
When I understand this correct, then the only thing you have to do is, to make DirectedEdge
s from your E
list:
s = {{1}, {2}, {1, 2}, {2, 3}} (*Set of vertices*)
e = {{{1}, {2}}, {{2}, {1, 2}}, {{1, 2}, {2, 3}}, {{2, 3}, {1}}} (*Edges*)
Graph[s, DirectedEdge @@@ e, VertexLabels -> "Name", ImagePadding -> 10]
Upvotes: 3