Skip to content
Snippets Groups Projects
Commit 36a4904b authored by Lukas Garbas's avatar Lukas Garbas Committed by p-hamann
Browse files

Push-relabel adjacency as reference

parent f9af5a80
Branches
1 merge request!7Incremental bfs
......@@ -5,6 +5,9 @@
push_relabel::push_relabel(const graph &g, std::vector<node_id> sources, std::vector<node_id> sinks)
: g_(g), sources_(sources), sinks_(sinks) {
// Initial flow value
flow_value_ = 0;
int num_nodes = g_.num_nodes();
// Initialize all vectors
......@@ -41,7 +44,7 @@ void push_relabel::relabel(node_id u) {
int d = std::numeric_limits<int>::max();
std::vector<node_id> neighbors = g_.get_adjacency(u);
const std::vector<node_id> & neighbors = g_.get_adjacency(u);
for (auto v : neighbors) {
if (1 - flow_[u][v] > 0)
......@@ -58,7 +61,7 @@ void push_relabel::discharge(node_id u) {
while (excess_[u] > 0) {
std::vector<node_id> neighbors = g_.get_adjacency(u);
const std::vector<node_id> & neighbors = g_.get_adjacency(u);
bool push_possible = false;
for (auto v : neighbors) {
......@@ -87,7 +90,7 @@ void push_relabel::preflow() {
}
for (node_id s : sources_) {
std::vector<node_id> neighbors = g_.get_adjacency(s);
const std::vector<node_id> & neighbors = g_.get_adjacency(s);
for (auto n : neighbors) {
if (sources_and_sinks_[n] != 1) // if n is not a source node
push(s, n);
......@@ -109,7 +112,6 @@ void push_relabel::run() {
}
// Flow value is the sum of excesses in sinks
flow_value_ = 0;
for (node_id t : sinks_)
flow_value_ += excess_[t];
......
......@@ -30,22 +30,21 @@ private:
public:
/**
* Constructs an instance of the EdmondsKarp algorithm for the given graph, source and sink
* Constructs an instance of the Push-Realbel algorithm for the given graph, sources and sinks
* @param graph The graph.
* @param source The source node.
* @param sink The sink node.
* @param sources Vector of source nodes.
* @param sinks Vector of sink nodes.
*/
push_relabel(const graph &g, std::vector<node_id> sources, std::vector<node_id> sinks);
/**
* Computes the maximum flow, executes the EdmondsKarp algorithm.
* Computes the maximum flow, executes the Peush-Relabel algorithm with discharge.
* For unweighted, undirected Graphs.
*/
void run();
/**
* Returns the value of the maximum flow from source to sink.
*
* Returns the value of the maximum flow from sources to sinks.
* @return The maximum flow value
*/
int get_max_flow() const;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment