maxitg / SetReplace

C++/Wolfram Language package for exploring set and graph rewriting systems
MIT License
217 stars 44 forks source link

Hypergraph head symbol #494

Open maxitg opened 3 years ago

maxitg commented 3 years ago

The problem

Have a Hypergraph symbol analogous to Graph, which will convert Hypergraph[{hyperedges... }] to a (hopefully) optimized standard form. This will make easier the addition of new functions for working with hypergraphs as we could overload WL symbol (e.g. VertexList, EdgeAdd, etc.). Also, the head would allows as to make use of MakeBoxes to represent the hypergraph using HypergraphPlot.

Possible solution

Proposed functionality:

vertexList = Sort @* Union @* Catenate;
hypergraphQ = MatchQ[Hold[#], Hold[Hypergraph[_List, {__Hyperedge}, ___]]] &;

Hypergraph[hypergraph : {__List}] :=
 Hypergraph[
  vertexList @ hypergraph,
  Apply[Hyperedge] /@ hypergraph]

Hypergraph /: Normal[hg_Hypergraph ? hypergraphQ] := List @@@ hg[[2]]

Hypergraph /: MakeBoxes[
  hg_Hypergraph ? hypergraphQ,
  fmt_] := ToBoxes @ WolframModelPlot[Normal @ hg]
In[]:= Hypergraph[{{x, x, y, z}, {z, w}}]

image

In[]:= % // InputForm
Out[]//InputForm= Hypergraph[{w, x, y, z}, {Hyperedge[x, x, y, z], Hyperedge[z, w]}]

Comment

Should the hyperedges be of the form Hyperedge[{x, x, y, z}] instead of Hyperedge[x, x, y, z]? The idea was to specify directedness as a second argument, i.e. Hyperedge[..., "Directed" | "Undirected"].

Additional context

The symbol should probably optimize when the hypergraph is solely composed of integer, i.e. by using PackedArray's.

maxitg commented 3 years ago

The box should be some kind of an interpretation box (ideally with a purple frame like Graph if possible). Otherwise, if you copy it and use it as an input, it does not work.

And yes, the format should be Hyperedge[{vertices...}] because, as you say, we need the second argument for other functionality.

daneelsan commented 3 years ago

Yes, that was really a quick implementation. As you said, the idea would be to TimeConstrain it and show the Graph-like box if it takes too much time.

maxitg commented 3 years ago

We can probably estimate how long it would take and constrain based on the number of vertices/hyperedges.

taliesinb commented 3 years ago

I'd like to suggest the following syntax for when it comes time to implement symmetries on hyperedges:

These all evaluate to Hyperedge[vertexlist, symmetrygroup]. The symmetry for colors is fully symmetric within each color. The symmetry for directed and undirected hyperedges is fully symmetric within the source, and the destination separately.