{"id":1505,"date":"2015-06-02T06:00:20","date_gmt":"2015-06-02T04:00:20","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=1505"},"modified":"2022-03-26T14:33:08","modified_gmt":"2022-03-26T12:33:08","slug":"caminos-en-un-grafo-1","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/caminos-en-un-grafo-1\/","title":{"rendered":"Caminos en un grafo"},"content":{"rendered":"<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   grafo   :: [(Int,Int)] -> Grafo Int Int\n   caminos :: Grafo Int Int -> Int -> Int -> [[Int]]\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>(grafo as) es el grafo no dirigido definido cuyas aristas son as. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     ghci> grafo [(2,4),(4,5)]\n     G ND (array (2,5) [(2,[(4,0)]),(3,[]),(4,[(2,0),(5,0)]),(5,[(4,0)])])\n<\/pre>\n<ul>\n<li>(caminos g a b) es la lista los caminos en el grafo g desde a hasta b sin pasar dos veces por el mismo nodo. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     ghci> sort (caminos (grafo [(1,3),(2,5),(3,5),(3,7),(5,7)]) 1 7)\n     [[1,3,5,7],[1,3,7]]\n     ghci> sort (caminos (grafo [(1,3),(2,5),(3,5),(3,7),(5,7)]) 2 7)\n     [[2,5,3,7],[2,5,7]]\n     ghci> sort (caminos (grafo [(1,3),(2,5),(3,5),(3,7),(5,7)]) 1 2)\n     [[1,3,5,2],[1,3,7,5,2]]\n     ghci> caminos (grafo [(1,3),(2,5),(3,5),(3,7),(5,7)]) 1 4\n     []\n     ghci> length (caminos (grafo [(i,j) | i <- [1..10], j <- [i..10]]) 1 10)\n     109601\n<\/pre>\n<p>Nota: Este ejercicio debe realizarse usando \u00fanicamente las funciones de la librer\u00eda de grafos (I1M.Grafo) que se describe <a href=\"http:\/\/bit.ly\/1IugN6r\">aqu\u00ed<\/a> y se encuentra <a href=\"http:\/\/bit.ly\/1AKmUQB\">aqu\u00ed<\/a>.<\/p>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (sort)\nimport I1M.Grafo\nimport I1M.BusquedaEnEspaciosDeEstados\n\ngrafo :: [(Int,Int)] -> Grafo Int Int\ngrafo as = creaGrafo ND (m,n) [(x,y,0) | (x,y) <- as]\n    where ns = map fst as ++ map snd as\n          m  = minimum ns\n          n  = maximum ns\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\ncaminos :: Grafo Int Int -> Int -> Int -> [[Int]]\ncaminos g a b = aux [[b]] where \n    aux [] = []\n    aux ((x:xs):yss)\n        | x == a    = (x:xs) : aux yss\n        | otherwise = aux ([z:x:xs | z <- adyacentes g x\n                                   , z `notElem` (x:xs)] \n                           ++ yss) \n\n-- 2\u00aa soluci\u00f3n (mediante espacio de estados)\n-- =========================================\n\ncaminos2 :: Grafo Int Int -> Int -> Int -> [[Int]]\ncaminos2 g a b = buscaEE sucesores esFinal inicial\n    where inicial          = [b]\n          sucesores (x:xs) = [z:x:xs | z <- adyacentes g x\n                                     , z `notElem` (x:xs)] \n          esFinal (x:xs)   = x == a\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    ghci> length (caminos (grafo [(i,j) | i <- [1..10], j <- [i..10]]) 1 10)\n--    109601\n--    (3.57 secs, 500533816 bytes)\n--    ghci> length (caminos2 (grafo [(i,j) | i <- [1..10], j <- [i..10]]) 1 10)\n--    109601\n--    (3.53 secs, 470814096 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir las funciones grafo :: [(Int,Int)] -> Grafo Int Int caminos :: Grafo Int Int -> Int -> Int -> [[Int]] tales que (grafo as) es el grafo no dirigido definido cuyas aristas son as. Por ejemplo, ghci> grafo [(2,4),(4,5)] G ND (array (2,5) [(2,[(4,0)]),(3,[]),(4,[(2,0),(5,0)]),(5,[(4,0)])]) (caminos g a b) es la lista los caminos en&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[7],"tags":[498,456,453],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1505"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=1505"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1505\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1505\/revisions\/1557"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=1505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=1505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=1505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}