Andry
Andry

Reputation: 16865

Creating a Graph object whose nodes are sets

This question concerns graphs objects in Mathematica 8 when nodes are sets and not numbers or canonical symbols.

The problem

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.

Question

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.

Trials

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

Answers (3)

Mr.Wizard
Mr.Wizard

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

DavidC
DavidC

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

halirutan
halirutan

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

enter image description here

Upvotes: 3

Related Questions