{"id":7943,"date":"2023-06-03T18:01:48","date_gmt":"2023-06-03T16:01:48","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/?p=7943"},"modified":"2023-06-03T18:08:47","modified_gmt":"2023-06-03T16:08:47","slug":"03-jun-23","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/03-jun-23\/","title":{"rendered":"La semana en Exercitium (3 de junio de 2023)"},"content":{"rendered":"<p>Esta semana he publicado en <a href=\"http:\/\/bit.ly\/2sqPtGs\">Exercitium<\/a> las soluciones de los siguientes problemas sobre el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>:<\/p>\n<ul>\n<li><a href=\"#ej1\">1. Incidentes de un v\u00e9rtice<\/a><\/li>\n<li><a href=\"#ej2\">2. Contiguos de un v\u00e9rtice<\/a><\/li>\n<li><a href=\"#ej3\">3. Lazos de un grafo<\/a><\/li>\n<li><a href=\"#ej4\">4. N\u00famero de aristas de un grafo<\/a><\/li>\n<li><a href=\"#ej5\">5. Grado positivo y negativo de un v\u00e9rtice<\/a><\/li>\n<\/ul>\n<p>A continuaci\u00f3n se muestran las soluciones.<br \/>\n<!--more--><br \/>\n<a name=\"ej1\"><\/a><\/p>\n<h3>1. Incidentes de un v\u00e9rtice<\/h3>\n<p>En un un grafo g, los incidentes de un v\u00e9rtice v es el conjuntos de v\u00e9rtices x de g para los que hay un arco (o una arista) de x a v; es decir, que v es adyacente a x.<\/p>\n<p>Usando el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>, definir la funci\u00f3n,<\/p>\n<pre lang=\"text\">\n   incidentes :: (Ix v,Num p) => (Grafo v p) -> v -> [v]\n<\/pre>\n<p>tal que <code>incidentes g v<\/code> es la lista de los v\u00e9rtices incidentes en el v\u00e9rtice <code>v<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> g1 = creaGrafo' D (1,3) [(1,2),(2,2),(3,1),(3,2)]\n   \u03bb> incidentes g1 1\n   [3]\n   \u03bb> incidentes g1 2\n   [1,2,3]\n   \u03bb> incidentes g1 3\n   []\n   \u03bb> g2 = creaGrafo' ND (1,3) [(1,2),(2,2),(3,1),(3,2)]\n   \u03bb> incidentes g2 1\n   [2,3]\n   \u03bb> incidentes g2 2\n   [1,2,3]\n   \u03bb> incidentes g2 3\n   [1,2]\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmodule Grafo_Incidentes_de_un_vertice where\n\nimport TAD.Grafo (Grafo, Orientacion (D, ND), nodos,  adyacentes, creaGrafo')\nimport Data.Ix\nimport Test.Hspec\n\nincidentes :: (Ix v,Num p) => Grafo v p -> v -> [v]\nincidentes g v = [x | x <- nodos g, v `elem` adyacentes g x]\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspec :: Spec\nspec = do\n  it \"e1\" $\n    incidentes g1 1 `shouldBe` [3]\n  it \"e2\" $\n    incidentes g1 2 `shouldBe` [1,2,3]\n  it \"e3\" $\n    incidentes g1 3 `shouldBe` []\n  it \"e4\" $\n    incidentes g2 1 `shouldBe` [2,3]\n  it \"e5\" $\n    incidentes g2 2 `shouldBe` [1,2,3]\n  it \"e6\" $\n    incidentes g2 3 `shouldBe` [1,2]\n  where\n    g1, g2 :: Grafo Int Int\n    g1  = creaGrafo' D (1,3) [(1,2),(2,2),(3,1),(3,2)]\n    g2 = creaGrafo' ND (1,3) [(1,2),(2,2),(3,1),(3,2)]\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--\n--    e1\n--    e2\n--    e3\n--    e4\n--    e5\n--    e6\n--\n--    Finished in 0.0005 seconds\n--    6 examples, 0 failures\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom src.TAD.Grafo import (Grafo, Orientacion, Vertice, adyacentes, creaGrafo_,\n                           nodos)\n\n\ndef incidentes(g: Grafo, v: Vertice) -> list[Vertice]:\n    return [x for x in nodos(g) if v in adyacentes(g, x)]\n\n# Verificaci\u00f3n\n# ============\n\ndef test_incidentes() -> None:\n    g1 = creaGrafo_(Orientacion.D, (1,3), [(1,2),(2,2),(3,1),(3,2)])\n    g2 = creaGrafo_(Orientacion.ND, (1,3), [(1,2),(2,2),(3,1),(3,2)])\n    assert incidentes(g1,1) == [3]\n    assert incidentes(g1,2) == [1, 2, 3]\n    assert incidentes(g1,3) == []\n    assert incidentes(g2, 1) == [2, 3]\n    assert incidentes(g2, 2) == [1, 2, 3]\n    assert incidentes(g2, 3) == [1, 2]\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_incidentes()\n#    Verificado\n<\/pre>\n<p><a name=\"ej2\"><\/a><\/p>\n<h3>2. Contiguos de un v\u00e9rtice<\/h3>\n<p>En un un grafo g, los contiguos de un v\u00e9rtice v es el conjuntos de v\u00e9rtices x de g tales que x es adyacente o incidente con v.<\/p>\n<p>Usando el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>, definir la funci\u00f3n,<\/p>\n<pre lang=\"text\">\n   contiguos :: (Ix v,Num p) => Grafo v p -> v -> [v]\n<\/pre>\n<p>tal que <code>contiguos g v<\/code> es el conjunto de los v\u00e9rtices de g contiguos con el v\u00e9rtice v. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> g1 = creaGrafo' D (1,3) [(1,2),(2,2),(3,1),(3,2)]\n   \u03bb> contiguos g1 1\n   [2,3]\n   \u03bb> contiguos g1 2\n   [2,1,3]\n   \u03bb> contiguos g1 3\n   [1,2]\n   \u03bb> g2 = creaGrafo' ND (1,3) [(1,2),(2,2),(3,1),(3,2)]\n   \u03bb> contiguos g2 1\n   [2,3]\n   \u03bb> contiguos g2 2\n   [1,2,3]\n   \u03bb> contiguos g2 3\n   [1,2]\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmodule Grafo_Contiguos_de_un_vertice where\n\nimport TAD.Grafo (Grafo, Orientacion (D, ND), adyacentes, creaGrafo')\nimport Grafo_Incidentes_de_un_vertice (incidentes)\nimport Data.List (nub)\nimport Data.Ix\nimport Test.Hspec\n\ncontiguos :: (Ix v,Num p) => Grafo v p -> v -> [v]\ncontiguos g v = nub (adyacentes g v ++ incidentes g v)\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspec :: Spec\nspec = do\n  it \"e1\" $\n    contiguos g1 1 `shouldBe` [2,3]\n  it \"e2\" $\n    contiguos g1 2 `shouldBe` [2,1,3]\n  it \"e3\" $\n    contiguos g1 3 `shouldBe` [1,2]\n  it \"e4\" $\n    contiguos g2 1 `shouldBe` [2,3]\n  it \"e5\" $\n    contiguos g2 2 `shouldBe` [1,2,3]\n  it \"e6\" $\n    contiguos g2 3 `shouldBe` [1,2]\n  where\n    g1, g2 :: Grafo Int Int\n    g1 = creaGrafo' D (1,3) [(1,2),(2,2),(3,1),(3,2)]\n    g2 = creaGrafo' ND (1,3) [(1,2),(2,2),(3,1),(3,2)]\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--\n--    e1\n--    e2\n--    e3\n--    e4\n--    e5\n--    e6\n--\n--    Finished in 0.0005 seconds\n--    6 examples, 0 failures\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom src.Grafo_Incidentes_de_un_vertice import incidentes\nfrom src.TAD.Grafo import Grafo, Orientacion, Vertice, adyacentes, creaGrafo_\n\n\ndef contiguos(g: Grafo, v: Vertice) -> list[Vertice]:\n    return list(set(adyacentes(g, v) + incidentes(g, v)))\n\n# Verificaci\u00f3n\n# ============\n\ndef test_contiguos() -> None:\n    g1 = creaGrafo_(Orientacion.D, (1,3), [(1,2),(2,2),(3,1),(3,2)])\n    g2 = creaGrafo_(Orientacion.ND, (1,3), [(1,2),(2,2),(3,1),(3,2)])\n    assert contiguos(g1, 1) == [2, 3]\n    assert contiguos(g1, 2) == [1, 2, 3]\n    assert contiguos(g1, 3) == [1, 2]\n    assert contiguos(g2, 1) == [2, 3]\n    assert contiguos(g2, 2) == [1, 2, 3]\n    assert contiguos(g2, 3) == [1, 2]\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_contiguos()\n#    Verificado\n<\/pre>\n<p><a name=\"ej3\"><\/a><\/p>\n<h3>3. Lazos de un grafo<\/h3>\n<p>Usando el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>, definir las funciones,<\/p>\n<pre lang=\"text\">\n   lazos  :: (Ix v,Num p) => Grafo v p -> [(v,v)]\n   nLazos :: (Ix v,Num p) => Grafo v p -> Int\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li><code>lazos g<\/code> es el conjunto de los lazos (es decir, aristas cuyos extremos son iguales) del grafo <code>g<\/code>. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> ej1 = creaGrafo' D (1,3) [(1,1),(2,3),(3,2),(3,3)]\n     \u03bb> ej2 = creaGrafo' ND (1,3) [(2,3),(3,1)]\n     \u03bb> lazos ej1\n     [(1,1),(3,3)]\n     \u03bb> lazos ej2\n     []\n<\/pre>\n<ul>\n<li><code>nLazos g<\/code> es el n\u00famero de lazos del grafo <code>g<\/code>. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> nLazos ej1\n     2\n     \u03bb> nLazos ej2\n     0\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmodule Grafo_Lazos_de_un_grafo where\n\nimport TAD.Grafo (Grafo, Orientacion (D, ND), nodos, aristaEn, creaGrafo')\nimport Data.Ix (Ix)\nimport Test.Hspec (Spec, hspec, it, shouldBe)\n\nlazos :: (Ix v,Num p) => Grafo v p -> [(v,v)]\nlazos g = [(x,x) | x <- nodos g, aristaEn g (x,x)]\n\nnLazos :: (Ix v,Num p) => Grafo v p ->  Int\nnLazos = length . lazos\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspec :: Spec\nspec = do\n  it \"e1\" $\n    lazos ej1 `shouldBe` [(1,1),(3,3)]\n  it \"e2\" $\n    lazos ej2 `shouldBe` []\n  it \"e3\" $\n    nLazos ej1 `shouldBe` 2\n  it \"e4\" $\n    nLazos ej2 `shouldBe` 0\n  where\n    ej1, ej2 :: Grafo Int Int\n    ej1 = creaGrafo' D (1,3) [(1,1),(2,3),(3,2),(3,3)]\n    ej2 = creaGrafo' ND (1,3) [(2,3),(3,1)]\n\n-- La verificaci\u00f3n es\n--      \u03bb> verifica\n--\n--      e1\n--      e2\n--      e3\n--      e4\n--\n--      Finished in 0.0005 seconds\n--      4 examples, 0 failures\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom src.TAD.Grafo import (Grafo, Orientacion, Vertice, aristaEn, creaGrafo_,\n                           nodos)\n\n\ndef lazos(g: Grafo) -> list[tuple[Vertice, Vertice]]:\n    return [(x, x) for x in nodos(g) if aristaEn(g, (x, x))]\n\ndef nLazos(g: Grafo) -> int:\n    return len(lazos(g))\n\n# Verificaci\u00f3n\n# ============\n\ndef test_lazos() -> None:\n    ej1 = creaGrafo_(Orientacion.D, (1,3), [(1,1),(2,3),(3,2),(3,3)])\n    ej2 = creaGrafo_(Orientacion.ND, (1,3), [(2,3),(3,1)])\n    assert lazos(ej1) == [(1,1),(3,3)]\n    assert lazos(ej2) == []\n    assert nLazos(ej1) == 2\n    assert nLazos(ej2) == 0\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_lazos()\n#    Verificado\n<\/pre>\n<p><a name=\"ej4\"><\/a><\/p>\n<h3>4. N\u00famero de aristas de un grafo<\/h3>\n<p>Usando el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>, definir la funci\u00f3n,<\/p>\n<pre lang=\"text\">\n   nAristas :: (Ix v,Num p) => Grafo v p ->  Int\n<\/pre>\n<p>tal que <code>nAristas g<\/code> es el n\u00famero de aristas del grafo <code>g<\/code>. Si g es no dirigido, las aristas de <code>v1<\/code> a <code>v2<\/code> y de <code>v2<\/code> a <code>v1<\/code> s\u00f3lo se cuentan una vez. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> g1 = creaGrafo' ND (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)]\n   \u03bb> g2 = creaGrafo' D  (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)]\n   \u03bb> g3 = creaGrafo' ND (1,3) [(1,2),(1,3),(2,3),(3,3)]\n   \u03bb> g4 = creaGrafo' ND (1,4) [(1,1),(1,2),(3,3)]\n   \u03bb> nAristas g1\n   8\n   \u03bb> nAristas g2\n   7\n   \u03bb> nAristas g3\n   4\n   \u03bb> nAristas g4\n   3\n   \u03bb> nAristas (completo 4)\n   6\n   \u03bb> nAristas (completo 5)\n   10\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   prop_nAristasCompleto :: Int -> Bool\n<\/pre>\n<p>tal que <code>prop_nAristasCompleto n<\/code> se verifica si el n\u00famero de aristas del grafo completo de orden <code>n<\/code> es <code>n*(n-1)\/2<\/code> y, usando la funci\u00f3n, comprobar que la propiedad se cumple para <code>n<\/code> de 1 a 20.<\/p>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmodule Grafo_Numero_de_aristas_de_un_grafo where\n\nimport TAD.Grafo (Grafo, Orientacion (D, ND), dirigido, aristas, creaGrafo')\nimport Data.Ix (Ix)\nimport Grafo_Lazos_de_un_grafo (nLazos)\nimport Grafo_Grafos_completos (completo)\nimport Test.Hspec (Spec, hspec, it, shouldBe, describe)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nnAristas :: (Ix v,Num p) => Grafo v p ->  Int\nnAristas g | dirigido g = length (aristas g)\n           | otherwise  = (length (aristas g) + nLazos g) `div` 2\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nnAristas2 :: (Ix v,Num p) => Grafo v p ->  Int\nnAristas2 g | dirigido g = length (aristas g)\n            | otherwise  = length [(x,y) | ((x,y),_) <- aristas g, x <= y]\n\n-- Propiedad\n-- =========\n\nprop_nAristasCompleto :: Int -> Bool\nprop_nAristasCompleto n =\n  nAristas (completo n) == n*(n-1) `div` 2\n\n-- La comprobaci\u00f3n es\n--    \u03bb> and [prop_nAristasCompleto n | n <- [1..20]]\n--    True\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspec :: Spec\nspec = do\n  describe \"def. 1\" $ specG nAristas\n  describe \"def. 2\" $ specG nAristas2\n\nspecG :: (Grafo Int Int -> Int) -> Spec\nspecG nAristas' = do\n  it \"e1\" $\n    nAristas' g1 `shouldBe` 8\n  it \"e2\" $\n    nAristas' g2 `shouldBe` 7\n  it \"e3\" $\n    nAristas' g3 `shouldBe` 4\n  it \"e4\" $\n    nAristas' g4 `shouldBe` 3\n  it \"e5\" $\n    nAristas' (completo 4) `shouldBe` 6\n  it \"e6\" $\n    nAristas' (completo 5) `shouldBe` 10\n  where\n    g1, g2, g3, g4 :: Grafo Int Int\n    g1 = creaGrafo' ND (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)]\n    g2 = creaGrafo' D  (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)]\n    g3 = creaGrafo' ND (1,3) [(1,2),(1,3),(2,3),(3,3)]\n    g4 = creaGrafo' ND (1,4) [(1,1),(1,2),(3,3)]\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--\n--    def. 1\n--      e1\n--      e2\n--      e3\n--      e4\n--      e5\n--      e6\n--    def. 2\n--      e1\n--      e2\n--      e3\n--      e4\n--      e5\n--      e6\n--\n--    Finished in 0.0013 seconds\n--    12 examples, 0 failures\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom src.Grafo_Grafos_completos import completo\nfrom src.Grafo_Lazos_de_un_grafo import nLazos\nfrom src.TAD.Grafo import Grafo, Orientacion, aristas, creaGrafo_, dirigido\n\n# 1\u00aa soluci\u00f3n\n# ===========\n\ndef nAristas(g: Grafo) -> int:\n    if dirigido(g):\n        return len(aristas(g))\n    return (len(aristas(g)) + nLazos(g)) \/\/ 2\n\n# 2\u00aa soluci\u00f3n\n# ===========\n\ndef nAristas2(g: Grafo) -> int:\n    if dirigido(g):\n        return len(aristas(g))\n    return len([(x, y) for ((x,y),_) in aristas(g) if x <= y])\n\n# Propiedad\n# =========\n\ndef prop_nAristasCompleto(n: int) -> bool:\n    return nAristas(completo(n)) == n*(n-1) \/\/ 2\n\n# La comprobaci\u00f3n es\n#    >>> all(prop_nAristasCompleto(n) for n in range(1, 21))\n#    True\n\n# Verificaci\u00f3n\n# ============\n\ndef test_nAristas() -> None:\n    g1 = creaGrafo_(Orientacion.ND, (1,5),\n                    [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)])\n    g2 = creaGrafo_(Orientacion.D, (1,5),\n                    [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)])\n    g3 = creaGrafo_(Orientacion.ND, (1,3), [(1,2),(1,3),(2,3),(3,3)])\n    g4 = creaGrafo_(Orientacion.ND, (1,4), [(1,1),(1,2),(3,3)])\n    for nAristas_ in [nAristas, nAristas2]:\n        assert nAristas_(g1) == 8\n        assert nAristas_(g2) == 7\n        assert nAristas_(g3) == 4\n        assert nAristas_(g4) == 3\n        assert nAristas_(completo(4)) == 6\n        assert nAristas_(completo(5)) == 10\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_nAristas()\n#    Verificado\n<\/pre>\n<p><a name=\"ej5\"><\/a><\/p>\n<h3>5. Grado positivo y negativo de un v\u00e9rtice<\/h3>\n<p>El grado positivo de un v\u00e9rtice v de un grafo g es el n\u00famero de v\u00e9rtices de g adyacentes con v y su grado negativo es el n\u00famero de v\u00e9rtices de g incidentes con v.<\/p>\n<p>Usando el <a href=\"https:\/\/bit.ly\/45cQ3Fo\">tipo abstracto de datos de los grafos<\/a>, definir las funciones,<\/p>\n<pre lang=\"text\">\n   gradoPos :: (Ix v,Num p) => Grafo v p -> v -> Int\n   gradoNeg :: (Ix v,Num p) => Grafo v p -> v -> Int\n<\/pre>\n<p>tales que<br \/>\n+ <code>gradoPos g v<\/code> es el grado positivo del v\u00e9rtice <code>v<\/code> en el grafo <code>g<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n     \u03bb> g1 = creaGrafo' ND (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)]\n     \u03bb> g2 = creaGrafo' D  (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)]\n     \u03bb> gradoPos g1 5\n     4\n     \u03bb> gradoPos g2 5\n     0\n     \u03bb> gradoPos g2 1\n     3\n<\/pre>\n<ul>\n<li><code>gradoNeg g v<\/code> es el grado negativo del v\u00e9rtice <code>v<\/code> en el grafo <code>g<\/code>. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> gradoNeg g1 5\n     4\n     \u03bb> gradoNeg g2 5\n     3\n     \u03bb> gradoNeg g2 1\n     0\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmodule Grafo_Grados_positivos_y_negativos where\n\nimport TAD.Grafo (Grafo, Orientacion (D, ND), adyacentes, creaGrafo')\nimport Data.Ix (Ix)\nimport Grafo_Incidentes_de_un_vertice (incidentes)\nimport Test.Hspec (Spec, hspec, it, shouldBe)\n\n-- 1\u00aa definici\u00f3n de gradoPos\ngradoPos :: (Ix v,Num p) => Grafo v p -> v -> Int\ngradoPos g v = length (adyacentes g v)\n\n-- 2\u00aa definici\u00f3n de gradoPos\ngradoPos2 :: (Ix v,Num p) => Grafo v p -> v -> Int\ngradoPos2 g = length . adyacentes g\n\n-- 1\u00aa definici\u00f3n de gradoNeg\ngradoNeg :: (Ix v,Num p) => Grafo v p -> v -> Int\ngradoNeg g v = length (incidentes g v)\n\n-- 2\u00aa definici\u00f3n de gradoNeg\ngradoNeg2 :: (Ix v,Num p) => Grafo v p -> v -> Int\ngradoNeg2 g = length . incidentes g\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspec :: Spec\nspec = do\n  it \"e1\" $\n    gradoPos g1 5 `shouldBe` 4\n  it \"e2\" $\n    gradoPos g2 5 `shouldBe` 0\n  it \"e3\" $\n    gradoPos g2 1 `shouldBe` 3\n  it \"e4\" $\n    gradoNeg g1 5 `shouldBe` 4\n  it \"e5\" $\n    gradoNeg g2 5 `shouldBe` 3\n  it \"e6\" $\n    gradoNeg g2 1 `shouldBe` 0\n  it \"e7\" $\n    gradoPos2 g1 5 `shouldBe` 4\n  it \"e8\" $\n    gradoPos2 g2 5 `shouldBe` 0\n  it \"e9\" $\n    gradoPos2 g2 1 `shouldBe` 3\n  it \"e10\" $\n    gradoNeg2 g1 5 `shouldBe` 4\n  it \"e11\" $\n    gradoNeg2 g2 5 `shouldBe` 3\n  it \"e12\" $\n    gradoNeg2 g2 1 `shouldBe` 0\n  where\n    g1, g2 :: Grafo Int Int\n    g1 = creaGrafo' ND (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)]\n    g2 = creaGrafo' D  (1,5) [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)]\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--\n--    def. 1\n--      e1\n--      e2\n--      e3\n--      e4\n--      e5\n--      e6\n--    def. 2\n--      e1\n--      e2\n--      e3\n--      e4\n--      e5\n--      e6\n--\n--    Finished in 0.0013 seconds\n--    12 examples, 0 failures\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom src.Grafo_Incidentes_de_un_vertice import incidentes\nfrom src.TAD.Grafo import Grafo, Orientacion, Vertice, adyacentes, creaGrafo_\n\n\ndef gradoPos(g: Grafo, v: Vertice) -> int:\n    return len(adyacentes(g, v))\n\ndef gradoNeg(g: Grafo, v: Vertice) -> int:\n    return len(incidentes(g, v))\n\n# Verificaci\u00f3n\n# ============\n\ndef test_GradoPosNeg() -> None:\n    g1 = creaGrafo_(Orientacion.ND, (1,5),\n                    [(1,2),(1,3),(1,5),(2,4),(2,5),(3,4),(3,5),(4,5)])\n    g2 = creaGrafo_(Orientacion.D, (1,5),\n                    [(1,2),(1,3),(1,5),(2,4),(2,5),(4,3),(4,5)])\n    assert gradoPos(g1, 5) == 4\n    assert gradoPos(g2, 5) == 0\n    assert gradoPos(g2, 1) == 3\n    assert gradoNeg(g1, 5) == 4\n    assert gradoNeg(g2, 5) == 3\n    assert gradoNeg(g2, 1) == 0\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_GradoPosNeg()\n#    Verificado\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Esta semana he publicado en Exercitium las soluciones de los siguientes problemas sobre el tipo abstracto de datos de los grafos: 1. Incidentes de un v\u00e9rtice 2. Contiguos de un v\u00e9rtice 3. Lazos de un grafo 4. N\u00famero de aristas de un grafo 5. Grado positivo y negativo de un v\u00e9rtice A continuaci\u00f3n se muestran&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","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":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7943"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/comments?post=7943"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7943\/revisions"}],"predecessor-version":[{"id":7945,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7943\/revisions\/7945"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/media?parent=7943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/categories?post=7943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/tags?post=7943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}