Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1.  
  2.  
  3. import List;
  4. import Maybe;
  5.  
  6.  
  7.  
  8. type Cost = Integer
  9. type Vertex = Integer
  10. type MaxVertexNo = Integer
  11. type Edge = (Vertex, Cost, Vertex)
  12. type Row = [Integer]
  13.  
  14. data ALgraph = ALg [(Vertex,[(Vertex,Cost)])] deriving (Eq,Show)
  15. data ELgraph = ELg MaxVertexNo [Edge] deriving (Eq,Show)
  16. data AMgraph = AMg [Row] deriving (Eq,Show)
  17.  
  18.  
  19. type Inp = (MaxVertexNo, [(Vertex,Vertex,Cost)])
  20.  
  21.  
  22. {- Helper: -}
  23.  
  24. fst3 :: (a, b, c) -> a
  25. fst3 (x,_,_) = x
  26.  
  27. snd3 :: (a, b, c) -> b
  28. snd3 (_,x,_) = x
  29.  
  30. thd3 :: (a, b, c) -> c
  31. thd3 (_,_,x) = x
  32.  
  33.  
  34.  
  35. {- Bsp 1: -}
  36. available :: [(Vertex, [Vertex])] -> (Vertex,Vertex,Cost) -> Bool
  37. available liste kante = isJust (find (\x ->  (fst x) == min (fst3 kante) (snd3 kante) && isJust (find (\y -> y == (max (fst3 kante) (snd3 kante))) (snd x))) liste)
  38.  
  39.  
  40. push :: [(Vertex, [Vertex])] -> (Vertex,Vertex, Cost) -> [(Vertex, [Vertex])]
  41. push liste lst = liste ++ [(von,[zu])]
  42.  where von = min (fst3 lst) (snd3 lst)
  43.        zu  = max (fst3 lst) (snd3 lst)
  44.  
  45. push2 :: [(Vertex, [Vertex])] -> (Vertex,Vertex, Cost) -> Maybe [(Vertex, [Vertex])]
  46. push2 liste kante
  47.   | available liste kante    = Nothing
  48.   | otherwise                = Just (push liste kante)
  49.  
  50.  
  51.  
  52. {- TODO:
  53.    !!!ACHTUNG!!!
  54.    Bitte schaut noch mal ob validnames-variable passt, oder ob ich die Namenscheck von den Schranken her falsch
  55.    aufgefasst habe!
  56. -}
  57. walktrough :: MaxVertexNo -> [(Vertex,Vertex,Cost)] -> [(Vertex, [Vertex])] -> Maybe [(Vertex, [Vertex])]
  58. walktrough max graph list
  59.   | length graph == 0   = Just list {- Prüft ob noch eine Liste am stack liegt. Wenn nein: Done! -}
  60.   | validnames == False = Nothing   {- Prüft die Namen -}
  61.   | (thd3 cur) <= 0     = Nothing   {- Prüft ob das Gewicht des aktuellen Elements valide ist -}
  62.   | isNothing (newlist) = Nothing   {- Prüft ob eine Kante mit den selben Endpunkten bereits existiert, und retuniert gegebenenfalls -}
  63.   | otherwise           = nextedge  {- Das nächste Element -}
  64.  where
  65.   ngraph = tail graph
  66.   cur  = head graph
  67.   nextedge = walktrough max ngraph (fromJust newlist)
  68.   newlist  = push2 list cur
  69.   validnames = (fst3 cur) >= 0 && (snd3 cur) >= 0 && (fst3 cur) < max && (snd3 cur) < max
  70.  
  71.  
  72. isValid :: Inp -> Bool
  73. isValid g = isJust (walktrough (fst g) (snd g) [])
  74.  
  75.  
  76.  
  77. {- Bsp 2: -}
  78.  
  79.  
  80. {- Wir gehen von einem validen Graphen aus! kA ob wir das dürfen. -}
  81. inp2el :: Inp -> ELgraph
  82. inp2el g = ELg maxno (map (\x -> (fst3 x, thd3 x, snd3 x)) list)
  83.  where
  84.   maxno = fst g
  85.   list = snd g
  86.  
  87.  
  88.  
  89. {- Bsp 3: -}
  90.  
  91. {-- Helpers: --}
  92. isEdge :: Vertex -> Vertex -> Edge -> Bool
  93. isEdge a b e = (a == (fst3 e) && b == (thd3 e)) || (b == (fst3 e) && a == (thd3 e))
  94.  
  95. notMe :: Vertex -> Vertex -> Vertex -> Vertex
  96. notMe a b ich
  97.   | a == ich  = b
  98.   | otherwise = a
  99.  
  100.  
  101.  
  102. {-- Lösungen: --}
  103. isNeighbourOf :: ELgraph -> Vertex -> Vertex -> Bool
  104. isNeighbourOf (ELg max edges) a b = isJust (find (\x -> (isEdge a b x)) edges)
  105.  
  106. allNeighboursOf :: ELgraph -> Vertex -> [Vertex]
  107. allNeighboursOf (ELg max edges) a = sort (map (\x -> (notMe (fst3 x) (thd3 x) a)) (myfilter (\x -> (fst3 x) == a || (thd3 x) == a) edges))
  108.  
  109. {- TODO: Sollte funktionieren, sobald Beispiel 2 fertig ist!
  110. numberOfEdges :: AMgraph -> Integer
  111. numberOfEdges g = length (allNeighboursOf (am2el g))
  112. -}
  113.  
  114.  
  115.  
  116. {- Bitte noch mal genauer testen, ob das mit den Restriktionen eh passt. (i.e. zwei Knoten dürfen nur einmal verbunden sein && Kanten sind gerichtet -- dann sollte es egientlich passen.)  -}
  117. isOnCycle :: ALgraph -> Vertex -> Cost -> Bool
  118. isOnCycle g v cost = path g v v cost
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. {---------------------------------------------}
  126. {-     Helper - kopiert aus aufgabe4.hs!     -}
  127. {---------------------------------------------}
  128.  
  129.  
  130. type MyGraph = [(Vertex,[(Vertex,Cost)])]
  131. castGraph :: ALgraph -> MyGraph
  132. castGraph (ALg xs) = xs
  133.  
  134. {- Prüft ob ein Vertex bereits besucht wurde -}
  135. exist :: [Vertex] -> Vertex -> Bool
  136. exist mypath node =  isNothing (find (==node) mypath) == False
  137.  
  138.  
  139. {- Gibt die Liste aller Kanten, passend zu einem Knoten, zurück. -}
  140. getedges :: MyGraph -> Vertex -> [(Vertex,Cost)]
  141. getedges g k
  142.   | isNothing e   = []
  143.   | otherwise     = snd ((maybeToList e)!!0)
  144.  where e = find (\n -> (fst n) == k) g
  145.  
  146.  
  147.  
  148.  
  149. {- Durchsucht den Graphen rekursiv nach einen Pfad und prüft, sobald ein Pfad gefunden wurde, ob er billig genug ist.
  150. Wenn kein Pfad existiert, oder dieser zu teuer ist, dann wird eine leere Liste zurück gegeben. -}
  151. findPath :: MyGraph -> Vertex -> Vertex -> [Vertex] -> Int -> Integer -> Integer -> Bool -> [Vertex]
  152. findPath g von zu mypath pos max cur first
  153.   | von == zu && cur <= max && first == False = mypath ++ [von]
  154.   | von == zu && first == False               = []
  155.   | (length edges) <= pos                     = []
  156.   | pos == 0 && (exist mypath von)            = []
  157.   | (length current /= 0)                     = current
  158.   | pos < (length edges)                      = next_sibling
  159.   | otherwise                                 = []
  160.  where
  161.   next_sibling = findPath g von zu mypath (pos+1) max cur first
  162.   edges   = getedges g von
  163.   weight  = cur + snd (edges!!pos)
  164.   current = findPath g (fst (edges!!pos)) zu (mypath++[von]) 0 max weight False
  165.  
  166.  
  167. {- Versucht erst einen Pfad zu finden. Wenn keiner gefunden wird, werden Start und Endknoten auf Existenz geprüft. -}
  168. path :: ALgraph -> Vertex -> Vertex -> Cost -> Bool
  169. path graph von zu max
  170.   | (length mypath) == 0      = False
  171.   | otherwise                 = True
  172.  where
  173.   mypath = (findPath g von zu [] 0 max 0) True
  174.   g = castGraph graph
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. myfilter :: (Edge -> Bool) -> [Edge] -> [Edge]
  185. myfilter praedikat list
  186.   | (length list) == 0   = []
  187.   | praedikat cur        = [cur] ++ (myfilter praedikat rest)
  188.   | otherwise            = myfilter praedikat rest
  189.  where
  190.   cur = head list
  191.   rest = tail list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement