Get complete graph from set of vertices?
$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]
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
$endgroup$
add a comment |
$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]
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
$endgroup$
add a comment |
$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]
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
$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]
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
function-construction graphs-and-networks
asked Jan 19 at 14:06
KagaratschKagaratsch
4,67031348
4,67031348
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
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[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
$endgroup$
$begingroup$
The firstCompleteGraph
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
add a comment |
$begingroup$
Using AdjacencyGraph:
AdjacencyGraph[vertices,
AdjacencyMatrix[CompleteGraph[Length[vertices]]]]
$endgroup$
add a comment |
$begingroup$
Another way is with AdjacencyGraph
.
SimpleGraph[
AdjacencyGraph[vertices, ConstantArray[1, Length[vertices] {1, 1}]],
VertexLabels -> Automatic
]
With IGraph/M, you can zero out the matrix diagonal directly:
AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]
$endgroup$
add a comment |
$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"]
$endgroup$
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$begingroup$
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[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
$endgroup$
$begingroup$
The firstCompleteGraph
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
add a comment |
$begingroup$
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[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
$endgroup$
$begingroup$
The firstCompleteGraph
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
add a comment |
$begingroup$
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[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
$endgroup$
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[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
edited Jan 22 at 15:15
answered Jan 19 at 14:13
kglrkglr
184k10202419
184k10202419
$begingroup$
The firstCompleteGraph
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
add a comment |
$begingroup$
The firstCompleteGraph
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
add a comment |
$begingroup$
Using AdjacencyGraph:
AdjacencyGraph[vertices,
AdjacencyMatrix[CompleteGraph[Length[vertices]]]]
$endgroup$
add a comment |
$begingroup$
Using AdjacencyGraph:
AdjacencyGraph[vertices,
AdjacencyMatrix[CompleteGraph[Length[vertices]]]]
$endgroup$
add a comment |
$begingroup$
Using AdjacencyGraph:
AdjacencyGraph[vertices,
AdjacencyMatrix[CompleteGraph[Length[vertices]]]]
$endgroup$
Using AdjacencyGraph:
AdjacencyGraph[vertices,
AdjacencyMatrix[CompleteGraph[Length[vertices]]]]
answered Jan 22 at 14:49
halmirhalmir
10.2k2443
10.2k2443
add a comment |
add a comment |
$begingroup$
Another way is with AdjacencyGraph
.
SimpleGraph[
AdjacencyGraph[vertices, ConstantArray[1, Length[vertices] {1, 1}]],
VertexLabels -> Automatic
]
With IGraph/M, you can zero out the matrix diagonal directly:
AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]
$endgroup$
add a comment |
$begingroup$
Another way is with AdjacencyGraph
.
SimpleGraph[
AdjacencyGraph[vertices, ConstantArray[1, Length[vertices] {1, 1}]],
VertexLabels -> Automatic
]
With IGraph/M, you can zero out the matrix diagonal directly:
AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]
$endgroup$
add a comment |
$begingroup$
Another way is with AdjacencyGraph
.
SimpleGraph[
AdjacencyGraph[vertices, ConstantArray[1, Length[vertices] {1, 1}]],
VertexLabels -> Automatic
]
With IGraph/M, you can zero out the matrix diagonal directly:
AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]
$endgroup$
Another way is with AdjacencyGraph
.
SimpleGraph[
AdjacencyGraph[vertices, ConstantArray[1, Length[vertices] {1, 1}]],
VertexLabels -> Automatic
]
With IGraph/M, you can zero out the matrix diagonal directly:
AdjacencyGraph[vertices,
IGZeroDiagonal@ConstantArray[1, Length[vertices] {1, 1}],
VertexLabels -> Automatic]
answered Jan 19 at 15:49
SzabolcsSzabolcs
160k14436933
160k14436933
add a comment |
add a comment |
$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"]
$endgroup$
add a comment |
$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"]
$endgroup$
add a comment |
$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"]
$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"]
answered Jan 22 at 15:34
Jason B.Jason B.
48.3k388191
48.3k388191
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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