Skip to content

Commit

Permalink
Unit test and docs update on Ford Fulkerson Max Flow Min Cut algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
chen0040 committed Apr 30, 2017
1 parent 46e1552 commit 98f158e
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 11 deletions.
44 changes: 42 additions & 2 deletions README.rst
Expand Up @@ -119,6 +119,10 @@ Features:
+ Topological Sort (for directed acyclic graph, namely dag)
+ Bellman-Ford (for graph with negative weight as well)

- MaxFlow MinCut

+ Ford-Fulkerson

* Strings

- String Sorting
Expand Down Expand Up @@ -389,7 +393,7 @@ Edge Weighted Graph

.. code-block:: python
from pyalgs.data_structures.graphs.graph import EdgeWeightGraph
from pyalgs.data_structures.graphs.graph import EdgeWeightGraph, Edge
def create_edge_weighted_graph():
g = EdgeWeightedGraph(8)
g.add_edge(Edge(0, 7, 0.16))
Expand All @@ -409,7 +413,7 @@ Directed Edge Weighted Graph

.. code-block:: python
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph, Edge
def create_edge_weighted_digraph():
g = DirectedEdgeWeightedGraph(8)
Expand All @@ -420,6 +424,30 @@ Directed Edge Weighted Graph
return g
Flow Network ( for max-flow min-cut problem)

.. code-block:: python
from pyalgs.data_structures.graphs.graph import FlowNetwork, FlowEdge
def create_flow_network():
g = FlowNetwork(8)
g.add_edge(FlowEdge(0, 1, 10))
g.add_edge(FlowEdge(0, 2, 5))
g.add_edge(FlowEdge(0, 3, 15))
g.add_edge(FlowEdge(1, 4, 9))
g.add_edge(FlowEdge(1, 5, 15))
g.add_edge(FlowEdge(1, 2, 4))
g.add_edge(FlowEdge(2, 5, 8))
g.add_edge(FlowEdge(2, 3, 4))
g.add_edge(FlowEdge(3, 6, 16))
g.add_edge(FlowEdge(4, 5, 15))
g.add_edge(FlowEdge(4, 7, 10))
g.add_edge(FlowEdge(5, 7, 10))
g.add_edge(FlowEdge(5, 6, 15))
g.add_edge(FlowEdge(6, 2, 6))
g.add_edge(FlowEdge(6, 7, 10))
return g
Algorithms
----------

Expand Down Expand Up @@ -734,6 +762,16 @@ Shortest Path (Bellman-Ford for positive and negative edge graph)
print('path length is ' + str(dijkstra.path_length_to(v)))
MaxFlow MinCut (Ford-Fulkerson)

.. code-block:: python
from pyalgs.algorithms.graphs.max_flow import FordFulkersonMaxFlow
network = create_flow_network()
ff = FordFulkersonMaxFlow(network, 0, 7)
print('max-flow: '+str(ff.max_flow_value()))
Strings
-------

Expand All @@ -753,5 +791,7 @@ Substring Search (Brute force)
ss = BruteForceSubstringSearch('find')
print(ss.search_in('I can find it here'))
print(ss.search_in('It is not here'))
.. _`docs`: http://pyalgs.readthedocs.org/en/latest/
.. _`documentation`: http://pyalgs.readthedocs.org/en/latest/
3 changes: 2 additions & 1 deletion docs/algorithms-graphs.rst
Expand Up @@ -11,4 +11,5 @@ Graphs
graphs-topological-sort
graphs-cyclic
graphs-mst
graphs-shortest-path
graphs-shortest-path
graphs-max-flow
31 changes: 29 additions & 2 deletions docs/graphs-create.rst
Expand Up @@ -42,7 +42,7 @@ Edge Weighted Graph

.. code-block:: python
from pyalgs.data_structures.graphs.graph import EdgeWeightGraph
from pyalgs.data_structures.graphs.graph import EdgeWeightGraph, Edge
def create_edge_weighted_graph():
g = EdgeWeightedGraph(8)
g.add_edge(Edge(0, 7, 0.16))
Expand All @@ -62,7 +62,7 @@ Directed Edge Weighted Graph

.. code-block:: python
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph, Edge
def create_edge_weighted_digraph():
g = DirectedEdgeWeightedGraph(8)
Expand All @@ -75,3 +75,30 @@ Directed Edge Weighted Graph
g.add_edge(
Edge(1, 2, 12.0))
return g
Flow Network ( for max-flow min-cut problem)
--------------------------------------------

