@@ -9,24 +9,23 @@ import qualified Data.Graph as G
9
9
10
10
main :: IO ()
11
11
main = do
12
- evaluate $ rnf randGs
12
+ evaluate $ rnf allGs
13
13
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
23
23
]
24
24
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 ]
30
29
31
30
-- Note: In practice it does not make sense to run topSort or bcc on a random
32
31
-- graph. For topSort the graph should be acyclic and for bcc the graph should
@@ -46,12 +45,32 @@ data Graph = Graph
46
45
instance NFData Graph where
47
46
rnf (Graph label g edges adj) = rnf label `seq` rnf g `seq` rnf edges `seq` rnf adj
48
47
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
+
49
54
-- A graph with vertices [1..n] and m random edges.
50
55
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
52
57
where
53
- label = " n=" ++ show n ++ " ,m=" ++ show m
58
+ label = " rand, n=" ++ show n ++ " ,m=" ++ show m
54
59
xs = randomRs (1 , n) (mkStdGen 1 )
55
60
(us, xs') = splitAt m xs
56
61
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