Get complete graph from set of vertices?












4












$begingroup$


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



CompleteGraph[5]



enter image description here




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?










share|improve this question









$endgroup$

















    4












    $begingroup$


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



    CompleteGraph[5]



    enter image description here




    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?










    share|improve this question









    $endgroup$















      4












      4








      4





      $begingroup$


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



      CompleteGraph[5]



      enter image description here




      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?










      share|improve this question









      $endgroup$




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



      CompleteGraph[5]



      enter image description here




      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?







      function-construction graphs-and-networks






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 14:06









      KagaratschKagaratsch

      4,67031348




      4,67031348






















          4 Answers
          4






          active

          oldest

          votes


















          3












          $begingroup$

          RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"]    


          enter image description here



          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[5], VertexLabels -> "Name"]
          SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph[5],
          VertexLabels -> "Name"]


          To change just the labels you can use:



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



          same picture







          share|improve this answer











          $endgroup$













          • $begingroup$
            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.)
            $endgroup$
            – Kagaratsch
            Jan 19 at 14:23












          • $begingroup$
            @Kagaratsch, my pleasure. Thank you for the accept.
            $endgroup$
            – kglr
            Jan 19 at 14:26



















          3












          $begingroup$

          Using AdjacencyGraph:



          AdjacencyGraph[vertices, 
          AdjacencyMatrix[CompleteGraph[Length[vertices]]]]





          share|improve this answer









          $endgroup$





















            2












            $begingroup$

            Another way is with AdjacencyGraph.



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


            enter image description here



            With IGraph/M, you can zero out the matrix diagonal directly:



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





            share|improve this answer









            $endgroup$





















              1












              $begingroup$

              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]},
              VertexReplace[g, Thread[VertexList[g] -> vertexList]]
              ]


              So you can do



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


              enter image description here






              share|improve this answer









              $endgroup$













                Your Answer





                StackExchange.ifUsing("editor", function () {
                return StackExchange.using("mathjaxEditing", function () {
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                });
                });
                }, "mathjax-editing");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "387"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189826%2fget-complete-graph-from-set-of-vertices%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3












                $begingroup$

                RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"]    


                enter image description here



                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[5], VertexLabels -> "Name"]
                SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph[5],
                VertexLabels -> "Name"]


                To change just the labels you can use:



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



                same picture







                share|improve this answer











                $endgroup$













                • $begingroup$
                  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.)
                  $endgroup$
                  – Kagaratsch
                  Jan 19 at 14:23












                • $begingroup$
                  @Kagaratsch, my pleasure. Thank you for the accept.
                  $endgroup$
                  – kglr
                  Jan 19 at 14:26
















                3












                $begingroup$

                RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"]    


                enter image description here



                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[5], VertexLabels -> "Name"]
                SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph[5],
                VertexLabels -> "Name"]


                To change just the labels you can use:



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



                same picture







                share|improve this answer











                $endgroup$













                • $begingroup$
                  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.)
                  $endgroup$
                  – Kagaratsch
                  Jan 19 at 14:23












                • $begingroup$
                  @Kagaratsch, my pleasure. Thank you for the accept.
                  $endgroup$
                  – kglr
                  Jan 19 at 14:26














                3












                3








                3





                $begingroup$

                RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"]    


                enter image description here



                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[5], VertexLabels -> "Name"]
                SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph[5],
                VertexLabels -> "Name"]


                To change just the labels you can use:



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



                same picture







                share|improve this answer











                $endgroup$



                RelationGraph[UnsameQ, vertices, VertexLabels -> "Name"]    


                enter image description here



                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[5], VertexLabels -> "Name"]
                SetProperty[VertexReplace[#, Thread[VertexList@# -> vertices]] &@ CompleteGraph[5],
                VertexLabels -> "Name"]


                To change just the labels you can use:



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



                same picture








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 22 at 15:15

























                answered Jan 19 at 14:13









                kglrkglr

                184k10202419




                184k10202419












                • $begingroup$
                  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.)
                  $endgroup$
                  – Kagaratsch
                  Jan 19 at 14:23












                • $begingroup$
                  @Kagaratsch, my pleasure. Thank you for the accept.
                  $endgroup$
                  – kglr
                  Jan 19 at 14:26


















                • $begingroup$
                  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.)
                  $endgroup$
                  – Kagaratsch
                  Jan 19 at 14:23












                • $begingroup$
                  @Kagaratsch, my pleasure. Thank you for the accept.
                  $endgroup$
                  – kglr
                  Jan 19 at 14:26
















                $begingroup$
                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.)
                $endgroup$
                – Kagaratsch
                Jan 19 at 14:23






                $begingroup$
                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.)
                $endgroup$
                – Kagaratsch
                Jan 19 at 14:23














                $begingroup$
                @Kagaratsch, my pleasure. Thank you for the accept.
                $endgroup$
                – kglr
                Jan 19 at 14:26




                $begingroup$
                @Kagaratsch, my pleasure. Thank you for the accept.
                $endgroup$
                – kglr
                Jan 19 at 14:26











                3












                $begingroup$

                Using AdjacencyGraph:



                AdjacencyGraph[vertices, 
                AdjacencyMatrix[CompleteGraph[Length[vertices]]]]





                share|improve this answer









                $endgroup$


















                  3












                  $begingroup$

                  Using AdjacencyGraph:



                  AdjacencyGraph[vertices, 
                  AdjacencyMatrix[CompleteGraph[Length[vertices]]]]





                  share|improve this answer









                  $endgroup$
















                    3












                    3








                    3





                    $begingroup$

                    Using AdjacencyGraph:



                    AdjacencyGraph[vertices, 
                    AdjacencyMatrix[CompleteGraph[Length[vertices]]]]





                    share|improve this answer









                    $endgroup$



                    Using AdjacencyGraph:



                    AdjacencyGraph[vertices, 
                    AdjacencyMatrix[CompleteGraph[Length[vertices]]]]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 22 at 14:49









                    halmirhalmir

                    10.2k2443




                    10.2k2443























                        2












                        $begingroup$

                        Another way is with AdjacencyGraph.



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


                        enter image description here



                        With IGraph/M, you can zero out the matrix diagonal directly:



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





                        share|improve this answer









                        $endgroup$


















                          2












                          $begingroup$

                          Another way is with AdjacencyGraph.



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


                          enter image description here



                          With IGraph/M, you can zero out the matrix diagonal directly:



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





                          share|improve this answer









                          $endgroup$
















                            2












                            2








                            2





                            $begingroup$

                            Another way is with AdjacencyGraph.



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


                            enter image description here



                            With IGraph/M, you can zero out the matrix diagonal directly:



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





                            share|improve this answer









                            $endgroup$



                            Another way is with AdjacencyGraph.



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


                            enter image description here



                            With IGraph/M, you can zero out the matrix diagonal directly:



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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 19 at 15:49









                            SzabolcsSzabolcs

                            160k14436933




                            160k14436933























                                1












                                $begingroup$

                                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]},
                                VertexReplace[g, Thread[VertexList[g] -> vertexList]]
                                ]


                                So you can do



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


                                enter image description here






                                share|improve this answer









                                $endgroup$


















                                  1












                                  $begingroup$

                                  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]},
                                  VertexReplace[g, Thread[VertexList[g] -> vertexList]]
                                  ]


                                  So you can do



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


                                  enter image description here






                                  share|improve this answer









                                  $endgroup$
















                                    1












                                    1








                                    1





                                    $begingroup$

                                    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]},
                                    VertexReplace[g, Thread[VertexList[g] -> vertexList]]
                                    ]


                                    So you can do



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


                                    enter image description here






                                    share|improve this answer









                                    $endgroup$



                                    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]},
                                    VertexReplace[g, Thread[VertexList[g] -> vertexList]]
                                    ]


                                    So you can do



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


                                    enter image description here







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 22 at 15:34









                                    Jason B.Jason B.

                                    48.3k388191




                                    48.3k388191






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Mathematica Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        Use MathJax to format equations. MathJax reference.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189826%2fget-complete-graph-from-set-of-vertices%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Mario Kart Wii

                                        The Binding of Isaac: Rebirth/Afterbirth

                                        What does “Dominus providebit” mean?