Finding MST with a given path (C++)










0















I have a code for finding MST which would include a given path to MST, which gives me correct weight of MST (I know what the output is supposed to be). I tried to rewrite one part of the code to use std::map instead of std::vector for more efficient look-up (will become clear from the code below), but now it gives wrong answer, and despite looking into it for few hours, I am unable to understand where did I make a mistake. Any help with locating a bug will be appreciated.



Old version that uses vector<Edge>. Goes through the vector of all edges (m_all_edges). If an edge e is in vector<Edge> path, adds it to MST (m_mst). Otherwise, adds it to candidate edges for MST (m_edges).



void MSTPath::m_add_shortest_path_old(vector<Edge> path)
for (auto e : path)
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto e : m_all_edges)
if (tempfunc(e,path)) m_mst.push_back(e);
else m_edges.push_back(e);



bool MSTPath::tempfunc(Edge e, vector<Edge> path)
for (auto edge : path)
if((edge.get_node()==e.get_node())&&(edge.get_opposite_node(-1)==e.get_opposite_node(-1))) return true;

return false;



Version with std::map.



void MSTPath::m_add_shortest_path(map<Edge,int> path_map)
for (auto const& x : path_map)
Edge e = x.first;
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto const& x : m_all_edges_map)(path_map.count(x.first.reversed())>0)) m_mst.push_back(x.first);
else m_edges.push_back(x.first);




Function for finding MST (called after calling m_add_shortest_path / m_add_shortest_path_old).



int MSTPath::m_compute_mst()
sort(m_edges.begin(),m_edges.end(),(Edge e1, Edge e2) return e1.weight < e2.weight; );
int i = 0;
int mst_size = m_mst.size();

int mst_weight = 0;
int v,u;
for (Edge e : m_mst) mst_weight += e.weight;

while (mst_size<m_N-1)
Edge edge = m_edges[i++];
v = m_graph->find(edge.get_node());
u = m_graph->find(edge.get_opposite_node(-1));
if (u!=v)
mst_size++;
mst_weight += edge.weight;
if (mst_weight>=min_mst_weight) return numeric_limits<int>::max();
m_mst.push_back(edge);
m_graph->union_vu(v,u);


return mst_weight;



From what I see, both function get exactly the same m_mst (and starting mst_weight) and the same m_edges (but in different order). But ordering should not be a problem for the algorithm (except for ordering by weight, but this is handles in m_compute_mst), right?



Edge class:



class Edge
public:
Edge(int v_e, int u_e, int w)
v = v_e;
u = u_e;
weight = w;


int get_node() const return v;
int get_opposite_node(int k) const

Edge reversed() const
return Edge(u,v,weight);


bool operator<(const Edge& e) const
if (this->get_node()<e.get_node()) return true;
else if (this->get_node()==e.get_node()) return this->get_opposite_node(-1)<e.get_opposite_node(-1);
else return false;


int v, u, weight;
;


Yes, I know that passing by value makes additional copies. This is a temporary version.










share|improve this question






















  • map<Edge,int>, what are you mapping Edges to ?

    – Piotr Skotnicki
    Nov 15 '18 at 18:46











  • To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

    – Valeria
    Nov 15 '18 at 21:11















0















I have a code for finding MST which would include a given path to MST, which gives me correct weight of MST (I know what the output is supposed to be). I tried to rewrite one part of the code to use std::map instead of std::vector for more efficient look-up (will become clear from the code below), but now it gives wrong answer, and despite looking into it for few hours, I am unable to understand where did I make a mistake. Any help with locating a bug will be appreciated.



Old version that uses vector<Edge>. Goes through the vector of all edges (m_all_edges). If an edge e is in vector<Edge> path, adds it to MST (m_mst). Otherwise, adds it to candidate edges for MST (m_edges).



void MSTPath::m_add_shortest_path_old(vector<Edge> path)
for (auto e : path)
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto e : m_all_edges)
if (tempfunc(e,path)) m_mst.push_back(e);
else m_edges.push_back(e);



bool MSTPath::tempfunc(Edge e, vector<Edge> path)
for (auto edge : path)
if((edge.get_node()==e.get_node())&&(edge.get_opposite_node(-1)==e.get_opposite_node(-1))) return true;

return false;



Version with std::map.



void MSTPath::m_add_shortest_path(map<Edge,int> path_map)
for (auto const& x : path_map)
Edge e = x.first;
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto const& x : m_all_edges_map)(path_map.count(x.first.reversed())>0)) m_mst.push_back(x.first);
else m_edges.push_back(x.first);




