yingpotter

[99p]Graph(Complete w/o #85)

Jul 28th, 2011
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import Data.Function
  3.  
  4. -- predefine types
  5. type Arcgraph a = [(a,a)]
  6. type Wgraph a b = [((a,a),b)]
  7. agraph0 = [(1,2),(2,3)]
  8. agraph1 = [(1,2),(2,3),(1,3),(3,4),(4,2),(5,6)]
  9. agraph2 = [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
  10. agraph21 = [(1,2),(1,3),(1,4),(2,1),(2,3),(2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)]
  11. agraph3 = [('a','b'),('a','d'),('b','c'),('b','e'),('c','e'),('d','e'),('d','f'),('d','g'),('e','h'),('f','g'),('g','h')]
  12. agraph4 = [(1,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5),(4,5)]
  13. agraph5 = [('a','c'),('a','d'),('a','e'),('a','f'),('a','k'),('b','c'),('c','d'),('c','g'),('d','e'),('d','h'),('e','i'),
  14.            ('f','j'),('g','h'),('g','l'),('h','i'),('i','j'),('l','k')]
  15. agraph6 = [(1,2),(2,3),(3,4),(4,1)]
  16. agraph7 = [(1,2),(2,3),(1,4),(3,4),(5,2),(5,4)]
  17. wgraph3 = [(('a','b'),5),(('a','d'),3),(('b','c'),2),(('b','e'),4),(('c','e'),6),
  18.            (('d','e'),7),(('d','f'),4),(('d','g'),3),(('e','h'),5),(('f','g'),4),(('g','h'),1)]
  19. wgraph = [((1,2),12),((1,3),34),((1,5),78),((2,4),55),((2,5),32),((3,4),61),((3,5),44),((4,5),93)]
  20.  
  21. -- problem 81
  22. eles [] = []
  23. eles (g:gs) = nub ([fst g, snd g] ++ eles gs)
  24. paths :: Eq a => a -> a -> Arcgraph a -> [[a]]
  25. paths a b g
  26.         | a==b   =[[a]]
  27.         | a/=b   =(if (a,b) `elem` g then [[a,b]] else []) ++
  28.                    nub ([ (init ax)++xb |
  29.                           x <- (delete a (delete b (eles g))), ax <- paths a x ag, xb <- paths x b bg, ax /= [] , xb /= [],
  30.                           (init ax) `intersect` xb == [] ])
  31.                                       where ag = [ga | ga <- g, fst ga /= b, snd ga /= b]
  32.                                             bg = [gb | gb <- g, fst gb /= a, snd gb /= a]
  33.                                            
  34. -- problem 82
  35. cycles :: Eq a => a -> Arcgraph a -> [[a]]
  36. cycles a g = concat [map (++[a]) (paths a x g) | x <- eles g, (x,a) `elem`g]
  37.  
  38. -- problem 83 (solution for non-Arc graphs)
  39. biArc :: Eq a => Arcgraph a -> Arcgraph a
  40. biArc [] = []  
  41. biArc (g:gs) = nub ([g] ++ [(snd g, fst g)] ++ biArc gs)
  42. rid :: Eq a => Arcgraph a -> a -> Arcgraph a
  43. rid [] a = []
  44. rid g a = [x | x <- g, fst x /= a , snd x /= a]
  45. spantree' :: Eq a => Arcgraph a -> [[(a,a)]]
  46. spantree' [(a,b)] = [[(a,b)]]
  47. spantree' g = filter (\x -> length x == length (eles g) -1)
  48.              $ concat [map ([realedge (a,i)]++) (spantree' (rid g a)) | (a,i) <- biArc g]
  49.                         where realedge (a,i) = if (a,i) `elem` g then (a,i) else (i,a)
  50. spantree :: Ord a => Arcgraph a -> [[(a,a)]]
  51. spantree g = nub $ map sort (spantree' g)
  52. istree :: Ord a => Arcgraph a -> Bool
  53. istree g = length (spantree g) == 1
  54. isconnected :: Ord a => Arcgraph a -> Bool
  55. isconnected g = length (spantree g) /= 0
  56.  
  57. -- problem 84
  58. adjWedge' :: (Eq a, Eq b) => [a] -> Wgraph a b -> Wgraph a b
  59. adjWedge' [] g = []
  60. adjWedge' (x:xs) g = nub ([e |e <- g, (fst (fst e) == x)||(snd (fst e) == x)] ++ (adjWedge' xs g))
  61. adjWedge :: (Eq a, Ord b) => [a] -> Wgraph a b -> ((a,a),b)
  62. adjWedge xs g = head $sortBy (compare `on` snd)
  63.                (filter (\e -> (fst (fst e) `notElem` xs) || (snd (fst e) `notElem` xs) == True ) (adjWedge' xs g))
  64.  
  65. elesW :: Eq a => Wgraph a Integer -> [a]
  66. elesW [] = []
  67. elesW (e:gs) = nub ([fst (fst e), snd (fst e)] ++ elesW gs)                
  68. mstPrim' :: Ord a => [a] -> Wgraph a Integer -> Wgraph a Integer -> Wgraph a Integer
  69. mstPrim' xs es g
  70.         | sort xs == sort (elesW g)  = es
  71.         | sort xs /= sort (elesW g)  = (mstPrim' (nub(xs ++ [fst (fst enew)] ++ [snd (fst enew)])) (es++[enew]) g)
  72.                                        where enew = adjWedge xs g
  73. mstPrim :: Ord a => Wgraph a Integer -> Wgraph a Integer
  74. mstPrim [] = []
  75. mstPrim g = mstPrim' [head (sort (elesW g))] [] g
  76. mstPrimV :: Ord a => Wgraph a Integer -> Integer
  77. mstPrimV [] = 0
  78. mstPrimV g = foldr (+) 0 $ map (\e -> snd e) (mstPrim g)
  79.  
  80. -- problem 85
  81.  
  82. -- problem 86 (solution for non-Arc graphs)
  83. dgrcount :: Eq a => Arcgraph a -> a -> Int
  84. dgrcount [] _ = 0
  85. dgrcount (g:gs) x = (if (x == fst g) || (x == snd g) then 1 else 0) + dgrcount gs x
  86. dgrsort :: Eq a => Arcgraph a -> [a]
  87. dgrsort [] = []
  88. dgrsort g = reverse $ sortBy (compare `on` (dgrcount g)) (eles g)
  89. connectTo :: Eq a => a -> Arcgraph a -> [a]
  90. connectTo a [] = [a]
  91. connectTo a (g:gs)
  92.             |  fst g == a   = [snd g] ++ connectTo a gs
  93.             |  snd g == a   = [fst g] ++ connectTo a gs
  94.             |  otherwise    = connectTo a gs
  95. colorone :: Ord a => b -> Arcgraph a -> [a] -> [(a,b)]
  96. colorone _ [] _ = []
  97. colorone c g s
  98.          |  sort s == sort(eles g)  = []
  99.          |  otherwise               = [(m, c)] ++ colorone c g (nub (s ++ (connectTo m g)))
  100.                                           where m = head (filter (`notElem` s) (dgrsort g))
  101. gcolorWP' :: (Ord a, Num b) => b -> Arcgraph a -> [a] -> [(a,b)]
  102. gcolorWP' _ [] _ = []
  103. gcolorWP' c g cs
  104.          |  sort cs == sort(eles g)  = []
  105.          |  otherwise                = clist ++ gcolorWP' (c+1) g (cs++ (map fst clist))
  106.                                           where clist = colorone c g cs
  107. gcolorWP :: (Ord a, Num b) => Arcgraph a -> [(a,b)]
  108. gcolorWP g = gcolorWP' 0 g []
  109.  
  110. -- problem 87
  111. ------ very slow solution
  112. dfsGraph' :: Ord a => Arcgraph a -> [a] -> a -> [a]
  113. dfsGraph' [] _ v = [v]
  114. dfsGraph' g s v
  115.           |  sort s == sort(eles g) = []
  116.           |  otherwise              = nub([v] ++ concatMap (\x -> dfsGraph' (rid g v) (nub(s++[v])) x) (filter (`notElem` s) (connectTo v g)))
  117. dfsGraph :: Ord a => Arcgraph a -> a -> [a]
  118. dfsGraph g v = dfsGraph' g [] v
  119.  
  120. -- problem 88
  121. ridlist :: Eq a => Arcgraph a -> [a] -> Arcgraph a
  122. ridlist [] _ = []
  123. ridlist g as = [x | x <- g, (fst x) `notElem` as , (snd x) `notElem` as]
  124. splitGraph :: Ord a => Arcgraph a -> [[a]]
  125. splitGraph [] = []
  126. splitGraph g = [clist] ++ splitGraph (ridlist g clist)
  127.                where clist = dfsGraph g (head $dgrsort g)
  128.                
  129. -- problem 89 (bi-Arc)
  130. evencycle :: Eq a => a -> Arcgraph a -> Bool
  131. evencycle v [] = True
  132. evencycle v g = foldr (&&) True (map (odd. length) (cycles v g))
  133. bipartite :: Eq a => Arcgraph a -> Bool
  134. bipartite [] = True
  135. bipartite g = foldr (&&) True [evencycle v g | v <- eles g]
Advertisement
Add Comment
Please, Sign In to add comment