How can I construct and visualize a hypergraph?

A hypergraph is a generalization of a graph, in which an edge can connect more than two vertices. Thus you can think of an edge in an hypergraph as a subset of nodes.

Since version 8, Mathematica has supported the plotting of graphs, as well as graph algorithms.

Is there a way to plot hypergraphs on Mathematica? I know there are ways in which an hypergraph can be represented as a graph, or a matrix. But what I want is a method that allows me to plot the hypergraph directly, where edges that connect multiple vertices are drawn as branching lines between those vertices (for example, think of a reaction network). See, for example, the first image in this question.

Something similar to one in the Op's link:

hyperPlot[sets_, blayout_: "SpringEmbedding"] :=

Block[{l, esym, eset, vset, g, em, rules, edges, vcircle},
l = Length[sets];
esym = Table[Unique["e"], {l}];
eset = Flatten[Table[Thread[sets[[i]] <-> esym[[i]]], {i, l}]];
vset = Union[Join @@ sets];
g = Graph[vset, eset, GraphLayout -> blayout];
em = GraphEmbedding[g];
rules = Dispatch[Thread[VertexList[g] -> em]];
edges =
Table[{ColorData["DarkRainbow"][i/l],
Arrow[BSplineCurve[{sets[[i, 1]], esym[[i]], #} /. rules,
SplineWeights -> {1, 6, 1}], {.2, .2}] & /@
Rest[sets[[i]]]}, {i, l}];
vcircle =
With[{c = # /. rules}, {Text[Style[#, Bold, 13], c],
Circle[c, .2]}] & /@ vset;
Graphics[{{Thickness[.01], Arrowheads, edges}, Thickness[.005],
vcircle}]
]

basically, I added dummy vertex to each sets to define edges and construct graph. Using embedding information, I will draw graph and edges. It's not perfect, but another possibility. Also you could draw directed hyper graph..

vs = {{2, 3, 4, 5}, {5, 9, 12, 6, 7}, {8, 2, 11, 13}, {1, 8, 10}};
hyperPlot[vs] vs = RandomSample[Subsets[Range, {3}], 10];
hyperPlot[vs, "SpringElectricalEmbedding"] vs = {{1, 2, 3, 4}, {1, 3, 5}, {3, 4, 7}, {5, 6, 7}};
hyperPlot[vs] Here is a different take on @Szabolcs idea. If a general grouping will do CommunityGraphPlot can be used. Define a 13-vertex graph with no simple (as opposed to hyper) edges:

g = Graph[Range, {},
VertexCoordinates -> RandomReal[1, {13, 2}], VertexLabels -> "Name"] Explicetely define hyper-edges as groups of vertices they connect:

CommunityGraphPlot[g,
{{2, 3, 4, 5}, {5, 9, 12, 6, 7}, {8, 2, 11, 13}, {1, 8, 10}},
CommunityRegionStyle -> RandomColor[RGBColor[_, _, _, .5], 4]] Use Method option for different layouts:

CommunityGraphPlot[g,
{{2, 3, 4, 5}, {5, 9, 12, 6, 7}, {8, 2, 11, 13}, {1, 8, 10}},
CommunityRegionStyle -> RandomColor[RGBColor[_, _, _, .5], 4], Method -> "Hierarchical"] • This is much better than mine. I only did it for 3-hypergraphs, this is probably more general. Is there a guarantee that a region will never contain a node that should not be part of it? This is actually a non-trivial layout problem that I didn't know how to solve for the general case. If CommunityGraphPlot guarantees this, then I want to delete my answer. – Szabolcs Sep 2 '14 at 22:11
• @Szabolcs I do not think there is a guarantee. I can ask for certainty, but just by poking around I've seen this violated. – Vitaliy Kaurov Sep 2 '14 at 22:35
• @Szabolcs ...but coloring of vertices helps a bit. – Vitaliy Kaurov Sep 2 '14 at 22:41
• If the $n$ vertices are arranged on a regular $n$-gon (or they're just put on a circle), then it is automatically guaranteed that a polygon made of a subset of the vertices won't contain anything but that subset. This at least shows that this is possible. Putting the vertices on a circle may not be a very efficient arrangement though. I think CommunityGraphPlot doesn't support CircularEmbedding, right? – Szabolcs Sep 3 '14 at 19:06
• @Szabolcs I did not find an easy way to do this. – Vitaliy Kaurov Sep 4 '14 at 13:13

This is just a rough proof-of-concept that needs additional work for real applications.

For 3-hypergraphs, you could simply surround each triple with a rounded polygon. I didn't implement rounded polygons now (which would look better), I just used B-splines, but these are a but ugly.

hg = RandomSample[Subsets[Range, {3}], 10]
(* {{2, 5, 6}, {3, 5, 9}, {2, 4, 10}, {2, 6, 9}, {1, 4, 7}, {4,
6, 9}, {1, 6, 10}, {4, 8, 9}, {2, 3, 7}, {3, 8, 10}} *)

cg[n_] := Graph[n, UndirectedEdge @@@ Subsets[n, {2}]]

g = GraphUnion @@ (cg /@ hg);

pos = GraphEmbedding[g];

dt = Dispatch@Thread[VertexList[g] -> pos];

expand[pts_] :=
With[{c = Mean[pts]}, # + c & /@ (1.8 (# - c & /@ pts))]

Graphics[{Disk[#, 0.03] & /@ pos,
FaceForm@Directive[Darker@Blue, Opacity[0.1]], EdgeForm[Black],
FilledCurve@BSplineCurve[#, SplineClosed -> True] & /@
expand /@ (hg /. dt)}] • I don't know anything about hypregs. Just a question: Are they always undirected? – Dr. belisarius Sep 2 '14 at 21:17
• Nice visual @belisarius! What about n-edges? Found this on the demonstrations site: demonstrations.wolfram.com/ThreeUniformHypergraphs – user5601 Sep 2 '14 at 21:38
• @belisarius, oriented hyerpgraphs have been studied recently, see the work by Reff and Rusnak, esp the latter's thesis in 2010. – alancalvitti Sep 2 '14 at 21:41