module Exercise_5 where import Test.QuickCheck import Data.List {- H5.3 -} {-WETT-} type Vertex = Int type Edge = (Vertex, Vertex) type Graph = ([Vertex], [Edge]) longestPath :: Graph -> Vertex -> Int longestPath g t = magic t 0 where magic v n | null $ before v = n | otherwise = maximum [magic x (n+1) | x <- before v] before w = [fst x | x <- (snd g), (snd x == w)] {- longestPath :: Graph -> Vertex -> Int longestPath g t = magic findFirst where findFirst = head $ filter (\x -> not $ elem x (map snd (snd g))) (fst g) magic v | v == t = 0 | null $ getNext v = -1 | otherwise = maximum [(magic x) + 1 | x <- getNext v, x >= 0] getNext w = [snd x | x <- (snd g), (fst x == w)] -} {-TTEW-} -- generates a DAG with u vertices and only one node without incoming edges -- you can use this function to test your implementation using QuickCheck genDag :: Int -> Gen Graph genDag n = let v = [1..n] in do b <- mapM (\i -> choose (1,n-i)) [1..n-1] t <- mapM (\(c,i) -> vectorOf c (choose (i+1, n))) (zip b [1..n]) let e = nub $ ([(1, i) | i<-[2..n]] ++ edges t 1 []) return $ (v,e) where edges [] _ acc = acc edges (ts:xs) i acc = edges xs (i+1) (acc ++ [(i,t) | t<-ts])