Function for finding MST (called after calling m_add_shortest_path / m_add_shortest_path_old).



int MSTPath::m_compute_mst()
sort(m_edges.begin(),m_edges.end(),(Edge e1, Edge e2) return e1.weight < e2.weight; );
int i = 0;
int mst_size = m_mst.size();

int mst_weight = 0;
int v,u;
for (Edge e : m_mst) mst_weight += e.weight;

while (mst_size<m_N-1)
Edge edge = m_edges[i++];
v = m_graph->find(edge.get_node());
u = m_graph->find(edge.get_opposite_node(-1));
if (u!=v)
mst_size++;
mst_weight += edge.weight;
if (mst_weight>=min_mst_weight) return numeric_limits<int>::max();
m_mst.push_back(edge);
m_graph->union_vu(v,u);


return mst_weight;



From what I see, both function get exactly the same m_mst (and starting mst_weight) and the same m_edges (but in different order). But ordering should not be a problem for the algorithm (except for ordering by weight, but this is handles in m_compute_mst), right?



Edge class:



class Edge
public:
Edge(int v_e, int u_e, int w)
v = v_e;
u = u_e;
weight = w;


int get_node() const return v;
int get_opposite_node(int k) const

Edge reversed() const
return Edge(u,v,weight);


bool operator<(const Edge& e) const
if (this->get_node()<e.get_node()) return true;
else if (this->get_node()==e.get_node()) return this->get_opposite_node(-1)<e.get_opposite_node(-1);
else return false;


int v, u, weight;
;


Yes, I know that passing by value makes additional copies. This is a temporary version.










share|improve this question






















  • map<Edge,int>, what are you mapping Edges to ?

    – Piotr Skotnicki
    Nov 15 '18 at 18:46











  • To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

    – Valeria
    Nov 15 '18 at 21:11













0












0








0








I have a code for finding MST which would include a given path to MST, which gives me correct weight of MST (I know what the output is supposed to be). I tried to rewrite one part of the code to use std::map instead of std::vector for more efficient look-up (will become clear from the code below), but now it gives wrong answer, and despite looking into it for few hours, I am unable to understand where did I make a mistake. Any help with locating a bug will be appreciated.



Old version that uses vector<Edge>. Goes through the vector of all edges (m_all_edges). If an edge e is in vector<Edge> path, adds it to MST (m_mst). Otherwise, adds it to candidate edges for MST (m_edges).



void MSTPath::m_add_shortest_path_old(vector<Edge> path)
for (auto e : path)
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto e : m_all_edges)
if (tempfunc(e,path)) m_mst.push_back(e);
else m_edges.push_back(e);



bool MSTPath::tempfunc(Edge e, vector<Edge> path)
for (auto edge : path)
if((edge.get_node()==e.get_node())&&(edge.get_opposite_node(-1)==e.get_opposite_node(-1))) return true;

return false;



Version with std::map.



void MSTPath::m_add_shortest_path(map<Edge,int> path_map)
for (auto const& x : path_map)
Edge e = x.first;
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto const& x : m_all_edges_map)(path_map.count(x.first.reversed())>0)) m_mst.push_back(x.first);
else m_edges.push_back(x.first);




Function for finding MST (called after calling m_add_shortest_path / m_add_shortest_path_old).



int MSTPath::m_compute_mst()
sort(m_edges.begin(),m_edges.end(),(Edge e1, Edge e2) return e1.weight < e2.weight; );
int i = 0;
int mst_size = m_mst.size();

int mst_weight = 0;
int v,u;
for (Edge e : m_mst) mst_weight += e.weight;

while (mst_size<m_N-1)
Edge edge = m_edges[i++];
v = m_graph->find(edge.get_node());
u = m_graph->find(edge.get_opposite_node(-1));
if (u!=v)
mst_size++;
mst_weight += edge.weight;
if (mst_weight>=min_mst_weight) return numeric_limits<int>::max();
m_mst.push_back(edge);
m_graph->union_vu(v,u);


return mst_weight;



From what I see, both function get exactly the same m_mst (and starting mst_weight) and the same m_edges (but in different order). But ordering should not be a problem for the algorithm (except for ordering by weight, but this is handles in m_compute_mst), right?



Edge class:



class Edge
public:
Edge(int v_e, int u_e, int w)
v = v_e;
u = u_e;
weight = w;


int get_node() const return v;
int get_opposite_node(int k) const

Edge reversed() const
return Edge(u,v,weight);


