module Data.Graph.Inductive.Query.GVD (
Voronoi,LRTree,
gvdIn,gvdOut,
voronoiSet,nearestNode,nearestDist,nearestPath,
) where
import Data.List (nub)
import Data.Maybe (listToMaybe)
import qualified Data.Graph.Inductive.Internal.Heap as H
import Data.Graph.Inductive.Basic
import Data.Graph.Inductive.Graph
import Data.Graph.Inductive.Internal.RootPath
import Data.Graph.Inductive.Query.SP (dijkstra)
type Voronoi a = LRTree a
gvdIn :: (DynGraph gr, Real b) => [Node] -> gr a b -> Voronoi b
gvdIn :: forall (gr :: * -> * -> *) b a.
(DynGraph gr, Real b) =>
[Node] -> gr a b -> Voronoi b
gvdIn [Node]
vs gr a b
g = forall (gr :: * -> * -> *) b a.
(Graph gr, Real b) =>
[Node] -> gr a b -> Voronoi b
gvdOut [Node]
vs (forall (gr :: * -> * -> *) a b. DynGraph gr => gr a b -> gr a b
grev gr a b
g)
gvdOut :: (Graph gr, Real b) => [Node] -> gr a b -> Voronoi b
gvdOut :: forall (gr :: * -> * -> *) b a.
(Graph gr, Real b) =>
[Node] -> gr a b -> Voronoi b
gvdOut [Node]
vs = forall (gr :: * -> * -> *) b a.
(Graph gr, Real b) =>
Heap b (LPath b) -> gr a b -> LRTree b
dijkstra (forall a b. Ord a => [(a, b)] -> Heap a b
H.build (forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. a -> [a]
repeat b
0) (forall a b. (a -> b) -> [a] -> [b]
map (\Node
v->forall a. [LNode a] -> LPath a
LP [(Node
v,b
0)]) [Node]
vs)))
voronoiSet :: Node -> Voronoi b -> [Node]
voronoiSet :: forall b. Node -> Voronoi b -> [Node]
voronoiSet Node
v = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (\[Node]
p->forall a. [a] -> a
last [Node]
pforall a. Eq a => a -> a -> Bool
==Node
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (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 a. LPath a -> [LNode a]
unLPath)
maybePath :: Node -> Voronoi b -> Maybe (LPath b)
maybePath :: forall b. Node -> Voronoi b -> Maybe (LPath b)
maybePath Node
v = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ((Node
vforall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. LPath a -> [LNode a]
unLPath)
nearestNode :: Node -> Voronoi b -> Maybe Node
nearestNode :: forall b. Node -> Voronoi b -> Maybe Node
nearestNode Node
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
last forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. LPath a -> [LNode a]
unLPath) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. Node -> Voronoi b -> Maybe (LPath b)
maybePath Node
v
nearestDist :: Node -> Voronoi b -> Maybe b
nearestDist :: forall b. Node -> Voronoi b -> Maybe b
nearestDist Node
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. LPath a -> [LNode a]
unLPath) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. Node -> Voronoi b -> Maybe (LPath b)
maybePath Node
v
nearestPath :: Node -> Voronoi b -> Maybe Path
nearestPath :: forall b. Node -> Voronoi b -> Maybe [Node]
nearestPath Node
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (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 a. LPath a -> [LNode a]
unLPath) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. Node -> Voronoi b -> Maybe (LPath b)
maybePath Node
v