.. code-block:: python
from pyalgs.data_structures.graphs.graph import FlowNetwork, FlowEdge
def create_flow_network():
g = FlowNetwork(8)
g.add_edge(FlowEdge(0, 1, 10))
g.add_edge(FlowEdge(0, 2, 5))
g.add_edge(FlowEdge(0, 3, 15))
g.add_edge(FlowEdge(1, 4, 9))
g.add_edge(FlowEdge(1, 5, 15))
g.add_edge(FlowEdge(1, 2, 4))
g.add_edge(FlowEdge(2, 5, 8))
g.add_edge(FlowEdge(2, 3, 4))
g.add_edge(FlowEdge(3, 6, 16))
g.add_edge(FlowEdge(4, 5, 15))
g.add_edge(FlowEdge(4, 7, 10))
g.add_edge(FlowEdge(5, 7, 10))
g.add_edge(FlowEdge(5, 6, 15))
g.add_edge(FlowEdge(6, 2, 6))
g.add_edge(FlowEdge(6, 7, 10))
return g
13 changes: 13 additions & 0 deletions docs/graphs-max-flow.rst
@@ -0,0 +1,13 @@
Max-Flow-Min-Cut
================

MaxFlow MinCut (Ford-Fulkerson)
-------------------------------

.. code-block:: python
from pyalgs.algorithms.graphs.max_flow import FordFulkersonMaxFlow
network = create_flow_network()
ff = FordFulkersonMaxFlow(network, 0, 7)
print('max-flow: '+str(ff.max_flow_value()))
2 changes: 1 addition & 1 deletion pyalgs/__init__.py
Expand Up @@ -7,4 +7,4 @@
:license: BSD, see LICENSE for more details.
"""

__version__ = '0.0.5'
__version__ = '0.0.6'
2 changes: 1 addition & 1 deletion pyalgs/algorithms/graphs/max_flow.py
Expand Up @@ -42,7 +42,7 @@ def has_augmenting_path(self):
x = queue.dequeue()
for e in self.network.adj(x):
w = e.other(x)
if e.residual_capacity(w) > 0:
if e.residual_capacity_to(w) > 0:
if not self.marked[w]:
self.marked[w] = True
self.edgeTo[w] = e
Expand Down
6 changes: 3 additions & 3 deletions pyalgs/data_structures/graphs/graph.py
Expand Up @@ -240,8 +240,8 @@ def adj(self, v):
return self.adjList[v].iterate()

def add_edge(self, edge):
self.adjList[edge.start()] = edge
self.adjList[edge.end()] = edge
self.adjList[edge.start()].add(edge)
self.adjList[edge.end()].add(edge)

def vertex_count(self):
return self.V
Expand All @@ -250,6 +250,6 @@ def edges(self):
for v in range(self.V):
for e in self.adjList[v].iterate():
if e.start() == v:
yield e
yield e


15 changes: 15 additions & 0 deletions tests/algorithms/graphs/max_flow_unit_test.py
@@ -0,0 +1,15 @@
import unittest

from pyalgs.algorithms.graphs.max_flow import FordFulkersonMaxFlow
from tests.algorithms.graphs.util import create_flow_network


class FordFulkersonMaxFlowUnitTest(unittest.TestCase):
def test_max_flow(self):
network = create_flow_network()
ff = FordFulkersonMaxFlow(network, 0, 7)
print('max-flow: '+str(ff.max_flow_value()))
self.assertEqual(28, ff.max_flow_value())

if __name__ == '__main__':
unittest.main()
23 changes: 22 additions & 1 deletion tests/algorithms/graphs/util.py
@@ -1,5 +1,5 @@
from pyalgs.data_structures.graphs.graph import Graph, Digraph, EdgeWeightedGraph, Edge, EdgeWeightedGraph, \
DirectedEdgeWeightedGraph
DirectedEdgeWeightedGraph, FlowNetwork, FlowEdge


def create_graph():
Expand Down Expand Up @@ -167,3 +167,24 @@ def create_digraph_4_strongly_connected_components():
graph.add_edge(7, 6)

return graph


def create_flow_network():
g = FlowNetwork(8)
g.add_edge(FlowEdge(0, 1, 10))
g.add_edge(FlowEdge(0, 2, 5))
g.add_edge(FlowEdge(0, 3, 15))
g.add_edge(FlowEdge(1, 4, 9))
g.add_edge(FlowEdge(1, 5, 15))
g.add_edge(FlowEdge(1, 2, 4))
g.add_edge(FlowEdge(2, 5, 8))
g.add_edge(FlowEdge(2, 3, 4))
g.add_edge(FlowEdge(3, 6, 16))
g.add_edge(FlowEdge(4, 5, 15))
g.add_edge(FlowEdge(4, 7, 10))
g.add_edge(FlowEdge(5, 7, 10))
g.add_edge(FlowEdge(5, 6, 15))
g.add_edge(FlowEdge(6, 2, 6))
g.add_edge(FlowEdge(6, 7, 10))

return g

0 comments on commit 98f158e

Please sign in to comment.