Skip to content

Skielex/thinhpf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thin wrapper for HPF

Thin Python wrapper for the non-parametric Hochbaum Pseudoflow (HPF) min-cut/max-flow algorithm. The original source code by Bala Chandran and Dorit S. Hochbaum is availbable here. The C++ code used in this wrapper has been refractored by Patrick M. Jensen and published here.

Installation

pip install thinhpf

Tiny example

import thinhpf

hpf = thinhpf.hpf()

# Add s and t.
next_node_id = hpf.add_node(2)

def offset(n):
    return 2 + n

s = 0
t = 1

hpf.set_source(0)
hpf.set_sink(1)

# Number of nodes to add.
nodes_to_add = 2

# Add two nodes.
next_node_id = hpf.add_node(nodes_to_add)

# Add edges.
hpf.add_edge(s, offset(0), 5)  # s     --5->   n(0)
hpf.add_edge(offset(0), t, 1)  # n(0)  --1->   t
hpf.add_edge(offset(1), t, 3)  # n(1)  --3->   t
hpf.add_edge(offset(0), offset(1), 2)  # n(0)  --2->   n(1)
hpf.add_edge(offset(1), offset(0), 1)  # n(1)  --1->   n(0)
# Find maxflow/cut hpf.
hpf.mincut()
flow = hpf.compute_maxflow()

for n in range(nodes_to_add):
    segment = hpf.what_label(n)
    print('Node %d has label %d.' % (n, segment))
# Node 0 has label 0.
# Node 1 has label 1.

print('Flow: %s' % flow)
# Maximum flow: 3

Related repositories

License

The original C code by Bala Chandran and Dorit S. Hochbaum and thereby the content of hpf.h (previously pseudo.c) is published under an academic license (see LICENSE file). More information on the original website.