module Data.IntGraph.Directed (
Node,
NodeSet,
Edge,
IntGraph,
empty,
addNode,
nodes,
removeNode,
addEdge,
edges,
removeEdge,
fromEdges)
where
import qualified Data.IntMap as I
import qualified Data.Set as S
import Data.IntGraph
addEdge :: Edge -> IntGraph -> IntGraph
addEdge (u, v) graph = IG $ I.adjust (S.insert v) u g'
where
(IG g') = addNode u $ addNode v graph
adjacencyToList :: Node -> NodeSet -> [Edge]
adjacencyToList node neighbors = map ((,) node) $ S.elems neighbors
edges :: IntGraph -> [Edge]
edges (IG graph) = I.foldrWithKey f [] graph
where
f node neighbors rest = adjacencyToList node neighbors ++ rest
removeEdge :: Edge -> IntGraph -> IntGraph
removeEdge (u, v) (IG graph) = IG $ I.adjust (S.delete v) u graph
fromEdges :: [Edge] -> IntGraph
fromEdges graph = foldr addEdge empty graph