Necessary to MST edges in a graph












0















Given a G(V,E) weighted(on edges) graph i need to find the number of edges that belong in every MST as well as the number of the edges that belong in at least one but not all and the ones that belong in none.



The graph is given as input in the following form(example):



3 3



1 2 1



1 3 1



2 3 2



First 3 is the number of nodes,second 3 is the number of edges.The following three lines are the edges where first number is where they start,second is where they end and third is the value.



I've thought about running kruskal once to find an MST and then for each edge belonging in G check in(linear?) time if it can be replace an edge in this MST without altering it's overall weight.If it can't it belongs in none.If it can it belongs in one but not all.I can also mark the edges in the first MST(maybe with a second value 1 or 0)and in the end check how many of them could not be replaced.These are the ones belonging in every possible MST. This algorithm is probably O(V^2) and i am not quite sure how to write it in C++.My questions are,could we somehow reduce it's complexity? If i add an edge to the MST how can i check(and implement in C++) if the cycle formed contains an edge that has less weight?










share|improve this question























  • math.stackexchange.com/questions/2577365/…

    – juvian
    Jan 3 at 17:04
















0















Given a G(V,E) weighted(on edges) graph i need to find the number of edges that belong in every MST as well as the number of the edges that belong in at least one but not all and the ones that belong in none.



The graph is given as input in the following form(example):



3 3



1 2 1



1 3 1



2 3 2



First 3 is the number of nodes,second 3 is the number of edges.The following three lines are the edges where first number is where they start,second is where they end and third is the value.



I've thought about running kruskal once to find an MST and then for each edge belonging in G check in(linear?) time if it can be replace an edge in this MST without altering it's overall weight.If it can't it belongs in none.If it can it belongs in one but not all.I can also mark the edges in the first MST(maybe with a second value 1 or 0)and in the end check how many of them could not be replaced.These are the ones belonging in every possible MST. This algorithm is probably O(V^2) and i am not quite sure how to write it in C++.My questions are,could we somehow reduce it's complexity? If i add an edge to the MST how can i check(and implement in C++) if the cycle formed contains an edge that has less weight?










share|improve this question























  • math.stackexchange.com/questions/2577365/…

    – juvian
    Jan 3 at 17:04














0












0








0


1






Given a G(V,E) weighted(on edges) graph i need to find the number of edges that belong in every MST as well as the number of the edges that belong in at least one but not all and the ones that belong in none.



The graph is given as input in the following form(example):



3 3



1 2 1



1 3 1



2 3 2



First 3 is the number of nodes,second 3 is the number of edges.The following three lines are the edges where first number is where they start,second is where they end and third is the value.



I've thought about running kruskal once to find an MST and then for each edge belonging in G check in(linear?) time if it can be replace an edge in this MST without altering it's overall weight.If it can't it belongs in none.If it can it belongs in one but not all.I can also mark the edges in the first MST(maybe with a second value 1 or 0)and in the end check how many of them could not be replaced.These are the ones belonging in every possible MST. This algorithm is probably O(V^2) and i am not quite sure how to write it in C++.My questions are,could we somehow reduce it's complexity? If i add an edge to the MST how can i check(and implement in C++) if the cycle formed contains an edge that has less weight?










share|improve this question














Given a G(V,E) weighted(on edges) graph i need to find the number of edges that belong in every MST as well as the number of the edges that belong in at least one but not all and the ones that belong in none.



The graph is given as input in the following form(example):



3 3



1 2 1



1 3 1



2 3 2



First 3 is the number of nodes,second 3 is the number of edges.The following three lines are the edges where first number is where they start,second is where they end and third is the value.



I've thought about running kruskal once to find an MST and then for each edge belonging in G check in(linear?) time if it can be replace an edge in this MST without altering it's overall weight.If it can't it belongs in none.If it can it belongs in one but not all.I can also mark the edges in the first MST(maybe with a second value 1 or 0)and in the end check how many of them could not be replaced.These are the ones belonging in every possible MST. This algorithm is probably O(V^2) and i am not quite sure how to write it in C++.My questions are,could we somehow reduce it's complexity? If i add an edge to the MST how can i check(and implement in C++) if the cycle formed contains an edge that has less weight?







algorithm graph






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 16:52







user10639836




















  • math.stackexchange.com/questions/2577365/…

    – juvian
    Jan 3 at 17:04



















  • math.stackexchange.com/questions/2577365/…

    – juvian
    Jan 3 at 17:04

















