Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

What is the difference (in purpose) between Graph and GraphPlot? Which function is bested suited to which tasks?

Background: I just spent the last two hours trying to make this graph readable:

trans = Uncompress@
   "1:eJyVV01vwjAMRfu47U/sv3RKKc2KGsahxw2YOE0aXPff17RJSZD8/HIBga3Efn5+\
dl4/f/rT32q1ujyNH+35cj09+F8v40d1/j1+XY+\
Ht8P30T2Pf9ihbyvn7Y0T3GbjzZtyM5LbYmcP9Els++FdTWJy7EwrOEYzTjbeRkHyODnvB\
adgnb7d8KHi4e3OgBudmb5sB3xsV1CBOTLpymCdv1sp/\
mBlzhq9tJrfqolRC9aSDNSzGPwxFv5f05GAldDVoJKbrgBWJsnCIuFMB/\
28JVXNMR7IQsfVvZSTCD07V79Z78Clo1VLIS+YVn5/2H67Q3h4M83wZr2B4W/\
oCmhaFDXSWysUWcVpH4sElyUTFYsEGxnPRl7nlRgTFoFBu0xGjuDToRLAs5GeeDWqes22H\
ORPLDl/FENXLXCmuVWQanrkBwmT88uGjhh8JgIyYRI91EW4aBoqtOIbg60RxD+IAGjuZC+\
QRSeXfC2qkhGor6gqD60WfdoZTOy8SGu9iONKFEJeVO7eELQGMvOD0UDYaXHN5a6DT5Zlv\
GuYlu2TmhIyD726bP5gSnswmEttWR9puG1wH6WE1VuSBI2sVNA6cfxlK6JIx7utFDE7vI5\
lyAL+Kv09FopgpA9t+\
cIcCcjZMXgYV4YWrHd86DFyR2xpTh012aCEY8sSoy3sX3A0OHUHTjWYWgR8fv/wd/kB";
Graph[DeleteDuplicates@trans, VertexLabels -> Placed["Name", Center], 
 VertexShapeFunction -> "Capsule", VertexSize -> 1, 
 VertexLabelStyle -> Large, 
 EdgeShapeFunction -> 
  GraphElementData["ShortUnfilledArrow", "ArrowSize" -> 0.02]]

Unreadable

Then I discovered that GraphPlot produces much more readable results:

GraphPlot[
 DeleteDuplicates@trans /. (DirectedEdge[a_, b_] -> Rule[a, b]), 
 VertexLabeling -> True, DirectedEdges -> True]

Readable

Which prompted me to wonder what's the point of Graph.

Of course I noticed this, which while related, asks about the Combinatorica package.

share|improve this question
2  
Graph is not simply a way to plot graphs. It is a whole new data type on which you can perform lots of operations. It just happens to be displayed as its visualization. –  Szabolcs Jun 4 '12 at 18:38
add comment

1 Answer

up vote 17 down vote accepted

First of all Graph is more recent functionality. Now try this:

GraphPlot[{1 -> 2}] // Head

Graphics

and then this

Graph[{1 -> 2}] // Head

Graph

Basically while GraphPlot and GraphPlot3D both return pure graphic objects, Graph returns true graph which contains all information about the graph and can be computed with. For example:

g=Graph[{1->2}];
 AdjacencyMatrix[g] // Normal

{{0, 1}, {0, 0}}

Because they can be computed with, there is wide functionality built around Graph objects which GraphPlot does not have - from graph theory to visualization and styling.

Also to get more feel for the difference you can try AtomQ, which is the function that yields True if expression cannot be divided into subexpressions, and yields False otherwise, - as paraphrased from form Documentation Center.

AtomQ[g]

True

Compare with

 AtomQ[GraphPlot[{1 -> 2}]]

False

Which can be also hinted from:

 g // InputForm

Graph[{1, 2}, {DirectedEdge[1, 2]}]

and this:

 GraphPlot[{1 -> 2}] // InputForm

Graphics[Annotation[GraphicsComplex[{{1., 0.}, {0., 8.979318433952318*^-11}}, {{RGBColor[0.5, 0., 0.], Line[{{1, 2}}]}, {RGBColor[0, 0, 0.7], Tooltip[Point1, 1], Tooltip[Point2, 2]}}, {}], VertexCoordinateRules -> {{1., 0.}, {0., 8.979318433952318*^-11}}], FrameTicks -> None, PlotRange -> All, PlotRangePadding -> Scaled[0.1], AspectRatio -> Automatic]

Good thing to know is that built-in GraphData return Graph object on default:

GraphData /@ GraphData["Hypotraceable"]

enter image description here

And another cool consequence of the above is that you can even copy and paste the visual representations returned by Graph and compute them:

enter image description here

Besides GraphData there are many other functions that return Graph objects, such as CompleteGraph, GraphComplement, CayleyGraph, Subgraph and so on.

Another thing to know is that Graph objects have their own right-click menu alowing one to deal with the graph interactively (sample workflow is shown below):

GraphData["IcosahedralGraph", "LineGraph"]

enter image description here

share|improve this answer
4  
LayeredGraphPlot still often produces much better results than the equivalent Graph-based solution. Fortunately GraphPlot does work with Graph objects---you might want to mention this! –  Szabolcs Jun 4 '12 at 18:40
    
@Szabolcs Yes, thanks, I surely will add more info, - gathering it ;-) In meantime if anyone want's to see something relative added in this post - please comment. –  Vitaliy Kaurov Jun 4 '12 at 18:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.