-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
36 ford fulkerson algorithm for maximum flow problem (#199)
* Introduced Ford-Fulkerson Algorithm for Maximum Flow Problem * Introduced Tests for FordFulkerson Algorithm * Reformatted Files * Introduced Ford Fulkerson Algorithm on README
- Loading branch information
Showing
3 changed files
with
239 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,13 +83,14 @@ If you are interested, please contact us at [email protected] or contribute to | |
- [Floyd Warshall](#floyd-warshall) | ||
- [Kruskal Algorithm](#kruskal-algorithm) | ||
- [Borůvka's Algorithm](#borůvkas-algorithm) | ||
- [Graph Slicing based on connectivity](#graph-slicing-based-on-connectivity) | ||
- [Ford-Fulkerson Algorithm](#ford-fulkerson-algorithm) | ||
- [Partition Algorithm Explanation](#partition-algorithm-explanation) | ||
- [Vertex-Cut](#vertex-cut) | ||
- [Edge Balanced Vertex-Cut](#edge-balanced-vertex-cut) | ||
- [Greedy Vertex-Cut](#greedy-vertex-cut) | ||
- [HDRF](#hdrf) | ||
- [EBV](#ebv) | ||
- [Graph Slicing based on connectivity](#graph-slicing-based-on-connectivity) | ||
- [How to contribute](#how-to-contribute) | ||
- [Site](#site) | ||
- [Contact](#contact) | ||
|
@@ -424,6 +425,31 @@ The algorithm begins by finding the minimum-weight edge incident to each vertex | |
|
||
Borůvka's algorithm can be shown to take O(log V) iterations of the outer loop until it terminates, and therefore to run in time O(E log V), where E is the number of edges, and V is the number of vertices in G (assuming E ≥ V). | ||
|
||
### Graph Slicing based on connectivity | ||
|
||
Mathematical definition of the problem: | ||
Let G be the set of nodes in a graph and n be a given node in that set. | ||
Let C be the non-strict subset of G containing both n and all nodes reachable | ||
from n, and let C' be its complement. There's a third set M, which is the | ||
non-strict subset of C containing all nodes that are reachable from any node in C'. | ||
The problem consists of finding all nodes that belong to C but not to M. | ||
|
||
Currently implemented Algorithm: | ||
|
||
- Use DFS to find all nodes reachable from n. These are elements of set C. | ||
- Initialize C' to be complement of C (i.e. all nodes - nodes that are in C) | ||
- For all nodes in C', apply DFS and get the list of reachable nodes. This is set M. | ||
- Finally removes nodes from C that belong to M. This is our solution. | ||
|
||
Application: | ||
|
||
This algorithm is used in garbage collection systems to decide which other objects need to be released, given that one object is about to be released. | ||
|
||
### Ford-Fulkerson Algorithm | ||
|
||
[Ford-Fulkerson Algorithm](https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm) is a greedy algorithm for finding a maximum flow in a flow network. | ||
The idea behind the algorithm is as follows: as long as there is a path from the source (start node) to the sink (end node), with available capacity on all edges in the path, we send flow along one of the paths. Then we find another path, and so on. A path with available capacity is called an augmenting path. | ||
|
||
## Partition Algorithm Explanation | ||
|
||
### Vertex-Cut | ||
|
@@ -468,26 +494,6 @@ Eva(u,v)(i) =I(u ∈ keep[i]) + I(v ∈ keep[i]) +α * \frac{ecount[i]}{(|E|/p)} | |
|
||
The lowest value is taken as partition Id. | ||
|
||
### Graph Slicing based on connectivity | ||
|
||
Mathematical definition of the problem: | ||
Let G be the set of nodes in a graph and n be a given node in that set. | ||
Let C be the non-strict subset of G containing both n and all nodes reachable | ||
from n, and let C' be its complement. There's a third set M, which is the | ||
non-strict subset of C containing all nodes that are reachable from any node in C'. | ||
The problem consists of finding all nodes that belong to C but not to M. | ||
|
||
Currently implemented Algorithm: | ||
|
||
- Use DFS to find all nodes reachable from n. These are elements of set C. | ||
- Initialize C' to be complement of C (i.e. all nodes - nodes that are in C) | ||
- For all nodes in C', apply DFS and get the list of reachable nodes. This is set M. | ||
- Finally removes nodes from C that belong to M. This is our solution. | ||
|
||
Application: | ||
|
||
This algorithm is used in garbage collection systems to decide which other objects need to be released, given that one object is about to be released. | ||
|
||
## How to contribute | ||
|
||
[](https://GitHub.com/ZigRazor/CXXGraph/graphs/contributors/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include "gtest/gtest.h" | ||
#include "CXXGraph.hpp" | ||
|
||
TEST(FordFulkersonTest, test_1) | ||
{ | ||
CXXGRAPH::Node<int> node0("0", 0); | ||
CXXGRAPH::Node<int> node1("1", 1); | ||
CXXGRAPH::Node<int> node2("2", 2); | ||
CXXGRAPH::Node<int> node3("3", 3); | ||
CXXGRAPH::Node<int> node4("4", 4); | ||
CXXGRAPH::Node<int> node5("5", 5); | ||
|
||
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, node0, node1, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node0, node2, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node2, node1, 1); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node1, node3, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge5(5, node2, node4, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge6(6, node4, node3, 7); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge7(7, node4, node5, 4); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge8(8, node3, node5, 19); | ||
std::list<const CXXGRAPH::Edge<int> *> edgeSet; | ||
edgeSet.push_back(&edge1); | ||
edgeSet.push_back(&edge2); | ||
edgeSet.push_back(&edge3); | ||
edgeSet.push_back(&edge4); | ||
edgeSet.push_back(&edge5); | ||
edgeSet.push_back(&edge6); | ||
edgeSet.push_back(&edge7); | ||
edgeSet.push_back(&edge8); | ||
|
||
CXXGRAPH::Graph<int> graph(edgeSet); | ||
|
||
ASSERT_EQ(graph.fordFulkersonMaxFlow(node0, node5), 23); | ||
} | ||
|
||
TEST(FordFulkersonTest, test_2) | ||
{ | ||
CXXGRAPH::Node<int> node0("0", 0); | ||
CXXGRAPH::Node<int> node1("1", 1); | ||
CXXGRAPH::Node<int> node2("2", 2); | ||
CXXGRAPH::Node<int> node3("3", 3); | ||
CXXGRAPH::Node<int> node4("4", 4); | ||
CXXGRAPH::Node<int> node5("5", 5); | ||
|
||
CXXGRAPH::UndirectedWeightedEdge<int> edge1(1, node0, node1, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node0, node2, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node2, node1, 1); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node1, node3, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge5(5, node2, node4, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge6(6, node4, node3, 7); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge7(7, node4, node5, 4); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge8(8, node3, node5, 19); | ||
std::list<const CXXGRAPH::Edge<int> *> edgeSet; | ||
edgeSet.push_back(&edge1); | ||
edgeSet.push_back(&edge2); | ||
edgeSet.push_back(&edge3); | ||
edgeSet.push_back(&edge4); | ||
edgeSet.push_back(&edge5); | ||
edgeSet.push_back(&edge6); | ||
edgeSet.push_back(&edge7); | ||
edgeSet.push_back(&edge8); | ||
|
||
CXXGRAPH::Graph<int> graph(edgeSet); | ||
|
||
ASSERT_EQ(graph.fordFulkersonMaxFlow(node0, node5), -1); | ||
} | ||
|
||
TEST(FordFulkersonTest, test_3) | ||
{ | ||
CXXGRAPH::Node<int> node0("0", 0); | ||
CXXGRAPH::Node<int> node1("1", 1); | ||
CXXGRAPH::Node<int> node2("2", 2); | ||
CXXGRAPH::Node<int> node3("3", 3); | ||
CXXGRAPH::Node<int> node4("4", 4); | ||
CXXGRAPH::Node<int> node5("5", 5); | ||
|
||
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, node0, node1, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node0, node2, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node2, node1, 1); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node1, node3, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge5(5, node2, node4, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge6(6, node4, node3, 7); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge7(7, node4, node5, 4); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge8(8, node3, node5, 4); | ||
std::list<const CXXGRAPH::Edge<int> *> edgeSet; | ||
edgeSet.push_back(&edge1); | ||
edgeSet.push_back(&edge2); | ||
edgeSet.push_back(&edge3); | ||
edgeSet.push_back(&edge4); | ||
edgeSet.push_back(&edge5); | ||
edgeSet.push_back(&edge6); | ||
edgeSet.push_back(&edge7); | ||
edgeSet.push_back(&edge8); | ||
|
||
CXXGRAPH::Graph<int> graph(edgeSet); | ||
|
||
ASSERT_EQ(graph.fordFulkersonMaxFlow(node0, node5), 8); | ||
} | ||
|
||
TEST(FordFulkersonTest, test_4) | ||
{ | ||
CXXGRAPH::Node<int> node0("0", 0); | ||
CXXGRAPH::Node<int> node1("1", 1); | ||
CXXGRAPH::Node<int> node2("2", 2); | ||
CXXGRAPH::Node<int> node3("3", 3); | ||
CXXGRAPH::Node<int> node4("4", 4); | ||
CXXGRAPH::Node<int> node5("5", 5); | ||
|
||
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, node0, node1, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node0, node2, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node2, node1, 1); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node1, node3, 12); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge5(5, node2, node4, 11); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge6(6, node4, node3, 7); | ||
CXXGRAPH::DirectedEdge<int> edge7(7, node4, node5); | ||
CXXGRAPH::DirectedWeightedEdge<int> edge8(8, node3, node5, 4); | ||
std::list<const CXXGRAPH::Edge<int> *> edgeSet; | ||
edgeSet.push_back(&edge1); | ||
edgeSet.push_back(&edge2); | ||
edgeSet.push_back(&edge3); | ||
edgeSet.push_back(&edge4); | ||
edgeSet.push_back(&edge5); | ||
edgeSet.push_back(&edge6); | ||
edgeSet.push_back(&edge7); | ||
edgeSet.push_back(&edge8); | ||
|
||
CXXGRAPH::Graph<int> graph(edgeSet); | ||
|
||
ASSERT_EQ(graph.fordFulkersonMaxFlow(node0, node5), 4); | ||
} |