-
Notifications
You must be signed in to change notification settings - Fork 2
/
Problem80.hs
78 lines (66 loc) · 2.19 KB
/
Problem80.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE MultiParamTypeClasses, TupleSections, FlexibleInstances, ConstraintKinds #-}
module Problem80
( pairToList
, graphFormToAdjForm
, adjFormToGraphForm
, fndFormToGraphForm
, graphFormToFndForm
) where
import Data.List hiding (concatMap, concat)
import Data.Either
import Data.Foldable
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.Monoid
import Data.Function
import Prelude hiding
( concat
, concatMap
)
import Graph
pairToList :: (a,a) -> [a]
pairToList ~(x,y) = [x,y]
graphFormToAdjForm :: OrdVE v e => GraphForm v e -> AdjForm v e
graphFormToAdjForm (GraphForm vs es) = AdjForm (M.fromListWith S.union allPairs)
where
allPairs = emptyPairs ++ vertEdgePairs
-- used to ensure that isolated vertices are included
emptyPairs = map (,S.empty) . S.toList $ vs
vertEdgePairs = concatMap edgeToPair . S.toList $ es
edgeToPair e
| (a,b) <- terminals e = [(a, S.singleton e),(b, S.singleton e)]
adjFormToGraphForm :: OrdVE v e => AdjForm v e -> GraphForm v e
adjFormToGraphForm (AdjForm as) = GraphForm vs es
where
vs = M.keysSet as
es = mconcat . M.elems $ as
fndFormToGraphForm :: OrdVE v e => FndForm v e -> GraphForm v e
fndFormToGraphForm (FndForm fs) = GraphForm vs (S.fromList es)
where
es = rights fs
vs1 = lefts fs
vs2 = concatMap (pairToList . terminals ) es
vs = S.fromList $ vs1 ++ vs2
graphFormToFndForm :: OrdVE v e => GraphForm v e -> FndForm v e
graphFormToFndForm (GraphForm vs es) = FndForm (map Left vs' ++ map Right es')
where
-- vertices that appear at least once in the list of edges
evs = S.fromList . concatMap (pairToList . terminals) $ es
vs' = S.toList $ evs `S.union` vs
es' = S.toList es
example :: GraphForm Char (Edge Char)
example = GraphForm vs es
where
vs = S.fromList "ghbcfkd"
es = S.fromList (map (uncurry Edge)
[ ('g','h')
, ('b','c')
, ('b','f')
, ('f','c')
, ('f','k')
])
main :: IO ()
main = do
print example
print (graphFormToAdjForm example)
print (graphFormToFndForm example)