diff --git a/alib2algo/src/graph/shortestpath/BellmanFord.cpp b/alib2algo/src/graph/shortestpath/BellmanFord.cpp index 97cbad51d249c5a7b5365ca8c90ebb642b3c285a..8f16e2312253bedae0353f69e6e4eb3056a97cdc 100644 --- a/alib2algo/src/graph/shortestpath/BellmanFord.cpp +++ b/alib2algo/src/graph/shortestpath/BellmanFord.cpp @@ -54,33 +54,15 @@ static BellmanFord::Result bellmanford_impl(const DirectedGraph &graph, const No return d; } -static int get_weight(weights_t &w, const UndirectedGraph &g, const UndirectedEdge &e) -{ - int out; - auto &map = w[e.getFirstNode()]; - auto search = map.find(e.getSecondNode()); - - if (search == map.end()) { - out = g.getEdgeValue(e); - map[e.getSecondNode()] = out; - w[e.getSecondNode()][e.getFirstNode()] = out; - } else { - out = search->second; - } - - return out; -} - static BellmanFord::Result bellmanford_impl(const UndirectedGraph &graph, const Node &start) { BellmanFord::Result d; // distances - weights_t w; // minimum weights d[start] = 0; for (unsigned i = 0; i < graph.getNodes().size(); ++i) { for (const UndirectedEdge &e : graph.getEdges()) { - int ew = get_weight(w, graph, e); + int ew = graph.getEdgeValue(e); const Node &u = e.getFirstNode(); const Node &v = e.getSecondNode(); @@ -106,8 +88,8 @@ static BellmanFord::Result bellmanford_impl(const UndirectedGraph &graph, const const Node &u = e.getFirstNode(); const Node &v = e.getSecondNode(); - auto f = [&w, &d](const Node &u, const Node &v) { - if (d.at(u) + w[u][v] < d.at(v)) { + auto f = [&e, &d, &graph](const Node &u, const Node &v) { + if (d.at(u) + graph.getEdgeValue(e) < d.at(v)) { throw exception::AlibException("BellmanFord: Found negative-weight cycle!"); } };