yingpotter

[99p]VonKoch

Aug 8th, 2011
1,496
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. ----- predefined graphs
  5. type Arcgraph a = [(a, a)]
  6. vkgraphtest :: Arcgraph Int
  7. vkgraphtest = [(1,2),(1,3)]
  8. vkgraph0 :: Arcgraph Int
  9. vkgraph0 = [(1,2),(1,4),(1,7),(2,3),(2,5),(5,6)]
  10. vkgraph1 :: Arcgraph Int
  11. vkgraph1 = [(1,6),(2,6),(3,6),(4,6),(5,6),(5,7),(5,8),
  12.             (5,10),(8,9),(10,11),(11,12),(11,13),(13,14)]
  13. successlabel = [(1,1),(6,14),(2,2),(3,3),(4,4),(5,5),(7,13),(8,12),(10,11),(11,6),(12,10),(9,9),(13,8),(14,7)]            
  14.  
  15. ----- VonKoch Conjecture (graceful tree labeling)
  16.   --- A very slow , not applicable solution by permutation
  17. dist :: [Int] ->Arcgraph Int ->[Int]
  18. dist l g = map (\x -> abs(l!!(fst x -1) - l!!(snd x -1))) g
  19. vonPermu :: Arcgraph Int ->[[Int]]
  20. vonPermu [] = []
  21. vonPermu g = filter (\x -> sort (eds x) == [1..edge]) (permutations [1..edge+1])
  22.             where edge = length g
  23.                   eds x = dist x g
  24.  
  25.   --- Solution with a deterministic search
  26.   --- This solution doesnot return all possible labelings
  27. validLabel :: Ord a => Int -> [(a, Int)] -> Arcgraph a -> [(a, Int)]
  28. validLabel _ [] _ = []
  29. validLabel k labels g = [ (node, snd l + k) | node <- adj labels, l <- labels, (if node < fst l then (node, fst l) else (fst l, node)) `elem` g, (snd l + k) `elem` ([1..n] \\ (map snd labels)) ]
  30.                      ++ [ (node, snd l - k) | node <- adj labels, l <- labels, (if node < fst l then (node, fst l) else (fst l, node)) `elem` g, (snd l - k) `elem` ([1..n] \\ (map snd labels)) ]
  31.                         where n = length g + 1
  32.                               adj [] = []
  33.                               adj labels = nub([ fst x | x <- g, snd x `elem` (map fst labels) ] ++ [ snd x | x <- g, fst x `elem` (map fst labels) ]) \\ (map fst labels)
  34. vkSearch :: Ord a => Int -> Arcgraph a -> [[(a, Int)]] -> [[(a, Int)]]
  35. vkSearch 1 g labellists = labellists
  36. vkSearch n g labellists = vkSearch (n-1) g (concatMap (newLabel (n-1)) labellists)
  37.                           where newLabel k labels = [ labels ++ [s] | s <- validLabel k labels g ]
  38. vonKoch :: Arcgraph Int -> [[Int]]
  39. vonKoch [] = []
  40. vonKoch g = vkk $ concatMap (vkSearch n g) [ [[(i,1)]] | i <- [1..n] ]
  41.             where n = length g + 1
  42.                   vkk :: Ord a => [[(a, Int)]] -> [[Int]]
  43.                   vkk [] = []
  44.                   vkk labellists = map extractlabel labellists
  45.                                    where extractlabel labels = map snd $ sortBy (compare `on` fst) labels
Advertisement
Add Comment
Please, Sign In to add comment