From 772efa7c9575e68d3a724b27ef3e6ba4dcd38604 Mon Sep 17 00:00:00 2001 From: p-hamann <p.hamann@dareit.de> Date: Mon, 6 May 2019 14:25:35 +0200 Subject: [PATCH] Adjust member identifiers and datatype of edge indices --- bnb/edmonds_karp.cpp | 60 ++++++++++++++++----------------- include/gp-bnb/edmonds_karp.hpp | 14 ++++---- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/bnb/edmonds_karp.cpp b/bnb/edmonds_karp.cpp index f0cf84c..90c307b 100644 --- a/bnb/edmonds_karp.cpp +++ b/bnb/edmonds_karp.cpp @@ -5,9 +5,9 @@ #include <gp-bnb/edmonds_karp.hpp> edmonds_karp::edmonds_karp(const graph &g, node_id source, node_id sink) - : g(g), source(source), sink(sink) { - assert(source > 0); // Nodes have ids: 1, .., n - assert(source <= g.num_nodes()); + : g_(g), source_(source), sink_(sink) { + assert(source_ > 0); // Nodes have ids: 1, .., n + assert(source_ <= g.num_nodes()); }; /* Indexes edges of the graph @@ -15,22 +15,22 @@ Every edge in the graph is mapped to its unique id indexed_edges std::map consists of key: pair<node_id, node_id> value: edge_id */ void edmonds_karp::index_edges() { - unsigned int num_nodes = g.num_nodes(); + unsigned int num_nodes = g_.num_nodes(); unsigned int eid = 0; for (node_id u = 1; u <= num_nodes; u++) { - std::vector<node_id> neighbors = g.get_adjacency(u); + std::vector<node_id> neighbors = g_.get_adjacency(u); for (node_id v : neighbors) { auto node_pair = std::make_pair(u, v); // edge was already listed - if (indexed_edges.count(std::make_pair(v, u)) == 1) { - indexed_edges[node_pair] = indexed_edges.at(std::make_pair(v, u)); + if (indexed_edges_.count(std::make_pair(v, u)) == 1) { + indexed_edges_[node_pair] = indexed_edges_.at(std::make_pair(v, u)); } else { - indexed_edges[node_pair] = eid; + indexed_edges_[node_pair] = eid; eid++; } } @@ -42,32 +42,32 @@ When the sink is reached gain is added to gain[sink] */ int edmonds_karp::bfs(std::vector<int> &resid_flow, std::vector<unsigned int> &pred) const { pred.clear(); - pred.resize(g.num_nodes() + 1, -1 ); // undiscovered nodes are marked with -1 - std::vector<int> gain(g.num_nodes(), 0.0); + pred.resize(g_.num_nodes() + 1, -1 ); // undiscovered nodes are marked with -1 + std::vector<int> gain(g_.num_nodes(), 0.0); std::queue<node_id> q; - q.push(source); - pred[source] = source; - gain[source] = std::numeric_limits<int>::max(); + q.push(source_); + pred[source_] = source_; + gain[source_] = std::numeric_limits<int>::max(); while (!q.empty()) { node_id u = q.front(); q.pop(); bool sink_reached = false; - std::vector<node_id> neighbors = g.get_adjacency(u); + std::vector<node_id> neighbors = g_.get_adjacency(u); // iterate through neighbors of u for (node_id v : neighbors) { - unsigned int edge_id = indexed_edges.at(std::make_pair(u, v)); + unsigned int edge_id = indexed_edges_.at(std::make_pair(u, v)); int edge_weight = 1; // unweighted graph - if (((u >= v && flow[edge_id] < edge_weight) + if (((u >= v && flow_[edge_id] < edge_weight) || ( u < v && resid_flow[edge_id] < edge_weight)) && pred[v] == (unsigned int) -1) { // only add those neighbors with rest capacity and which were not discovered yet pred[v] = u; - gain[v] = std::min(gain[u], edge_weight - (u >= v ? flow[edge_id] : resid_flow[edge_id])); - if (v != sink && !sink_reached) { + gain[v] = std::min(gain[u], edge_weight - (u >= v ? flow_[edge_id] : resid_flow[edge_id])); + if (v != sink_ && !sink_reached) { q.push(v); } else { sink_reached = true; @@ -76,7 +76,7 @@ int edmonds_karp::bfs(std::vector<int> &resid_flow, std::vector<unsigned int> &p } if (sink_reached) { - return gain[sink]; + return gain[sink_]; } } return 0.0; @@ -92,13 +92,13 @@ Step 3: Add gain that was calculated during BFS to the flow value. Break from th Step 4: Update flow and residual flow values for each node */ void edmonds_karp::run() { index_edges(); - int num_edges = indexed_edges.size() / 2; + int num_edges = indexed_edges_.size() / 2; - flow.clear(); - flow.resize(num_edges, 0.0); + flow_.clear(); + flow_.resize(num_edges, 0.0); std::vector<int> resid_flow(num_edges, 0.0); - flow_value = 0; + flow_value_ = 0; while (true) { std::vector<node_id> pred; @@ -109,18 +109,18 @@ void edmonds_karp::run() { if (gain == 0) break; // Add gain that was calculated during BFS to the flow value - flow_value += gain; - node_id v = sink; + flow_value_ += gain; + node_id v = sink_; // Update flow and residual flow values for each edge - while (v != source) { + while (v != source_) { node_id u = pred[v]; - int edge_id = indexed_edges[std::make_pair(u, v)]; + int edge_id = indexed_edges_[std::make_pair(u, v)]; if (u >= v) { - flow[edge_id] += gain; + flow_[edge_id] += gain; resid_flow[edge_id] -= gain; } else { - flow[edge_id] -= gain; + flow_[edge_id] -= gain; resid_flow[edge_id] += gain; } v = u; @@ -129,5 +129,5 @@ void edmonds_karp::run() { }; int edmonds_karp::get_max_flow() const { - return flow_value; + return flow_value_; }; diff --git a/include/gp-bnb/edmonds_karp.hpp b/include/gp-bnb/edmonds_karp.hpp index 2c1fac4..95616fa 100644 --- a/include/gp-bnb/edmonds_karp.hpp +++ b/include/gp-bnb/edmonds_karp.hpp @@ -7,15 +7,15 @@ class edmonds_karp { private: - const graph &g; + const graph &g_; - std::map<std::pair<node_id, node_id>, int> indexed_edges; + std::map<std::pair<node_id, node_id>, unsigned int> indexed_edges_; - node_id source; - node_id sink; + node_id source_; + node_id sink_; - int flow_value; - std::vector<int> flow; + int flow_value_; + std::vector<int> flow_; /** * Indexes edges of the graph @@ -53,4 +53,4 @@ public: int get_max_flow() const; }; -#endif /* EDMONDSKARP_H_ */ \ No newline at end of file +#endif /* EDMONDSKARP_H_ */ -- GitLab