How to remove vertices from a graph?
I have 2 lists: list 1 is a list of vertices of a graph, says, {1,2,3,4,5} and list 2 keeps track of edges (i.e which vertex connects to which) such as {{1,2}, {2,3},{3,4},{4,1},{2,5}}. Now I want to remove some vertices in list 1 and I want that any edges in list 2 that have an end same as one of the removed vertices will also be removed. What would be the best way to do this in Mathematica? (I can do this easily in other languages like vb.net, but I still am trying to get my head on Mathematica)
list-manipulation graphs-and-networks
add a comment |
I have 2 lists: list 1 is a list of vertices of a graph, says, {1,2,3,4,5} and list 2 keeps track of edges (i.e which vertex connects to which) such as {{1,2}, {2,3},{3,4},{4,1},{2,5}}. Now I want to remove some vertices in list 1 and I want that any edges in list 2 that have an end same as one of the removed vertices will also be removed. What would be the best way to do this in Mathematica? (I can do this easily in other languages like vb.net, but I still am trying to get my head on Mathematica)
list-manipulation graphs-and-networks
add a comment |
I have 2 lists: list 1 is a list of vertices of a graph, says, {1,2,3,4,5} and list 2 keeps track of edges (i.e which vertex connects to which) such as {{1,2}, {2,3},{3,4},{4,1},{2,5}}. Now I want to remove some vertices in list 1 and I want that any edges in list 2 that have an end same as one of the removed vertices will also be removed. What would be the best way to do this in Mathematica? (I can do this easily in other languages like vb.net, but I still am trying to get my head on Mathematica)
list-manipulation graphs-and-networks
I have 2 lists: list 1 is a list of vertices of a graph, says, {1,2,3,4,5} and list 2 keeps track of edges (i.e which vertex connects to which) such as {{1,2}, {2,3},{3,4},{4,1},{2,5}}. Now I want to remove some vertices in list 1 and I want that any edges in list 2 that have an end same as one of the removed vertices will also be removed. What would be the best way to do this in Mathematica? (I can do this easily in other languages like vb.net, but I still am trying to get my head on Mathematica)
list-manipulation graphs-and-networks
list-manipulation graphs-and-networks
edited 4 hours ago
Henrik Schumacher
48.8k467139
48.8k467139
asked 6 hours ago
N.T.C
29017
29017
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
These are done easily with graph functions:
g = Graph[Range[5], {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 1, 2 <-> 5}];
g2 = VertexDelete[g, 1];
EdgeList[g2]
(*
{2 <-> 3, 3 <-> 4, 2 <-> 5}
*)
Of course this works as well if you want to delete more than one vertex, e.g., vertices 1 and 5:
g2 = VertexDelete[g, {1, 5}];
add a comment |
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
A few more alternatives:
Select[edges, FreeQ[1]]
Pick[edges, FreeQ[1] /@ edges]
DeleteCases[edges, {_, 1} | {1, _}]
List @@@ EdgeList[VertexDelete[edges, 1]]
all give
{{2, 3}, {3, 4}, {2, 5}}
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
@geordie, if you want to remove a list of vertices, say{1,2}
, you can use1|2
instead of1
.
– kglr
4 hours ago
add a comment |
The following works for removing several vertices and corresponding edges:
verts = {1, 2, 3, 4, 5};
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
vertdel = {1, 4}
verts2 = Complement[verts, vertdel]
edges2 = Complement[edges, Flatten[Select[edges, MemberQ[#]] & /@ vertdel, 1]]
{1, 4}
{2, 3, 5}
{{2, 3}, {2, 5}}
add a comment |
Tiny keyboard alert.
A = SparseArray[edges -> 1, {1 ,1} Max[edges]];
a = DiagonalMatrix[SparseArray[vertdel -> 0, {Length[A]}, 1]];
(a.A.a)["NonzeroPositions"]
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%2f188657%2fhow-to-remove-vertices-from-a-graph%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
These are done easily with graph functions:
g = Graph[Range[5], {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 1, 2 <-> 5}];
g2 = VertexDelete[g, 1];
EdgeList[g2]
(*
{2 <-> 3, 3 <-> 4, 2 <-> 5}
*)
Of course this works as well if you want to delete more than one vertex, e.g., vertices 1 and 5:
g2 = VertexDelete[g, {1, 5}];
add a comment |
These are done easily with graph functions:
g = Graph[Range[5], {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 1, 2 <-> 5}];
g2 = VertexDelete[g, 1];
EdgeList[g2]
(*
{2 <-> 3, 3 <-> 4, 2 <-> 5}
*)
Of course this works as well if you want to delete more than one vertex, e.g., vertices 1 and 5:
g2 = VertexDelete[g, {1, 5}];
add a comment |
These are done easily with graph functions:
g = Graph[Range[5], {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 1, 2 <-> 5}];
g2 = VertexDelete[g, 1];
EdgeList[g2]
(*
{2 <-> 3, 3 <-> 4, 2 <-> 5}
*)
Of course this works as well if you want to delete more than one vertex, e.g., vertices 1 and 5:
g2 = VertexDelete[g, {1, 5}];
These are done easily with graph functions:
g = Graph[Range[5], {1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 1, 2 <-> 5}];
g2 = VertexDelete[g, 1];
EdgeList[g2]
(*
{2 <-> 3, 3 <-> 4, 2 <-> 5}
*)
Of course this works as well if you want to delete more than one vertex, e.g., vertices 1 and 5:
g2 = VertexDelete[g, {1, 5}];
edited 5 hours ago
answered 5 hours ago
David G. Stork
23.1k22051
23.1k22051
add a comment |
add a comment |
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
A few more alternatives:
Select[edges, FreeQ[1]]
Pick[edges, FreeQ[1] /@ edges]
DeleteCases[edges, {_, 1} | {1, _}]
List @@@ EdgeList[VertexDelete[edges, 1]]
all give
{{2, 3}, {3, 4}, {2, 5}}
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
@geordie, if you want to remove a list of vertices, say{1,2}
, you can use1|2
instead of1
.
– kglr
4 hours ago
add a comment |
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
A few more alternatives:
Select[edges, FreeQ[1]]
Pick[edges, FreeQ[1] /@ edges]
DeleteCases[edges, {_, 1} | {1, _}]
List @@@ EdgeList[VertexDelete[edges, 1]]
all give
{{2, 3}, {3, 4}, {2, 5}}
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
@geordie, if you want to remove a list of vertices, say{1,2}
, you can use1|2
instead of1
.
– kglr
4 hours ago
add a comment |
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
A few more alternatives:
Select[edges, FreeQ[1]]
Pick[edges, FreeQ[1] /@ edges]
DeleteCases[edges, {_, 1} | {1, _}]
List @@@ EdgeList[VertexDelete[edges, 1]]
all give
{{2, 3}, {3, 4}, {2, 5}}
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
A few more alternatives:
Select[edges, FreeQ[1]]
Pick[edges, FreeQ[1] /@ edges]
DeleteCases[edges, {_, 1} | {1, _}]
List @@@ EdgeList[VertexDelete[edges, 1]]
all give
{{2, 3}, {3, 4}, {2, 5}}
answered 5 hours ago
kglr
177k9198404
177k9198404
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
@geordie, if you want to remove a list of vertices, say{1,2}
, you can use1|2
instead of1
.
– kglr
4 hours ago
add a comment |
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
@geordie, if you want to remove a list of vertices, say{1,2}
, you can use1|2
instead of1
.
– kglr
4 hours ago
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
Your first three suggestions only work for removing a single vertex.
– geordie
5 hours ago
1
1
@geordie, if you want to remove a list of vertices, say
{1,2}
, you can use 1|2
instead of 1
.– kglr
4 hours ago
@geordie, if you want to remove a list of vertices, say
{1,2}
, you can use 1|2
instead of 1
.– kglr
4 hours ago
add a comment |
The following works for removing several vertices and corresponding edges:
verts = {1, 2, 3, 4, 5};
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
vertdel = {1, 4}
verts2 = Complement[verts, vertdel]
edges2 = Complement[edges, Flatten[Select[edges, MemberQ[#]] & /@ vertdel, 1]]
{1, 4}
{2, 3, 5}
{{2, 3}, {2, 5}}
add a comment |
The following works for removing several vertices and corresponding edges:
verts = {1, 2, 3, 4, 5};
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
vertdel = {1, 4}
verts2 = Complement[verts, vertdel]
edges2 = Complement[edges, Flatten[Select[edges, MemberQ[#]] & /@ vertdel, 1]]
{1, 4}
{2, 3, 5}
{{2, 3}, {2, 5}}
add a comment |
The following works for removing several vertices and corresponding edges:
verts = {1, 2, 3, 4, 5};
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
vertdel = {1, 4}
verts2 = Complement[verts, vertdel]
edges2 = Complement[edges, Flatten[Select[edges, MemberQ[#]] & /@ vertdel, 1]]
{1, 4}
{2, 3, 5}
{{2, 3}, {2, 5}}
The following works for removing several vertices and corresponding edges:
verts = {1, 2, 3, 4, 5};
edges = {{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}};
vertdel = {1, 4}
verts2 = Complement[verts, vertdel]
edges2 = Complement[edges, Flatten[Select[edges, MemberQ[#]] & /@ vertdel, 1]]
{1, 4}
{2, 3, 5}
{{2, 3}, {2, 5}}
answered 5 hours ago
geordie
1,9781530
1,9781530
add a comment |
add a comment |
Tiny keyboard alert.
A = SparseArray[edges -> 1, {1 ,1} Max[edges]];
a = DiagonalMatrix[SparseArray[vertdel -> 0, {Length[A]}, 1]];
(a.A.a)["NonzeroPositions"]
add a comment |
Tiny keyboard alert.
A = SparseArray[edges -> 1, {1 ,1} Max[edges]];
a = DiagonalMatrix[SparseArray[vertdel -> 0, {Length[A]}, 1]];
(a.A.a)["NonzeroPositions"]
add a comment |
Tiny keyboard alert.
A = SparseArray[edges -> 1, {1 ,1} Max[edges]];
a = DiagonalMatrix[SparseArray[vertdel -> 0, {Length[A]}, 1]];
(a.A.a)["NonzeroPositions"]
Tiny keyboard alert.
A = SparseArray[edges -> 1, {1 ,1} Max[edges]];
a = DiagonalMatrix[SparseArray[vertdel -> 0, {Length[A]}, 1]];
(a.A.a)["NonzeroPositions"]
edited 4 hours ago
answered 4 hours ago
Henrik Schumacher
48.8k467139
48.8k467139
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2f188657%2fhow-to-remove-vertices-from-a-graph%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