I have a graph in the form {Dog -> Apple, Apple -> Screwdriver}, I used to plot a graph in the following way:

GraphPlot[{Dog -> Apple, Apple -> Screwdriver}]

I'd like to plot the complement of the graph, the following chain of commands does the job,

  UndirectedGraph[Graph[{Dog -> Apple, Apple -> Screwdriver}]]]]

but it has the disadvantage that the names of the vertices are changed into numbers. I'd like to keep the names. How can I do this?

g1 = Graph[{Dog -> Apple, Apple -> Screwdriver}, VertexLabels -> "Name", 
     GraphLayout -> "CircularEmbedding", ImagePadding -> 40]

Mathematica graphics

g2 = GraphComplement[UndirectedGraph[g1], VertexLabels -> "Name",   ImagePadding -> 40,
     VertexCoordinates -> GraphEmbedding[g1]]

Mathematica graphics

Using GraphPlot

GraphPlot[AdjacencyMatrix[g2], VertexLabeling -> True] /. 
 Framed[i_, rest__] :> Framed[VertexList[g2][[i]], rest]

Mathematica graphics


m = AdjacencyMatrix[UndirectedGraph@g1];
am = 1 - IdentityMatrix[Length[m]] - m;

GraphPlot[am, VertexLabeling -> True, DirectedEdges -> False, AspectRatio -> 1, 
 VertexRenderingFunction -> ({FaceForm[Orange], 
     Rectangle[# - {.2, .01}, {.2, .01} + #], Text[VertexList[g1][[#2]], #1]} &)]

Mathematica graphics

  • $\begingroup$ This looks really nice, thank you! Can it be done by using GraphPlot? I'd prefer that function since it has many methods for drawing the graph, and also it labels the vertices more discretely (using only a tooltip) so I can only check a label if I'm interested in said vertex. Also I'm sorry that I dod not specify this in the original question. $\endgroup$ – Daniel Soltész May 28 '16 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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