Skip to content

addEdge() method does not copy the weight  #350

Closed
@fajlip

Description

@fajlip

Describe the bug
Graph.addEdge() method calls a wrong constructor; thus if you give it undirectedWeightedEdge, it does not copy its weight; leading to unpredictable algorithms results. I imagine similar problem is with directedWeighted edge, but I have not tried it.

To Reproduce
CXXGraph::Node a("a",0);
CXXGraph::Node b("b",0);
CXXGraph::UndirectedWeightedEdge edge(0, a, b, 3.3);
graph.addEdge(&edge);

Expected behavior
I expect that the edge gets added to the graph with the weight equal 3.3

Additional context
Temporary fix:

template <typename T>
void Graph<T>::addEdge(const Edge<T> *edge) {
  if (edge->isDirected().has_value() && edge->isDirected().value()) {
    if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
      auto edge_shared = make_shared<DirectedWeightedEdge<T>>(*edge);
      this->edgeSet.insert(edge_shared);
    } else {
      auto edge_shared = make_shared<DirectedEdge<T>>(*edge);
      this->edgeSet.insert(edge_shared);
    }
  } else {
    if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
      auto edge_shared = make_shared<UndirectedWeightedEdge<T>>(*edge,
          dynamic_cast<const UndirectedWeightedEdge<T>&>(*edge).getWeight());
      this->edgeSet.insert(edge_shared);
    } else {
      auto edge_shared = make_shared<UndirectedEdge<T>>(*edge);
      this->edgeSet.insert(edge_shared);
    }
  }
}

Metadata

Metadata

Assignees

Labels

Priority:CriticalPriority Label for Critical IssuebugSomething isn't workingcoresomething about core

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions