Skip to content

Commit fa1d1e7

Browse files
authored
Add more graph types in Graph benchmarks (#931)
Add star and line graphs, which are the extremes of wide and deep graphs.
1 parent 0762255 commit fa1d1e7

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

containers-tests/benchmarks/Graph.hs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ import qualified Data.Graph as G
99

1010
main :: IO ()
1111
main = do
12-
evaluate $ rnf randGs
12+
evaluate $ rnf allGs
1313
defaultMain
14-
[ bgroup "buildG" $ forGs randGs $ \g -> nf (G.buildG (bounds (getG g))) (getEdges g)
15-
, bgroup "graphFromEdges" $ forGs randGs $ nf ((\(g, _, _) -> g) . G.graphFromEdges) . getAdjList
16-
, bgroup "transposeG" $ forGs randGs $ nf G.transposeG . getG
17-
, bgroup "dfs" $ forGs randGs $ nf (flip G.dfs [1]) . getG
18-
, bgroup "dff" $ forGs randGs $ nf G.dff . getG
19-
, bgroup "topSort" $ forGs randGs $ nf G.topSort . getG
20-
, bgroup "scc" $ forGs randGs $ nf G.scc . getG
21-
, bgroup "bcc" $ forGs randGs $ nf G.bcc . getG
22-
, bgroup "stronglyConnCompR" $ forGs randGs $ nf G.stronglyConnCompR . getAdjList
14+
[ bgroup "buildG" $ forGs allGs $ \g -> nf (G.buildG (bounds (getG g))) (getEdges g)
15+
, bgroup "graphFromEdges" $ forGs allGs $ nf ((\(g, _, _) -> g) . G.graphFromEdges) . getAdjList
16+
, bgroup "transposeG" $ forGs allGs $ nf G.transposeG . getG
17+
, bgroup "dfs" $ forGs allGs $ nf (flip G.dfs [1]) . getG
18+
, bgroup "dff" $ forGs allGs $ nf G.dff . getG
19+
, bgroup "topSort" $ forGs allGs $ nf G.topSort . getG
20+
, bgroup "scc" $ forGs allGs $ nf G.scc . getG
21+
, bgroup "bcc" $ forGs allGs $ nf G.bcc . getG
22+
, bgroup "stronglyConnCompR" $ forGs allGs $ nf G.stronglyConnCompR . getAdjList
2323
]
2424
where
25-
randG1 = buildRandG 100 1000
26-
randG2 = buildRandG 100 10000
27-
randG3 = buildRandG 10000 100000
28-
randG4 = buildRandG 100000 1000000
29-
randGs = [randG1, randG2, randG3, randG4]
25+
allGs = randGs ++ starGs ++ lineGs
26+
randGs = map (uncurry buildRandG) [(100, 1000), (100, 10000), (10000, 100000), (100000, 1000000)]
27+
starGs = map buildStarG [100, 1000000]
28+
lineGs = map buildLineG [100, 1000000]
3029

3130
-- Note: In practice it does not make sense to run topSort or bcc on a random
3231
-- graph. For topSort the graph should be acyclic and for bcc the graph should
@@ -46,12 +45,32 @@ data Graph = Graph
4645
instance NFData Graph where
4746
rnf (Graph label g edges adj) = rnf label `seq` rnf g `seq` rnf edges `seq` rnf adj
4847

48+
-- Makes a Graph for benchmarks, from a label, vertex bounds, and the edge list.
49+
makeG :: String -> G.Bounds -> [G.Edge] -> Graph
50+
makeG label bnds edges =
51+
let g = G.buildG bnds edges
52+
in Graph label g edges [(u, u, vs) | (u, vs) <- assocs g]
53+
4954
-- A graph with vertices [1..n] and m random edges.
5055
buildRandG :: Int -> Int -> Graph
51-
buildRandG n m = Graph label g (G.edges g) [(u, u, vs') | (u, vs') <- assocs g]
56+
buildRandG n m = makeG label (1, n) edges
5257
where
53-
label = "n=" ++ show n ++ ",m=" ++ show m
58+
label = "rand,n=" ++ show n ++ ",m=" ++ show m
5459
xs = randomRs (1, n) (mkStdGen 1)
5560
(us, xs') = splitAt m xs
5661
vs = take m xs'
57-
g = G.buildG (1, n) (zip us vs)
62+
edges = zip us vs
63+
64+
-- A star graph, i.e. a graph with an edge from vertex 1 to every other vertex.
65+
-- This serves as an extreme case of a "wide" graph.
66+
buildStarG :: Int -> Graph
67+
buildStarG n = makeG label (1, n) [(1, i) | i <- [2..n]]
68+
where
69+
label = "star,n=" ++ show n
70+
71+
-- A line graph, i.e. a graph with an edge from every vertex i to i+1. This
72+
-- serves an as extreme case of a "deep" graph.
73+
buildLineG :: Int -> Graph
74+
buildLineG n = makeG label (1, n) (zip [1..n-1] [2..])
75+
where
76+
label = "line,n=" ++ show n

0 commit comments

Comments
 (0)