3
$\begingroup$

I'd like to custom name the vertices of a (complete) graph and have those names reflected in the edges.

shiftGraph produces a complete graph with vertices labels from imin to imax.

             CompleteGraph[imax - imin + 1, DirectedEdges -> True, 
                           VertexLabels -> Thread[Rule[Range[1, imax - imin + 1],Range[imin, imax]]]];

shiftGraph[2,4] yields:

Complete Graph on 2 through 4

But those vertex labels are not inherited by the edges:

EdgeList[shiftGraph[2, 4]]

yields

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

Is that expected or desirable? I suppose the fix is to apply the VertexLabels rules to the edges. Cleaner methods?

$\endgroup$
1
  • 1
    $\begingroup$ VertexLabels only affects the display of the vertices - the underlying vertex names are still 1 to n (compare also with Graph). Unfortunately, you can't specify the vertex names for CompleteGraph upon construction, so you'll have to rename them after the fact using VertexReplace $\endgroup$
    – Lukas Lang
    Mar 13 at 18:52

2 Answers 2

2
$\begingroup$

You can use IndexGraph to get the desired result:

ClearAll[shiftGraph]
shiftGraph[imin_, imax_, o : OptionsPattern[Graph]] := 
 Graph[IndexGraph[CompleteGraph[imax - imin + 1, DirectedEdges -> True], imin], o]


shiftGraph[2, 4, VertexLabels -> "Name", EdgeLabels -> "Name"]

enter image description here

EdgeList[shiftGraph[2, 4]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Perfect! Thank you. The language is so rich, it is impossible to be familiar with all its many features. $\endgroup$
    – JV3
    Mar 15 at 13:05
0
$\begingroup$

IGCompleteGraph from IGraph/M makes it easy to create a complete graph with the vertex names of your choice.

EdgeList@IGCompleteGraph[{"a", "b", "c", "d"}]
(* {"a" \[UndirectedEdge] "b", "a" \[UndirectedEdge] "c", 
 "a" \[UndirectedEdge] "d", "b" \[UndirectedEdge] "c", 
 "b" \[UndirectedEdge] "d", "c" \[UndirectedEdge] "d"} *)
$\endgroup$

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.