Get complete graph from set of vertices?












3












$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$

















    3












    $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$















      3












      3








      3





      $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 10 hours ago









      KagaratschKagaratsch

      4,64831247




      4,64831247






















          2 Answers
          2






          active

          oldest

          votes


















          2












          $begingroup$

          Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]


          enter image description here



          Alternatively, you can use any of the following to get the same result:



          SimpleGraph[RelationGraph[True &, vertices], 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
            10 hours ago












          • $begingroup$
            @Kagaratsch, my pleasure. Thank you for the accept.
            $endgroup$
            – kglr
            10 hours ago



















          1












          $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$













            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

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2












            $begingroup$

            Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]


            enter image description here



            Alternatively, you can use any of the following to get the same result:



            SimpleGraph[RelationGraph[True &, vertices], 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
              10 hours ago












            • $begingroup$
              @Kagaratsch, my pleasure. Thank you for the accept.
              $endgroup$
              – kglr
              10 hours ago
















            2












            $begingroup$

            Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]


            enter image description here



            Alternatively, you can use any of the following to get the same result:



            SimpleGraph[RelationGraph[True &, vertices], 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
              10 hours ago












            • $begingroup$
              @Kagaratsch, my pleasure. Thank you for the accept.
              $endgroup$
              – kglr
              10 hours ago














            2












            2








            2





            $begingroup$

            Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]


            enter image description here



            Alternatively, you can use any of the following to get the same result:



            SimpleGraph[RelationGraph[True &, vertices], 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$



            Graph[UndirectedEdge @@@ Subsets[vertices, {2}], VertexLabels -> "Name"]


            enter image description here



            Alternatively, you can use any of the following to get the same result:



            SimpleGraph[RelationGraph[True &, vertices], 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 9 hours ago

























            answered 10 hours ago









            kglrkglr

            179k9199410




            179k9199410












            • $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
              10 hours ago












            • $begingroup$
              @Kagaratsch, my pleasure. Thank you for the accept.
              $endgroup$
              – kglr
              10 hours ago


















            • $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
              10 hours ago












            • $begingroup$
              @Kagaratsch, my pleasure. Thank you for the accept.
              $endgroup$
              – kglr
              10 hours ago
















            $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
            10 hours ago






            $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
            10 hours ago














            $begingroup$
            @Kagaratsch, my pleasure. Thank you for the accept.
            $endgroup$
            – kglr
            10 hours ago




            $begingroup$
            @Kagaratsch, my pleasure. Thank you for the accept.
            $endgroup$
            – kglr
            10 hours ago











            1












            $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$

              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












                1








                1





                $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 8 hours ago









                SzabolcsSzabolcs

                159k13435930




                159k13435930






























                    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

                    ف. موراي أبراهام

                    صرب

                    كأس إنترتوتو