# Get complete graph from set of vertices?

There is a function in Mathematica called CompleteGraph which takes a number and makes a complete graph with that number of vertices:

CompleteGraph However, in the above the vertices become labelled {1,2,3,4,5}. In contrast, given a set of vertices like e.g.,

vertices={1,3,5,6,8};


I would like to get a complete graph in which the vertices are labelled by the above labels. Is it possible to do that quickly (computationally efficiently) in Mathematica?

RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"] Alternatively, you can use any of the following to get the same result:

Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]
AdjacencyGraph[vertices, ConstantArray[1, {5,5}]-IdentityMatrix, VertexLabels -> "Name"]
SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph,
VertexLabels -> "Name"]


To change just the labels you can use:

CompleteGraph[5, VertexLabels -> {k_ :> vertices[[k]]}]


same picture

• The first CompleteGraph approach seems to only change the labels but not the vertex names. The other two versions work great, thank you! (True, I guess my question was asking about labels, sorry for the confusion.) – Kagaratsch Jan 19 '19 at 14:23
• @Kagaratsch, my pleasure. Thank you for the accept. – kglr Jan 19 '19 at 14:26

AdjacencyGraph[vertices,


Another way is with AdjacencyGraph.

SimpleGraph[
VertexLabels -> Automatic
] With IGraph/M, you can zero out the matrix diagonal directly:

AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]


To me it seems the most direct method is to use VertexReplace, and it doesn't seem any slower than the other methods.

completeGraph[vertexList_List,opts___] := With[
{g = CompleteGraph[ Length @ vertexList, opts]},

completeGraph[{a, b, c, d, e, f, g, h}, VertexLabels -> "Name"] 