Skip to content

Commit a0395c7

Browse files
committed
Speed up indegree
Instead of taking the transpose of a graph and then calculating the outdegree, calculate the indegree directly. This probably won't actually improve performance much (if at all) until `base-4.12` or so comes out: see [GHC Trac #14785](https://ghc.haskell.org/trac/ghc/ticket/14785). If we want, we can reimplement `accumArray` ourselves to work around this problem. Fixes #533
1 parent 7b66a78 commit a0395c7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Data/Graph.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ outdegree = mapT numEdges
348348
--
349349
-- > indegree (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,0),(1,1),(2,1)]
350350
indegree :: Graph -> Array Vertex Int
351-
indegree = outdegree . transposeG
351+
indegree g = accumArray (+) 0 (bounds g) [(v, 1) | (_, outs) <- assocs g, v <- outs]
352352

353353
-- | Identical to 'graphFromEdges', except that the return value
354354
-- does not include the function which maps keys to vertices. This

0 commit comments

Comments
 (0)