module Data.Graph.Inductive.Query.MaxFlow(
getRevEdges, augmentGraph, updAdjList, updateFlow, mfmg, mf, maxFlowgraph,
maxFlow
) where
import Data.List
import Data.Graph.Inductive.Basic
import Data.Graph.Inductive.Graph
import Data.Graph.Inductive.Query.BFS
getRevEdges :: (Num b) => [Edge] -> [LEdge b]
getRevEdges :: forall b. Num b => [Edge] -> [LEdge b]
getRevEdges [] = []
getRevEdges ((Int
u,Int
v):[Edge]
es) | (Int
v,Int
u) forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Edge]
es = (Int
v,Int
u,b
0)forall a. a -> [a] -> [a]
:forall b. Num b => [Edge] -> [LEdge b]
getRevEdges [Edge]
es
| Bool
otherwise = forall b. Num b => [Edge] -> [LEdge b]
getRevEdges (forall a. Eq a => a -> [a] -> [a]
delete (Int
v,Int
u) [Edge]
es)
augmentGraph :: (DynGraph gr, Num b) => gr a b -> gr a (b,b,b)
augmentGraph :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b) =>
gr a b -> gr a (b, b, b)
augmentGraph gr a b
g = forall (gr :: * -> * -> *) b c a.
DynGraph gr =>
(b -> c) -> gr a b -> gr a c
emap (\b
i->(b
i,b
0,b
i)) (forall (gr :: * -> * -> *) b a.
DynGraph gr =>
[LEdge b] -> gr a b -> gr a b
insEdges (forall b. Num b => [Edge] -> [LEdge b]
getRevEdges (forall (gr :: * -> * -> *) a b. Graph gr => gr a b -> [Edge]
edges gr a b
g)) gr a b
g)
updAdjList::(Num b) => Adj (b,b,b) -> Node -> b -> Bool -> Adj (b,b,b)
updAdjList :: forall b.
Num b =>
Adj (b, b, b) -> Int -> b -> Bool -> Adj (b, b, b)
updAdjList Adj (b, b, b)
s Int
v b
cf Bool
fwd = Adj (b, b, b)
rs forall a. [a] -> [a] -> [a]
++ ((b
x,b
yforall a. Num a => a -> a -> a
+b
cf',b
zforall a. Num a => a -> a -> a
-b
cf'),Int
w) forall a. a -> [a] -> [a]
: Adj (b, b, b)
rs'
where
(Adj (b, b, b)
rs, ((b
x,b
y,b
z),Int
w):Adj (b, b, b)
rs') = forall a. (a -> Bool) -> [a] -> ([a], [a])
break ((Int
vforall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) Adj (b, b, b)
s
cf' :: b
cf' = if Bool
fwd
then b
cf
else forall a. Num a => a -> a
negate b
cf
updateFlow :: (DynGraph gr, Num b) => Path -> b -> gr a (b,b,b) -> gr a (b,b,b)
updateFlow :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b) =>
Path -> b -> gr a (b, b, b) -> gr a (b, b, b)
updateFlow [] b
_ gr a (b, b, b)
g = gr a (b, b, b)
g
updateFlow [Int
_] b
_ gr a (b, b, b)
g = gr a (b, b, b)
g
updateFlow (Int
u:Int
v:Path
vs) b
cf gr a (b, b, b)
g = case forall (gr :: * -> * -> *) a b.
Graph gr =>
Int -> gr a b -> Decomp gr a b
match Int
u gr a (b, b, b)
g of
(MContext a (b, b, b)
Nothing,gr a (b, b, b)
g') -> gr a (b, b, b)
g'
(Just (Adj (b, b, b)
p,Int
u',a
l,Adj (b, b, b)
s),gr a (b, b, b)
g') -> (Adj (b, b, b)
p',Int
u',a
l,Adj (b, b, b)
s') forall (gr :: * -> * -> *) a b.
DynGraph gr =>
Context a b -> gr a b -> gr a b
& gr a (b, b, b)
g2
where
g2 :: gr a (b, b, b)
g2 = forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b) =>
Path -> b -> gr a (b, b, b) -> gr a (b, b, b)
updateFlow (Int
vforall a. a -> [a] -> [a]
:Path
vs) b
cf gr a (b, b, b)
g'
s' :: Adj (b, b, b)
s' = forall b.
Num b =>
Adj (b, b, b) -> Int -> b -> Bool -> Adj (b, b, b)
updAdjList Adj (b, b, b)
s Int
v b
cf Bool
True
p' :: Adj (b, b, b)
p' = forall b.
Num b =>
Adj (b, b, b) -> Int -> b -> Bool -> Adj (b, b, b)
updAdjList Adj (b, b, b)
p Int
v b
cf Bool
False
mfmg :: (DynGraph gr, Num b, Ord b) => gr a (b,b,b) -> Node -> Node -> gr a (b,b,b)
mfmg :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a (b, b, b) -> Int -> Int -> gr a (b, b, b)
mfmg gr a (b, b, b)
g Int
s Int
t
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null Path
augPath = gr a (b, b, b)
g
| Bool
otherwise = forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a (b, b, b) -> Int -> Int -> gr a (b, b, b)
mfmg (forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b) =>
Path -> b -> gr a (b, b, b) -> gr a (b, b, b)
updateFlow Path
augPath b
minC gr a (b, b, b)
g) Int
s Int
t
where
minC :: b
minC = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum (forall a b. (a -> b) -> [a] -> [b]
map ((\(b
_,b
_,b
z)->b
z)forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd)(forall a. [a] -> [a]
tail [(Int, (b, b, b))]
augLPath))
augPath :: Path
augPath = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Int, (b, b, b))]
augLPath
LP [(Int, (b, b, b))]
augLPath = forall (gr :: * -> * -> *) a b.
Graph gr =>
Int -> Int -> gr a b -> LPath b
lesp Int
s Int
t gr a (b, b, b)
gf
gf :: gr a (b, b, b)
gf = forall (gr :: * -> * -> *) b a.
DynGraph gr =>
(b -> Bool) -> gr a b -> gr a b
elfilter (\(b
_,b
_,b
z)->b
zforall a. Eq a => a -> a -> Bool
/=b
0) gr a (b, b, b)
g
mf :: (DynGraph gr, Num b, Ord b) => gr a b -> Node -> Node -> gr a (b,b,b)
mf :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a b -> Int -> Int -> gr a (b, b, b)
mf gr a b
g = forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a (b, b, b) -> Int -> Int -> gr a (b, b, b)
mfmg (forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b) =>
gr a b -> gr a (b, b, b)
augmentGraph gr a b
g)
maxFlowgraph :: (DynGraph gr, Num b, Ord b) => gr a b -> Node -> Node -> gr a (b,b)
maxFlowgraph :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a b -> Int -> Int -> gr a (b, b)
maxFlowgraph gr a b
g Int
s Int
t = forall (gr :: * -> * -> *) b c a.
DynGraph gr =>
(b -> c) -> gr a b -> gr a c
emap (\(b
u,b
v,b
_)->(b
v,b
u))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (gr :: * -> * -> *) b a.
DynGraph gr =>
(b -> Bool) -> gr a b -> gr a b
elfilter (\(b
x,b
_,b
_) -> b
xforall a. Eq a => a -> a -> Bool
/=b
0 )
forall a b. (a -> b) -> a -> b
$ forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a b -> Int -> Int -> gr a (b, b, b)
mf gr a b
g Int
s Int
t
maxFlow :: (DynGraph gr, Num b, Ord b) => gr a b -> Node -> Node -> b
maxFlow :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a b -> Int -> Int -> b
maxFlow gr a b
g Int
s Int
t = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. LEdge b -> b
edgeLabel) (forall (gr :: * -> * -> *) a b.
Graph gr =>
gr a b -> Int -> [LEdge b]
out (forall (gr :: * -> * -> *) b a.
(DynGraph gr, Num b, Ord b) =>
gr a b -> Int -> Int -> gr a (b, b)
maxFlowgraph gr a b
g Int
s Int
t) Int
s))