bool operator<(const Edge& e) const
if (this->get_node()<e.get_node()) return true;
else if (this->get_node()==e.get_node()) return this->get_opposite_node(-1)<e.get_opposite_node(-1);
else return false;


int v, u, weight;
;


Yes, I know that passing by value makes additional copies. This is a temporary version.










share|improve this question














I have a code for finding MST which would include a given path to MST, which gives me correct weight of MST (I know what the output is supposed to be). I tried to rewrite one part of the code to use std::map instead of std::vector for more efficient look-up (will become clear from the code below), but now it gives wrong answer, and despite looking into it for few hours, I am unable to understand where did I make a mistake. Any help with locating a bug will be appreciated.



Old version that uses vector<Edge>. Goes through the vector of all edges (m_all_edges). If an edge e is in vector<Edge> path, adds it to MST (m_mst). Otherwise, adds it to candidate edges for MST (m_edges).



void MSTPath::m_add_shortest_path_old(vector<Edge> path)
for (auto e : path)
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto e : m_all_edges)
if (tempfunc(e,path)) m_mst.push_back(e);
else m_edges.push_back(e);



bool MSTPath::tempfunc(Edge e, vector<Edge> path)
for (auto edge : path)
if((edge.get_node()==e.get_node())&&(edge.get_opposite_node(-1)==e.get_opposite_node(-1))) return true;

return false;



Version with std::map.



void MSTPath::m_add_shortest_path(map<Edge,int> path_map)
for (auto const& x : path_map)
Edge e = x.first;
m_graph->union_vu(e.get_node(),e.get_opposite_node(-1));

for (auto const& x : m_all_edges_map)(path_map.count(x.first.reversed())>0)) m_mst.push_back(x.first);
else m_edges.push_back(x.first);




Function for finding MST (called after calling m_add_shortest_path / m_add_shortest_path_old).



int MSTPath::m_compute_mst()
sort(m_edges.begin(),m_edges.end(),(Edge e1, Edge e2) return e1.weight < e2.weight; );
int i = 0;
int mst_size = m_mst.size();

int mst_weight = 0;
int v,u;
for (Edge e : m_mst) mst_weight += e.weight;

while (mst_size<m_N-1)
Edge edge = m_edges[i++];
v = m_graph->find(edge.get_node());
u = m_graph->find(edge.get_opposite_node(-1));
if (u!=v)
mst_size++;
mst_weight += edge.weight;
if (mst_weight>=min_mst_weight) return numeric_limits<int>::max();
m_mst.push_back(edge);
m_graph->union_vu(v,u);


return mst_weight;



From what I see, both function get exactly the same m_mst (and starting mst_weight) and the same m_edges (but in different order). But ordering should not be a problem for the algorithm (except for ordering by weight, but this is handles in m_compute_mst), right?



Edge class:



class Edge
public:
Edge(int v_e, int u_e, int w)
v = v_e;
u = u_e;
weight = w;


int get_node() const return v;
int get_opposite_node(int k) const

Edge reversed() const
return Edge(u,v,weight);


bool operator<(const Edge& e) const
if (this->get_node()<e.get_node()) return true;
else if (this->get_node()==e.get_node()) return this->get_opposite_node(-1)<e.get_opposite_node(-1);
else return false;


int v, u, weight;
;


Yes, I know that passing by value makes additional copies. This is a temporary version.







c++ graph-theory minimum-spanning-tree






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 18:24









ValeriaValeria

10911




10911












  • map<Edge,int>, what are you mapping Edges to ?

    – Piotr Skotnicki
    Nov 15 '18 at 18:46











  • To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

    – Valeria
    Nov 15 '18 at 21:11

















  • map<Edge,int>, what are you mapping Edges to ?

    – Piotr Skotnicki
    Nov 15 '18 at 18:46











  • To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

    – Valeria
    Nov 15 '18 at 21:11
















map<Edge,int>, what are you mapping Edges to ?

– Piotr Skotnicki
Nov 15 '18 at 18:46





map<Edge,int>, what are you mapping Edges to ?

– Piotr Skotnicki
Nov 15 '18 at 18:46













To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

– Valeria
Nov 15 '18 at 21:11





To nothing (for now, at least; just to 0). I just use map to check if map contains the given key (Edge) in O(logN).

– Valeria
Nov 15 '18 at 21:11












0






active

oldest

votes











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%2f53325726%2ffinding-mst-with-a-given-path-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53325726%2ffinding-mst-with-a-given-path-c%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto