Skip to content

Commit 7ce05c4

Browse files
committed
Define a function for getting number of edges
This does have caveats for undirected graphs but is now pre-defined. Taking the opportunity to also provide a more graph-theoretic alias for the horribly named `noNodes`. Closes #32
1 parent f58da57 commit 7ce05c4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Data/Graph/Inductive/Graph.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module Data.Graph.Inductive.Graph (
3434
Graph(..),
3535
DynGraph(..),
3636
-- * Operations
37+
order,
38+
size,
3739
-- ** Graph Folds and Maps
3840
ufold,gmap,nmap,emap,nemap,
3941
-- ** Graph Projection
@@ -178,6 +180,24 @@ class (Graph gr) => DynGraph gr where
178180
-- or the node in the Context itself (for loops).
179181
(&) :: Context a b -> gr a b -> gr a b
180182

183+
184+
-- | The number of nodes in the graph. An alias for 'noNodes'.
185+
order :: (Graph gr) => gr a b -> Int
186+
order = noNodes
187+
188+
-- | The number of edges in the graph.
189+
--
190+
-- Note that this counts every edge found, so if you are
191+
-- representing an unordered graph by having each edge mirrored this
192+
-- will be incorrect.
193+
--
194+
-- If you created an unordered graph by either mirroring every edge
195+
-- (including loops!) or using the @undir@ function in
196+
-- "Data.Graph.Inductive.Basic" then you can safely halve the value
197+
-- returned by this.
198+
size :: (Graph gr) => gr a b -> Int
199+
size = length . labEdges
200+
181201
-- | Fold a function over the graph.
182202
ufold :: (Graph gr) => (Context a b -> c -> c) -> c -> gr a b -> c
183203
ufold f u g

0 commit comments

Comments
 (0)