How to supress loops in a digraph?Dynamic graph construction and visualization (Watts' “Small Worlds” α-model)LayeredGraphPlot Vertex Spacing / Edge LengthHow can I reduce a directed graph to only its “junctions”?How to generate all Feynman diagrams with Mathematica?How to filter-out edges in a HighlightGraph[] visualization based on VertexCoordinates[]?Completing graph edges “around the world” to GeoPlot an itineraryHopcroft's Algorithm for Minimization of Deterministic Finite State Automaton
Single-key teletype?
How can I end combat quickly when the outcome is inevitable?
Traversing Oceania: A Cryptic Journey
How come the nude protesters were not arrested?
Are there any important biographies of nobodies?
Active low-pass filters --- good to what frequencies?
How is the excise border managed in Ireland?
Who enforces MPAA rating adherence?
Cascading Switches. Will it affect performance?
How to safely destroy (a large quantity of) valid checks?
Overlapping String-Blocks
Can I utilise a baking stone to make crepes?
If I leave the US through an airport, do I have to return through the same airport?
Which languages would be most useful in Europe at the end of the 19th century?
Getting UPS Power from One Room to Another
What is the color of artificial intelligence?
Is an entry level DSLR going to shoot nice portrait pictures?
How do you say "homebrewer" in Spanish?
How can I get an unreasonable manager to approve time off?
Does the Long March-11 increase its thrust after clearing the launch tower?
Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?
Is it legal for a bar bouncer to confiscate a fake ID
Has there been a multiethnic Star Trek character?
Does the 2019 UA Artificer's Many-Handed Pouch infusion enable unlimited infinite-range cross-planar communication?
How to supress loops in a digraph?
Dynamic graph construction and visualization (Watts' “Small Worlds” α-model)LayeredGraphPlot Vertex Spacing / Edge LengthHow can I reduce a directed graph to only its “junctions”?How to generate all Feynman diagrams with Mathematica?How to filter-out edges in a HighlightGraph[] visualization based on VertexCoordinates[]?Completing graph edges “around the world” to GeoPlot an itineraryHopcroft's Algorithm for Minimization of Deterministic Finite State Automaton
$begingroup$
I have the following Code
working nicely (Part of this code has been developed by users in this forum).
Clear[vLabels, selectAbove, subgraphAbove];
mm = RandomReal[0.5, 10, 10];
vLabels = 1 -> AGR, 2 -> FIS, 3 -> CO1, 4 -> CO2, 5 -> MA1, 6 -> MA2,
7 -> EGW, 8 -> CST, 9 -> WHS, 10 -> HOT;
selectAbove[[Theta]_] := BoolEval[[Theta] < mm];
subgraphAbove[[Theta]_] := AdjacencyGraph[selectAbove[[Theta]],
PlotLabel -> [Theta] < Subscript[m, ij], VertexSize -> 0.3,
VertexLabels -> vLabels , ImagePadding -> 20, ImageSize -> 150];
Manipulate[
subgraphAbove[[Theta]], [Theta], 0.001, "Threshold [Theta] <",
0, 0.5, 0.01]
I simply do not want to see the loops in this digraph. I looked for a command to suppress the loops, but to my surprise no single command exists. Or I could find it in my search.
I will appreciate if you suggest me a way to suppress the loops.
- EDIT 1
I face a puzzle here. The Code
given above simply counts the number of edges in an interval theta1
and theta2
. If I keep the maximum range to find the maximum number of edges in the entire graph, it gives me a puzzling
result. To see the puzzle, let us just look at the following three maps.
The first graph indicates that in the range given, the number of linkages or edges is 136. (Keep in mind that histogram is giving the right and consistent answer.)
The second graph indicates that in the range given, the number of linkages or edges is 271, which is correct. (Keep in mind that histogram is giving the right and consistent answer again.)
The puzzle is that, although I reduce the range, expecting that I will get a smaller number of edges compared to the range covering the entire graph, I receive a larger number that 136.
Can somebody tell me what I am missing here or Mathematica
is doing something that I cannot see. The additional puzzle is that Histogram always gives the correct number (I checked it through almost all ranges.) but a simple command EdgeCount
does not behave in the expected manner.
See the following full Code
to replicate the situation, where the mm
matrix is large and hence you may check it with random numbers between 0-0.5:
Clear[selectBetween, subgraphBetween];
selectBetween[[Theta]1_, [Theta]2_] :=
BoolEval[[Theta]1 <= mm <= [Theta]2];
subgraphBetween[[Theta]1_, [Theta]2_] :=
AdjacencyGraph[selectBetween[[Theta]1, [Theta]2],
PlotLabel -> [Theta]1 <= Subscript[m, ij] <= [Theta]2,
VertexSize -> 0.3, VertexLabels -> vLabels , ImagePadding -> 20,
ImageSize -> 150];
Manipulate[Grid[
"Digraph within the range for " <>
ToString[Subscript[m, ij], TraditionalForm],
"Total number of edges in the interval",
"Histogram", subgraphBetween[[Theta]1, [Theta]2],
EdgeCount[subgraphBetween[[Theta]1, [Theta]2]],
Histogram[0, binlims =
Range[[Theta]1, [Theta]2, ([Theta]2 - [Theta]1)/
nbars], (EdgeCount[subgraphBetween[##]] & @@@
Partition[binlims, 2, 1] &), LabelingFunction -> Above] //
Transpose
],
[Theta]1, 0.00001,
"Threshold [Theta]1 < " <>
ToString[Subscript[m, ij], TraditionalForm], 0.00001, 0.5,
0.005, [Theta]2, 0.0001,
ToString[Subscript[m, ij], TraditionalForm] <>
" < Threshold [Theta]2", [Theta]1 + 0.00001, 0.5, 0.005,
nbars, 10, 1, 100, 1
]
graphs-and-networks
$endgroup$
add a comment |
$begingroup$
I have the following Code
working nicely (Part of this code has been developed by users in this forum).
Clear[vLabels, selectAbove, subgraphAbove];
mm = RandomReal[0.5, 10, 10];
vLabels = 1 -> AGR, 2 -> FIS, 3 -> CO1, 4 -> CO2, 5 -> MA1, 6 -> MA2,
7 -> EGW, 8 -> CST, 9 -> WHS, 10 -> HOT;
selectAbove[[Theta]_] := BoolEval[[Theta] < mm];
subgraphAbove[[Theta]_] := AdjacencyGraph[selectAbove[[Theta]],
PlotLabel -> [Theta] < Subscript[m, ij], VertexSize -> 0.3,
VertexLabels -> vLabels , ImagePadding -> 20, ImageSize -> 150];
Manipulate[
subgraphAbove[[Theta]], [Theta], 0.001, "Threshold [Theta] <",
0, 0.5, 0.01]
I simply do not want to see the loops in this digraph. I looked for a command to suppress the loops, but to my surprise no single command exists. Or I could find it in my search.
I will appreciate if you suggest me a way to suppress the loops.
- EDIT 1
I face a puzzle here. The Code
given above simply counts the number of edges in an interval theta1
and theta2
. If I keep the maximum range to find the maximum number of edges in the entire graph, it gives me a puzzling
result. To see the puzzle, let us just look at the following three maps.
The first graph indicates that in the range given, the number of linkages or edges is 136. (Keep in mind that histogram is giving the right and consistent answer.)
The second graph indicates that in the range given, the number of linkages or edges is 271, which is correct. (Keep in mind that histogram is giving the right and consistent answer again.)
The puzzle is that, although I reduce the range, expecting that I will get a smaller number of edges compared to the range covering the entire graph, I receive a larger number that 136.
Can somebody tell me what I am missing here or Mathematica
is doing something that I cannot see. The additional puzzle is that Histogram always gives the correct number (I checked it through almost all ranges.) but a simple command EdgeCount
does not behave in the expected manner.
See the following full Code
to replicate the situation, where the mm
matrix is large and hence you may check it with random numbers between 0-0.5:
Clear[selectBetween, subgraphBetween];
selectBetween[[Theta]1_, [Theta]2_] :=
BoolEval[[Theta]1 <= mm <= [Theta]2];
subgraphBetween[[Theta]1_, [Theta]2_] :=
AdjacencyGraph[selectBetween[[Theta]1, [Theta]2],
PlotLabel -> [Theta]1 <= Subscript[m, ij] <= [Theta]2,
VertexSize -> 0.3, VertexLabels -> vLabels , ImagePadding -> 20,
ImageSize -> 150];
Manipulate[Grid[
"Digraph within the range for " <>
ToString[Subscript[m, ij], TraditionalForm],
"Total number of edges in the interval",
"Histogram", subgraphBetween[[Theta]1, [Theta]2],
EdgeCount[subgraphBetween[[Theta]1, [Theta]2]],
Histogram[0, binlims =
Range[[Theta]1, [Theta]2, ([Theta]2 - [Theta]1)/
nbars], (EdgeCount[subgraphBetween[##]] & @@@
Partition[binlims, 2, 1] &), LabelingFunction -> Above] //
Transpose
],
[Theta]1, 0.00001,
"Threshold [Theta]1 < " <>
ToString[Subscript[m, ij], TraditionalForm], 0.00001, 0.5,
0.005, [Theta]2, 0.0001,
ToString[Subscript[m, ij], TraditionalForm] <>
" < Threshold [Theta]2", [Theta]1 + 0.00001, 0.5, 0.005,
nbars, 10, 1, 100, 1
]
graphs-and-networks
$endgroup$
2
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46
add a comment |
$begingroup$
I have the following Code
working nicely (Part of this code has been developed by users in this forum).
Clear[vLabels, selectAbove, subgraphAbove];
mm = RandomReal[0.5, 10, 10];
vLabels = 1 -> AGR, 2 -> FIS, 3 -> CO1, 4 -> CO2, 5 -> MA1, 6 -> MA2,
7 -> EGW, 8 -> CST, 9 -> WHS, 10 -> HOT;
selectAbove[[Theta]_] := BoolEval[[Theta] < mm];
subgraphAbove[[Theta]_] := AdjacencyGraph[selectAbove[[Theta]],
PlotLabel -> [Theta] < Subscript[m, ij], VertexSize -> 0.3,
VertexLabels -> vLabels , ImagePadding -> 20, ImageSize -> 150];
Manipulate[
subgraphAbove[[Theta]], [Theta], 0.001, "Threshold [Theta] <",
0, 0.5, 0.01]
I simply do not want to see the loops in this digraph. I looked for a command to suppress the loops, but to my surprise no single command exists. Or I could find it in my search.
I will appreciate if you suggest me a way to suppress the loops.
- EDIT 1
I face a puzzle here. The Code
given above simply counts the number of edges in an interval theta1
and theta2
. If I keep the maximum range to find the maximum number of edges in the entire graph, it gives me a puzzling
result. To see the puzzle, let us just look at the following three maps.
The first graph indicates that in the range given, the number of linkages or edges is 136. (Keep in mind that histogram is giving the right and consistent answer.)
The second graph indicates that in the range given, the number of linkages or edges is 271, which is correct. (Keep in mind that histogram is giving the right and consistent answer again.)
The puzzle is that, although I reduce the range, expecting that I will get a smaller number of edges compared to the range covering the entire graph, I receive a larger number that 136.
Can somebody tell me what I am missing here or Mathematica
is doing something that I cannot see. The additional puzzle is that Histogram always gives the correct number (I checked it through almost all ranges.) but a simple command EdgeCount
does not behave in the expected manner.
See the following full Code
to replicate the situation, where the mm
matrix is large and hence you may check it with random numbers between 0-0.5:
Clear[selectBetween, subgraphBetween];
selectBetween[[Theta]1_, [Theta]2_] :=
BoolEval[[Theta]1 <= mm <= [Theta]2];
subgraphBetween[[Theta]1_, [Theta]2_] :=
AdjacencyGraph[selectBetween[[Theta]1, [Theta]2],
PlotLabel -> [Theta]1 <= Subscript[m, ij] <= [Theta]2,
VertexSize -> 0.3, VertexLabels -> vLabels , ImagePadding -> 20,
ImageSize -> 150];
Manipulate[Grid[
"Digraph within the range for " <>
ToString[Subscript[m, ij], TraditionalForm],
"Total number of edges in the interval",
"Histogram", subgraphBetween[[Theta]1, [Theta]2],
EdgeCount[subgraphBetween[[Theta]1, [Theta]2]],
Histogram[0, binlims =
Range[[Theta]1, [Theta]2, ([Theta]2 - [Theta]1)/
nbars], (EdgeCount[subgraphBetween[##]] & @@@
Partition[binlims, 2, 1] &), LabelingFunction -> Above] //
Transpose
],
[Theta]1, 0.00001,
"Threshold [Theta]1 < " <>
ToString[Subscript[m, ij], TraditionalForm], 0.00001, 0.5,
0.005, [Theta]2, 0.0001,
ToString[Subscript[m, ij], TraditionalForm] <>
" < Threshold [Theta]2", [Theta]1 + 0.00001, 0.5, 0.005,
nbars, 10, 1, 100, 1
]
graphs-and-networks
$endgroup$
I have the following Code
working nicely (Part of this code has been developed by users in this forum).
Clear[vLabels, selectAbove, subgraphAbove];
mm = RandomReal[0.5, 10, 10];
vLabels = 1 -> AGR, 2 -> FIS, 3 -> CO1, 4 -> CO2, 5 -> MA1, 6 -> MA2,
7 -> EGW, 8 -> CST, 9 -> WHS, 10 -> HOT;
selectAbove[[Theta]_] := BoolEval[[Theta] < mm];
subgraphAbove[[Theta]_] := AdjacencyGraph[selectAbove[[Theta]],
PlotLabel -> [Theta] < Subscript[m, ij], VertexSize -> 0.3,
VertexLabels -> vLabels , ImagePadding -> 20, ImageSize -> 150];
Manipulate[
subgraphAbove[[Theta]], [Theta], 0.001, "Threshold [Theta] <",
0, 0.5, 0.01]
I simply do not want to see the loops in this digraph. I looked for a command to suppress the loops, but to my surprise no single command exists. Or I could find it in my search.
I will appreciate if you suggest me a way to suppress the loops.
- EDIT 1
I face a puzzle here. The Code
given above simply counts the number of edges in an interval theta1
and theta2
. If I keep the maximum range to find the maximum number of edges in the entire graph, it gives me a puzzling
result. To see the puzzle, let us just look at the following three maps.
The first graph indicates that in the range given, the number of linkages or edges is 136. (Keep in mind that histogram is giving the right and consistent answer.)
The second graph indicates that in the range given, the number of linkages or edges is 271, which is correct. (Keep in mind that histogram is giving the right and consistent answer again.)
The puzzle is that, although I reduce the range, expecting that I will get a smaller number of edges compared to the range covering the entire graph, I receive a larger number that 136.
Can somebody tell me what I am missing here or Mathematica
is doing something that I cannot see. The additional puzzle is that Histogram always gives the correct number (I checked it through almost all ranges.) but a simple command EdgeCount
does not behave in the expected manner.
See the following full Code
to replicate the situation, where the mm
matrix is large and hence you may check it with random numbers between 0-0.5:
Clear[selectBetween, subgraphBetween];
selectBetween[[Theta]1_, [Theta]2_] :=
BoolEval[[Theta]1 <= mm <= [Theta]2];
subgraphBetween[[Theta]1_, [Theta]2_] :=
AdjacencyGraph[selectBetween[[Theta]1, [Theta]2],
PlotLabel -> [Theta]1 <= Subscript[m, ij] <= [Theta]2,
VertexSize -> 0.3, VertexLabels -> vLabels , ImagePadding -> 20,
ImageSize -> 150];
Manipulate[Grid[
"Digraph within the range for " <>
ToString[Subscript[m, ij], TraditionalForm],
"Total number of edges in the interval",
"Histogram", subgraphBetween[[Theta]1, [Theta]2],
EdgeCount[subgraphBetween[[Theta]1, [Theta]2]],
Histogram[0, binlims =
Range[[Theta]1, [Theta]2, ([Theta]2 - [Theta]1)/
nbars], (EdgeCount[subgraphBetween[##]] & @@@
Partition[binlims, 2, 1] &), LabelingFunction -> Above] //
Transpose
],
[Theta]1, 0.00001,
"Threshold [Theta]1 < " <>
ToString[Subscript[m, ij], TraditionalForm], 0.00001, 0.5,
0.005, [Theta]2, 0.0001,
ToString[Subscript[m, ij], TraditionalForm] <>
" < Threshold [Theta]2", [Theta]1 + 0.00001, 0.5, 0.005,
nbars, 10, 1, 100, 1
]
graphs-and-networks
graphs-and-networks
edited Jun 1 at 16:45
Tugrul Temel
asked Jun 1 at 1:48
Tugrul TemelTugrul Temel
1,014213
1,014213
2
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46
add a comment |
2
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46
2
2
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Use SimpleGraph
:
SimpleGraph[g] removes all self-loops and multiple edges between the same vertices.
Manipulate[SimpleGraph[g = subgraphAbove[θ], ImageSize -> Medium, Options[g]],
θ, 0.001, "Threshold θ <", 0, 0.5, 0.01, Alignment -> Center]
$endgroup$
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
$begingroup$
@Tugrul, you can useSimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options ofsubgraphAbove[[Theta]]
inSimpleGraph
.
$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
$begingroup$
@TugrulTemel Beware that just transferringOptions[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.
$endgroup$
– Szabolcs
Jun 1 at 14:55
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
|
show 2 more comments
$begingroup$
As others said, SimpleGraph
will remove both loops and multi-edges. Often this is all you need. If you need to control the removal of loops and multi-edges separately, you can use IGSimpleGraph
from IGraph/M.
Create a graph.
g = IGShorthand["1->2->3->1->2->2->1",
MultiEdges -> True, SelfLoops -> True]
Make the graph simple.
IGSimpleGraph[g]
Preserve self-loops, but not parallel edges.
IGSimpleGraph[g, SelfLoops -> True]
IGSimpleGraph[g, MultiEdges -> True]
Preserve parallel edges but not self-loops.
IGSimpleGraph
does not currently preserve graph properties such as egde weights (SimpleGraph
does, but only in M12.0+). IGraph/M also provides IGWeightedSimpleGraph
which takes the same options, but preserves edge weights, the most commonly needed edge property.
You could also use EdgeDelete
to remove self-loops. In a directed graph, use
EdgeDelete[g, x_ [DirectedEdge] x_]
In M11.3 and earlier, EdgeDelete
was buggy and would often break the graph if it had properties (styling also counts as properties). In M12.0 it is finally fixed, therefore I can finally recommend it (for M12.0+ only!)
Another option, which is specific to your setup, is to remove the diagonal of the adjacency matrix before using AdjacencyGraph
. You can do this using IGZeroDiagonal[matrix]
(which is also part of IGraph/M).
Finally, if you have an already constructed graph with properties (such as vertex labels), you need to preserve the properties, and you have Mathematica 11.3 or earlier, then you can use IGTakeSubgraph
.
sg = subgraphAbove[.23];
IGTakeSubgraph[sg, DeleteCases[EdgeList[sg], x_ [DirectedEdge] x_]]
The second argument of IGTakeSubgraph
can be a set of edges, or a graph. It will keep only those edges from the input graph (the first argument). IGTakeSubgraph
is quite slow, but it's the most convenient way to take a subgraph and preserve properties in Mathematica 11.3 and earlier. In Mathematica 12.0 and later, the built-in Subgraph
, VertexDelete
and EdgeDelete
already preserve properties.
$endgroup$
$begingroup$
It works butIGSimpleGraph
remove the vertex names also.SimpleGraph
removes the names too. I like to keep them as they are if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
$begingroup$
@TugrulTemelSimpleGraph
does not remove them in Mathematica 12.0 (finally!!)
$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I haveMath 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.
$endgroup$
– Tugrul Temel
Jun 1 at 14:46
add a comment |
$begingroup$
myGraph = Graph[1 -> 2, 1 -> 3, 2 -> 3, 2 -> 2]
fixedMatrix =
Table[If[i == j, 0, AdjacencyMatrix[myGraph][[i, j]]],
i, 1, 3,
j, 1, 3];
AdjacencyGraph[fixedMatrix]
Could also use:
fixedMatrix =
ReplacePart[AdjacencyMatrix[myGraph], Table[i, i -> 0, i, 1, 3]]
Or
SimpleGraph[myGraph]
$endgroup$
add a comment |
Your Answer
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%2f199492%2fhow-to-supress-loops-in-a-digraph%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Use SimpleGraph
:
SimpleGraph[g] removes all self-loops and multiple edges between the same vertices.
Manipulate[SimpleGraph[g = subgraphAbove[θ], ImageSize -> Medium, Options[g]],
θ, 0.001, "Threshold θ <", 0, 0.5, 0.01, Alignment -> Center]
$endgroup$
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
$begingroup$
@Tugrul, you can useSimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options ofsubgraphAbove[[Theta]]
inSimpleGraph
.
$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
$begingroup$
@TugrulTemel Beware that just transferringOptions[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.
$endgroup$
– Szabolcs
Jun 1 at 14:55
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
|
show 2 more comments
$begingroup$
Use SimpleGraph
:
SimpleGraph[g] removes all self-loops and multiple edges between the same vertices.
Manipulate[SimpleGraph[g = subgraphAbove[θ], ImageSize -> Medium, Options[g]],
θ, 0.001, "Threshold θ <", 0, 0.5, 0.01, Alignment -> Center]
$endgroup$
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
$begingroup$
@Tugrul, you can useSimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options ofsubgraphAbove[[Theta]]
inSimpleGraph
.
$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
$begingroup$
@TugrulTemel Beware that just transferringOptions[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.
$endgroup$
– Szabolcs
Jun 1 at 14:55
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
|
show 2 more comments
$begingroup$
Use SimpleGraph
:
SimpleGraph[g] removes all self-loops and multiple edges between the same vertices.
Manipulate[SimpleGraph[g = subgraphAbove[θ], ImageSize -> Medium, Options[g]],
θ, 0.001, "Threshold θ <", 0, 0.5, 0.01, Alignment -> Center]
$endgroup$
Use SimpleGraph
:
SimpleGraph[g] removes all self-loops and multiple edges between the same vertices.
Manipulate[SimpleGraph[g = subgraphAbove[θ], ImageSize -> Medium, Options[g]],
θ, 0.001, "Threshold θ <", 0, 0.5, 0.01, Alignment -> Center]
edited Jun 1 at 12:01
answered Jun 1 at 2:57
kglrkglr
197k10220442
197k10220442
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
$begingroup$
@Tugrul, you can useSimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options ofsubgraphAbove[[Theta]]
inSimpleGraph
.
$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
$begingroup$
@TugrulTemel Beware that just transferringOptions[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.
$endgroup$
– Szabolcs
Jun 1 at 14:55
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
|
show 2 more comments
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
$begingroup$
@Tugrul, you can useSimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options ofsubgraphAbove[[Theta]]
inSimpleGraph
.
$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
$begingroup$
@TugrulTemel Beware that just transferringOptions[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.
$endgroup$
– Szabolcs
Jun 1 at 14:55
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
$begingroup$
Thank you very much. It works but somehow it removes the vertex names from the graph. I like to preserve the vertex names if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:47
1
1
$begingroup$
@Tugrul, you can use
SimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options of subgraphAbove[[Theta]]
in SimpleGraph
.$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
@Tugrul, you can use
SimpleGraph[g = subgraphAbove[[Theta]], Options[g]]
to retain the options of subgraphAbove[[Theta]]
in SimpleGraph
.$endgroup$
– kglr
Jun 1 at 11:35
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
$begingroup$
Yes, finally, I got it and it works in the way I wanted. Thank you very much.
$endgroup$
– Tugrul Temel
Jun 1 at 11:56
1
1
$begingroup$
@TugrulTemel Beware that just transferring
Options[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.$endgroup$
– Szabolcs
Jun 1 at 14:55
$begingroup$
@TugrulTemel Beware that just transferring
Options[graph]
is not a robust solution. It won't work if the graph has edge properties (e.g. edge weights) as some edges are removed.$endgroup$
– Szabolcs
Jun 1 at 14:55
1
1
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
$begingroup$
For your case the best solution is to zero out the diagonal of the matrix before creating the graph (I updated my answer)
$endgroup$
– Szabolcs
Jun 1 at 14:56
|
show 2 more comments
$begingroup$
As others said, SimpleGraph
will remove both loops and multi-edges. Often this is all you need. If you need to control the removal of loops and multi-edges separately, you can use IGSimpleGraph
from IGraph/M.
Create a graph.
g = IGShorthand["1->2->3->1->2->2->1",
MultiEdges -> True, SelfLoops -> True]
Make the graph simple.
IGSimpleGraph[g]
Preserve self-loops, but not parallel edges.
IGSimpleGraph[g, SelfLoops -> True]
IGSimpleGraph[g, MultiEdges -> True]
Preserve parallel edges but not self-loops.
IGSimpleGraph
does not currently preserve graph properties such as egde weights (SimpleGraph
does, but only in M12.0+). IGraph/M also provides IGWeightedSimpleGraph
which takes the same options, but preserves edge weights, the most commonly needed edge property.
You could also use EdgeDelete
to remove self-loops. In a directed graph, use
EdgeDelete[g, x_ [DirectedEdge] x_]
In M11.3 and earlier, EdgeDelete
was buggy and would often break the graph if it had properties (styling also counts as properties). In M12.0 it is finally fixed, therefore I can finally recommend it (for M12.0+ only!)
Another option, which is specific to your setup, is to remove the diagonal of the adjacency matrix before using AdjacencyGraph
. You can do this using IGZeroDiagonal[matrix]
(which is also part of IGraph/M).
Finally, if you have an already constructed graph with properties (such as vertex labels), you need to preserve the properties, and you have Mathematica 11.3 or earlier, then you can use IGTakeSubgraph
.
sg = subgraphAbove[.23];
IGTakeSubgraph[sg, DeleteCases[EdgeList[sg], x_ [DirectedEdge] x_]]
The second argument of IGTakeSubgraph
can be a set of edges, or a graph. It will keep only those edges from the input graph (the first argument). IGTakeSubgraph
is quite slow, but it's the most convenient way to take a subgraph and preserve properties in Mathematica 11.3 and earlier. In Mathematica 12.0 and later, the built-in Subgraph
, VertexDelete
and EdgeDelete
already preserve properties.
$endgroup$
$begingroup$
It works butIGSimpleGraph
remove the vertex names also.SimpleGraph
removes the names too. I like to keep them as they are if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
$begingroup$
@TugrulTemelSimpleGraph
does not remove them in Mathematica 12.0 (finally!!)
$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I haveMath 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.
$endgroup$
– Tugrul Temel
Jun 1 at 14:46
add a comment |
$begingroup$
As others said, SimpleGraph
will remove both loops and multi-edges. Often this is all you need. If you need to control the removal of loops and multi-edges separately, you can use IGSimpleGraph
from IGraph/M.
Create a graph.
g = IGShorthand["1->2->3->1->2->2->1",
MultiEdges -> True, SelfLoops -> True]
Make the graph simple.
IGSimpleGraph[g]
Preserve self-loops, but not parallel edges.
IGSimpleGraph[g, SelfLoops -> True]
IGSimpleGraph[g, MultiEdges -> True]
Preserve parallel edges but not self-loops.
IGSimpleGraph
does not currently preserve graph properties such as egde weights (SimpleGraph
does, but only in M12.0+). IGraph/M also provides IGWeightedSimpleGraph
which takes the same options, but preserves edge weights, the most commonly needed edge property.
You could also use EdgeDelete
to remove self-loops. In a directed graph, use
EdgeDelete[g, x_ [DirectedEdge] x_]
In M11.3 and earlier, EdgeDelete
was buggy and would often break the graph if it had properties (styling also counts as properties). In M12.0 it is finally fixed, therefore I can finally recommend it (for M12.0+ only!)
Another option, which is specific to your setup, is to remove the diagonal of the adjacency matrix before using AdjacencyGraph
. You can do this using IGZeroDiagonal[matrix]
(which is also part of IGraph/M).
Finally, if you have an already constructed graph with properties (such as vertex labels), you need to preserve the properties, and you have Mathematica 11.3 or earlier, then you can use IGTakeSubgraph
.
sg = subgraphAbove[.23];
IGTakeSubgraph[sg, DeleteCases[EdgeList[sg], x_ [DirectedEdge] x_]]
The second argument of IGTakeSubgraph
can be a set of edges, or a graph. It will keep only those edges from the input graph (the first argument). IGTakeSubgraph
is quite slow, but it's the most convenient way to take a subgraph and preserve properties in Mathematica 11.3 and earlier. In Mathematica 12.0 and later, the built-in Subgraph
, VertexDelete
and EdgeDelete
already preserve properties.
$endgroup$
$begingroup$
It works butIGSimpleGraph
remove the vertex names also.SimpleGraph
removes the names too. I like to keep them as they are if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
$begingroup$
@TugrulTemelSimpleGraph
does not remove them in Mathematica 12.0 (finally!!)
$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I haveMath 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.
$endgroup$
– Tugrul Temel
Jun 1 at 14:46
add a comment |
$begingroup$
As others said, SimpleGraph
will remove both loops and multi-edges. Often this is all you need. If you need to control the removal of loops and multi-edges separately, you can use IGSimpleGraph
from IGraph/M.
Create a graph.
g = IGShorthand["1->2->3->1->2->2->1",
MultiEdges -> True, SelfLoops -> True]
Make the graph simple.
IGSimpleGraph[g]
Preserve self-loops, but not parallel edges.
IGSimpleGraph[g, SelfLoops -> True]
IGSimpleGraph[g, MultiEdges -> True]
Preserve parallel edges but not self-loops.
IGSimpleGraph
does not currently preserve graph properties such as egde weights (SimpleGraph
does, but only in M12.0+). IGraph/M also provides IGWeightedSimpleGraph
which takes the same options, but preserves edge weights, the most commonly needed edge property.
You could also use EdgeDelete
to remove self-loops. In a directed graph, use
EdgeDelete[g, x_ [DirectedEdge] x_]
In M11.3 and earlier, EdgeDelete
was buggy and would often break the graph if it had properties (styling also counts as properties). In M12.0 it is finally fixed, therefore I can finally recommend it (for M12.0+ only!)
Another option, which is specific to your setup, is to remove the diagonal of the adjacency matrix before using AdjacencyGraph
. You can do this using IGZeroDiagonal[matrix]
(which is also part of IGraph/M).
Finally, if you have an already constructed graph with properties (such as vertex labels), you need to preserve the properties, and you have Mathematica 11.3 or earlier, then you can use IGTakeSubgraph
.
sg = subgraphAbove[.23];
IGTakeSubgraph[sg, DeleteCases[EdgeList[sg], x_ [DirectedEdge] x_]]
The second argument of IGTakeSubgraph
can be a set of edges, or a graph. It will keep only those edges from the input graph (the first argument). IGTakeSubgraph
is quite slow, but it's the most convenient way to take a subgraph and preserve properties in Mathematica 11.3 and earlier. In Mathematica 12.0 and later, the built-in Subgraph
, VertexDelete
and EdgeDelete
already preserve properties.
$endgroup$
As others said, SimpleGraph
will remove both loops and multi-edges. Often this is all you need. If you need to control the removal of loops and multi-edges separately, you can use IGSimpleGraph
from IGraph/M.
Create a graph.
g = IGShorthand["1->2->3->1->2->2->1",
MultiEdges -> True, SelfLoops -> True]
Make the graph simple.
IGSimpleGraph[g]
Preserve self-loops, but not parallel edges.
IGSimpleGraph[g, SelfLoops -> True]
IGSimpleGraph[g, MultiEdges -> True]
Preserve parallel edges but not self-loops.
IGSimpleGraph
does not currently preserve graph properties such as egde weights (SimpleGraph
does, but only in M12.0+). IGraph/M also provides IGWeightedSimpleGraph
which takes the same options, but preserves edge weights, the most commonly needed edge property.
You could also use EdgeDelete
to remove self-loops. In a directed graph, use
EdgeDelete[g, x_ [DirectedEdge] x_]
In M11.3 and earlier, EdgeDelete
was buggy and would often break the graph if it had properties (styling also counts as properties). In M12.0 it is finally fixed, therefore I can finally recommend it (for M12.0+ only!)
Another option, which is specific to your setup, is to remove the diagonal of the adjacency matrix before using AdjacencyGraph
. You can do this using IGZeroDiagonal[matrix]
(which is also part of IGraph/M).
Finally, if you have an already constructed graph with properties (such as vertex labels), you need to preserve the properties, and you have Mathematica 11.3 or earlier, then you can use IGTakeSubgraph
.
sg = subgraphAbove[.23];
IGTakeSubgraph[sg, DeleteCases[EdgeList[sg], x_ [DirectedEdge] x_]]
The second argument of IGTakeSubgraph
can be a set of edges, or a graph. It will keep only those edges from the input graph (the first argument). IGTakeSubgraph
is quite slow, but it's the most convenient way to take a subgraph and preserve properties in Mathematica 11.3 and earlier. In Mathematica 12.0 and later, the built-in Subgraph
, VertexDelete
and EdgeDelete
already preserve properties.
edited Jun 1 at 14:31
answered Jun 1 at 6:30
SzabolcsSzabolcs
168k17460974
168k17460974
$begingroup$
It works butIGSimpleGraph
remove the vertex names also.SimpleGraph
removes the names too. I like to keep them as they are if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
$begingroup$
@TugrulTemelSimpleGraph
does not remove them in Mathematica 12.0 (finally!!)
$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I haveMath 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.
$endgroup$
– Tugrul Temel
Jun 1 at 14:46
add a comment |
$begingroup$
It works butIGSimpleGraph
remove the vertex names also.SimpleGraph
removes the names too. I like to keep them as they are if possible.
$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
$begingroup$
@TugrulTemelSimpleGraph
does not remove them in Mathematica 12.0 (finally!!)
$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I haveMath 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.
$endgroup$
– Tugrul Temel
Jun 1 at 14:46
$begingroup$
It works but
IGSimpleGraph
remove the vertex names also. SimpleGraph
removes the names too. I like to keep them as they are if possible.$endgroup$
– Tugrul Temel
Jun 1 at 10:49
$begingroup$
It works but
IGSimpleGraph
remove the vertex names also. SimpleGraph
removes the names too. I like to keep them as they are if possible.$endgroup$
– Tugrul Temel
Jun 1 at 10:49
1
1
$begingroup$
@TugrulTemel
SimpleGraph
does not remove them in Mathematica 12.0 (finally!!)$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
@TugrulTemel
SimpleGraph
does not remove them in Mathematica 12.0 (finally!!)$endgroup$
– Szabolcs
Jun 1 at 14:25
$begingroup$
I have
Math 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.$endgroup$
– Tugrul Temel
Jun 1 at 14:46
$begingroup$
I have
Math 11
. @klrg suggested a solution for this issue. I want to thank you all for your time and help.$endgroup$
– Tugrul Temel
Jun 1 at 14:46
add a comment |
$begingroup$
myGraph = Graph[1 -> 2, 1 -> 3, 2 -> 3, 2 -> 2]
fixedMatrix =
Table[If[i == j, 0, AdjacencyMatrix[myGraph][[i, j]]],
i, 1, 3,
j, 1, 3];
AdjacencyGraph[fixedMatrix]
Could also use:
fixedMatrix =
ReplacePart[AdjacencyMatrix[myGraph], Table[i, i -> 0, i, 1, 3]]
Or
SimpleGraph[myGraph]
$endgroup$
add a comment |
$begingroup$
myGraph = Graph[1 -> 2, 1 -> 3, 2 -> 3, 2 -> 2]
fixedMatrix =
Table[If[i == j, 0, AdjacencyMatrix[myGraph][[i, j]]],
i, 1, 3,
j, 1, 3];
AdjacencyGraph[fixedMatrix]
Could also use:
fixedMatrix =
ReplacePart[AdjacencyMatrix[myGraph], Table[i, i -> 0, i, 1, 3]]
Or
SimpleGraph[myGraph]
$endgroup$
add a comment |
$begingroup$
myGraph = Graph[1 -> 2, 1 -> 3, 2 -> 3, 2 -> 2]
fixedMatrix =
Table[If[i == j, 0, AdjacencyMatrix[myGraph][[i, j]]],
i, 1, 3,
j, 1, 3];
AdjacencyGraph[fixedMatrix]
Could also use:
fixedMatrix =
ReplacePart[AdjacencyMatrix[myGraph], Table[i, i -> 0, i, 1, 3]]
Or
SimpleGraph[myGraph]
$endgroup$
myGraph = Graph[1 -> 2, 1 -> 3, 2 -> 3, 2 -> 2]
fixedMatrix =
Table[If[i == j, 0, AdjacencyMatrix[myGraph][[i, j]]],
i, 1, 3,
j, 1, 3];
AdjacencyGraph[fixedMatrix]
Could also use:
fixedMatrix =
ReplacePart[AdjacencyMatrix[myGraph], Table[i, i -> 0, i, 1, 3]]
Or
SimpleGraph[myGraph]
edited Jun 1 at 2:59
answered Jun 1 at 2:54
David G. StorkDavid G. Stork
25.4k22256
25.4k22256
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%2f199492%2fhow-to-supress-loops-in-a-digraph%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
2
$begingroup$
Extract the AdjacencyMatrix and set the diagonal entries to $0$ then plot a new graph with the edited AdjacencyMatrix.
$endgroup$
– David G. Stork
Jun 1 at 2:46