Preventing automatic layout of Graph [] objects in Mathematica 8

Some types of objects have special I / O formatting in Mathematica. This includes Graphics , bitmaps, and Mathematica 8 graphics, ( Graph[] ). Unfortunately, large graphs can take a very long time to render, much longer than most of the other operations that I perform on them during interactive work.

How to prevent the automatic assembly of Graph[] objects in StandardForm and TraditionalForm and show them as, for example. -Graph- , preferably preserving the interpretability of the output (possibly using Interpretation ?). I think this will be due to a change in Format and / or MakeBoxes in some way, but I could not achieve this.

I would like to do this in a reversible way and it is preferable to define a function that will return the original display of an interactive graph when applied to a Graph object (not the same as GraphPlot , which is not interactive).

Is there a way in the corresponding note to get format definitions / MakeBoxes associated with specific characters? FormatValues is one appropriate function, but for Graph it is empty.

Session Example:

 In[1]:= Graph[{1->2, 2->3, 3->1}] Out[1]= -Graph- In[2]:= interactiveGraphPlot[%] (* note that % works *) Out[2]= (the usual interactive graph plot should be shown here) 
+6
source share
3 answers

Although I don't have Mathematica 8 to try this, one possibility is to use this construct:

 Unprotect[Graph] MakeBoxes[g_Graph, StandardForm] /; TrueQ[$short] ^:= ToBoxes@Interpretation [Skeleton["Graph"], g] $short = True; 

Then the Graph object should appear in the Skeleton form, and setting $short = False should restore the default behavior.

Hope this works to automate the switch:

 interactiveGraphPlot[g_Graph] := Block[{$short}, Print[g]] 

Noting the problem of changing Graph made me consider using $PrePrint . I think this should also impede a slow layout. This might be more desirable if you are not using $PrePrint for something else.

 $PrePrint = If[TrueQ[$short], # /. _Graph -> Skeleton["Graph"], #] &; $short = True 

It’s also convenient, at least with Graphics (again, I can’t check with Graph in version 7), you can get a graphic image just Print . Here, with the graphics:

 g = Plot[Sin[x], {x, 0, 2 Pi}] (* Out = <<"Graphics">> *) 

Then

 Print[g] 

enter image description here

I left the $short test for convenient switching using a global symbol, but it could be left and used:

  $PrePrint = # /. _Graph -> Skeleton["Graph"] &; 

And then use $PrePrint = . to reset default functionality.

+2
source

You can use the GraphLayout option of Graph , as well as graph constructors to suppress rendering. A graph can still be rendered using GraphPlot . Try the following

 {gr1, gr2, gr3} = {RandomGraph[{100, 120}, GraphLayout -> None], PetersenGraph[10, 3, GraphLayout -> None], Graph[{1 -> 2, 2 -> 3, 3 -> 1}, GraphLayout -> None]} 

enter image description here

To make SetOptions easier, you can use SetOptions to set the GraphLayout option to None for all graph designers you are interested in.

+2
source

Did you try to just suppress the exit? I don't think the V8 Graph team makes any layout if you do. To explore this, we can create a large list of edges and compare the timings of graph[edges]; , graph[edges]; and GraphPlot[edges];

 In[23]:= SeedRandom[1]; edges = Union[Rule @@@ (Sort /@ RandomInteger[{1, 5000}, {50000, 2}])]; In[25]:= t = AbsoluteTime[]; graph[edges]; In[27]:= AbsoluteTime[] - t Out[27]= 0.029354 In[28]:= t = AbsoluteTime[]; Graph[edges]; In[30]:= AbsoluteTime[] - t Out[30]= 0.080434 In[31]:= t = AbsoluteTime[]; GraphPlot[edges]; In[33]:= AbsoluteTime[] - t Out[33]= 4.934918 

The inertial team Graph , of course, is the fastest. The Graph command takes much longer, but not where until the GraphPlot command. Thus, it seems to me that Graph does not actually calculate the layout, as GraphPlot does.

The logical question is that Graph wasting time. Let's look at the output of InputForm for Graph in the simple case:

 Graph[{1 -> 2, 2 -> 3, 3 -> 1, 1 -> 4}] // InputForm Out[123]//InputForm= Graph[{1, 2, 3, 4}, {DirectedEdge[1, 2], DirectedEdge[2, 3], DirectedEdge[3, 1], DirectedEdge[1, 4]}] 

Note that the vertices of the graph are defined, and I think this is what Graph does. In fact, the time taken to calculate Graph[edges] in the first example is comparable to the fastest way I can do for this:

 Union[Sequence @@@ edges]; // Timing 

It took 0.087045 seconds.

+1
source

Source: https://habr.com/ru/post/887628/


All Articles