math.stackexchange.com/questions/2577365/…

– juvian
Jan 3 at 17:04





math.stackexchange.com/questions/2577365/…

– juvian
Jan 3 at 17:04












1 Answer
1






active

oldest

votes


















0














You can do this by adding some extra work to Kruskal's algorithm.



In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.



No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.



If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.



If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.



All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)



So, modify Kruskal's algorithm as follows:




  • When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.

  • Make a small graph with those edges using the find() sets they connect as vertices

  • Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.

  • The remaining edges in the small graph are in some, but not all, MSTs.

  • perform all the unions for edges in the small graph and move on to the next weight.


Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).






share|improve this answer


























  • I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

    – user10639836
    Jan 8 at 23:33














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54026545%2fnecessary-to-mst-edges-in-a-graph%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can do this by adding some extra work to Kruskal's algorithm.



In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.



No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.



If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.



If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.



All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)



So, modify Kruskal's algorithm as follows:




  • When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.

  • Make a small graph with those edges using the find() sets they connect as vertices

  • Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.

  • The remaining edges in the small graph are in some, but not all, MSTs.

  • perform all the unions for edges in the small graph and move on to the next weight.


Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).






share|improve this answer


























  • I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

    – user10639836
    Jan 8 at 23:33


















0














You can do this by adding some extra work to Kruskal's algorithm.



In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.



No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.



If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.



If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.



All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)



So, modify Kruskal's algorithm as follows:




  • When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.

  • Make a small graph with those edges using the find() sets they connect as vertices

  • Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.

  • The remaining edges in the small graph are in some, but not all, MSTs.

  • perform all the unions for edges in the small graph and move on to the next weight.


Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).






share|improve this answer


























  • I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

    – user10639836
    Jan 8 at 23:33
















0












0








0







You can do this by adding some extra work to Kruskal's algorithm.



In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.



No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.



If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.



If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.



All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)



So, modify Kruskal's algorithm as follows:




  • When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.

  • Make a small graph with those edges using the find() sets they connect as vertices

  • Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.

  • The remaining edges in the small graph are in some, but not all, MSTs.

  • perform all the unions for edges in the small graph and move on to the next weight.


Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).






share|improve this answer















You can do this by adding some extra work to Kruskal's algorithm.



In Kruskal's algorithm, edges with the same weight can be in any order, and the order in which they are actually checked determines which MST you get out of all possible MSTs. For every MST, there is a sort-consistent order of the edges that will produce that tree.



No matter what sort-consistent order is used, the state of the union-find structure will be the same between weights.



If there is only one edge of a specific weight, then it is in every MST if Kruskal's algorithm selects it, because it would be selected with any sort-consistent edge order, and otherwise it is in no MSTs.



If there are multiple edges with the same weight, and Kruskal's algorithm would select at least one of them, then you can pause Kruskal's at that point and make a new (small) graph with just those edges that connect different sets, using the sets that they connect as vertices.



All of those edges are in at least one MST, because Kruskal's might pick them first. Any of those edges that are bridges in the new graph are in every MST, because Kruskal's would select them no matter what. See: https://en.wikipedia.org/wiki/Bridge_(graph_theory)



So, modify Kruskal's algorithm as follows:




  • When Kruskal's would select an edge, before doing the union, gather and process ALL the non-redundant edges with the same weight.

  • Make a small graph with those edges using the find() sets they connect as vertices

  • Use Tarjan's algorithm or equivalent (see the link) to find all the bridges in the graph. Those edges are in ALL MSTs.

  • The remaining edges in the small graph are in some, but not all, MSTs.

  • perform all the unions for edges in the small graph and move on to the next weight.


Since bridge-finding can be done in linear time, and every edge is in at most one small graph, the whole algorithm is still O(|V| + |E| log |E|).







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 6 at 20:42

























answered Jan 6 at 20:34









Matt TimmermansMatt Timmermans

21k11733




21k11733













  • I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

    – user10639836
    Jan 8 at 23:33





















  • I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

    – user10639836
    Jan 8 at 23:33



















I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

– user10639836
Jan 8 at 23:33







I thought about modifying kruskal's algorithm but in a different way.Each time using find() if the edge being processed forms a cycle check if there is an other edge with the same weight in the cycle formed.If there is both of these are used in one mst but not all.If an edge processed does not form a cycle add it to mst and mark it as the one contained in every mst(this may change later though).

– user10639836
Jan 8 at 23:33






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026545%2fnecessary-to-mst-edges-in-a-graph%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas