Diferencia entre revisiones de «Relación 8»
De Razonamiento automático (2013-14)
Línea 24: | Línea 24: | ||
value "N (N H H) (N H H)" | value "N (N H H) (N H H)" | ||
+ | value "(N (N H H) (N (N H H) H))" --"Este tambien es un arbol segun la definicion del tipo. Sin embargo no es completo." | ||
text {* | text {* | ||
Línea 38: | Línea 39: | ||
"hojas H = Suc 0" | "hojas H = Suc 0" | ||
| "hojas (N i d) = hojas i + hojas d" | | "hojas (N i d) = hojas i + hojas d" | ||
+ | |||
+ | --"julrobrel" | ||
+ | fun hojas_jrr :: "arbol => nat" where | ||
+ | "hojas_jrr H = 1" --"julrobrel: minima diferencia con la anterior" | ||
+ | |"hojas_jrr (N i d) = (hojas_jrr i) + (hojas_jrr d)" | ||
+ | |||
+ | value "hojas_jrr (N (N H H) (N H H))" -- "= 4" | ||
value "hojas (N (N H H) (N H H))" -- "= 4" | value "hojas (N (N H H) (N H H))" -- "= 4" | ||
+ | value "hojas (N (N H H) (N (N H H) H))" --"= 5" --"julrobrel: la funcion tambien funciona sobre arboles binarios no completos" | ||
text {* | text {* | ||
Línea 54: | Línea 63: | ||
"profundidad H = 0" | "profundidad H = 0" | ||
| "profundidad (N i d) = (if profundidad i > profundidad d then Suc (profundidad i) else Suc (profundidad d))" | | "profundidad (N i d) = (if profundidad i > profundidad d then Suc (profundidad i) else Suc (profundidad d))" | ||
+ | |||
+ | --"julrobrel" | ||
+ | fun maximo :: "nat => nat => nat" where | ||
+ | "maximo a b = (if a < b then b else a)" | ||
+ | |||
+ | value "maximo 3 4" -- "=4" | ||
+ | |||
+ | --"julrobrel" | ||
+ | fun profundidad_jrr :: "arbol => nat" where | ||
+ | "profundidad_jrr H = 0" | ||
+ | |"profundidad_jrr (N i d) = Suc (maximo (profundidad_jrr i) (profundidad_jrr d))" --"julrobrel: En realidad al ser arboles completos no deberia hacer falta el maximo" | ||
value "profundidad (N (N H H) (N H H))" -- "= 2" | value "profundidad (N (N H H) (N H H))" -- "= 2" | ||
+ | value "profundidad (N (N H H) (N (N H H) H))" -- "= 3" --"julrobrel: profundidad tambien funciona sobre arboles binarios no completos" | ||
+ | |||
+ | value "profundidad_jrr (N (N H H) (N H H))" -- "= 2" | ||
+ | value "profundidad_jrr (N (N H H) (N (N H H) H))" -- "= 3" --"julrobrel: profundidad tambien funciona sobre arboles binarios no completos" | ||
text {* | text {* | ||
Línea 67: | Línea 91: | ||
*} | *} | ||
− | --"diecabmen1" | + | --"diecabmen1, julrobrel" |
fun abc :: "nat ⇒ arbol" where | fun abc :: "nat ⇒ arbol" where | ||
"abc 0 = H" | "abc 0 = H" | ||
Línea 92: | Línea 116: | ||
"es_abc f H = True" | "es_abc f H = True" | ||
| "es_abc f (N i d) = ((es_abc f i) ∧ (es_abc f d) ∧ (f i = f d))" | | "es_abc f (N i d) = ((es_abc f i) ∧ (es_abc f d) ∧ (f i = f d))" | ||
+ | |||
+ | --"julrobrel" | ||
+ | fun es_abc_jrr:: "nat => arbol => bool" where | ||
+ | "es_abc_jrr 0 H = True" | ||
+ | |"es_abc_jrr (Suc f) H = False" | ||
+ | |"es_abc_jrr 0 (N i d) = False" | ||
+ | |"es_abc_jrr (Suc f) (N i d) = (((es_abc_jrr f i) ∧ (es_abc_jrr f d)) ∧ ((profundidad_jrr i) = (profundidad_jrr d)) ∧ ((profundidad_jrr i) = f))" | ||
+ | |||
+ | value "es_abc_jrr 2 (N (N H H) (N H H))" | ||
+ | value "es_abc_jrr 0 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo" | ||
+ | value "es_abc_jrr 1 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo" | ||
+ | value "es_abc_jrr 2 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo" | ||
+ | value "es_abc_jrr 3 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo" | ||
text {* | text {* | ||
Línea 102: | Línea 139: | ||
value "size (N (N H H) (N H H))" | value "size (N (N H H) (N H H))" | ||
value "size (N (N (N H H) (N H H)) (N (N H H) (N H H)))" | value "size (N (N (N H H) (N H H)) (N (N H H) (N H H)))" | ||
+ | value "size (N (N H H) (N (N H H) H))" --"julrobrel: No es un arbol binario completo" | ||
text {* | text {* | ||
Línea 118: | Línea 156: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | --"julrobrel: en un arbol binario completo la profundidad y las hojas estan relacionadas por la siguiente igualdad (hojas = 2^profundidad)" | ||
+ | lemma "(hojas (abc n))=2^(profundidad (abc n))" | ||
+ | by (induct n) auto | ||
text {* | text {* | ||
Línea 125: | Línea 166: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | --"julrobrel: la relacion entre hojas y nodos la da la siguiente ecuacion (nodos = hojas -1) o lo que es lo mismo (nodos+1=hojas)" | ||
+ | lemma "Suc(size (abc n))=hojas (abc n)" | ||
+ | by (induct n) auto | ||
text {* | text {* | ||
Línea 132: | Línea 176: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | --"julrobrel: la relacion entre la profundidad y el numero de nodos es (nodos+1=2^profundidad)" | ||
+ | lemma "Suc(size (abc n))=2^(profundidad (abc n))" | ||
+ | by (induct n) auto | ||
text {* | text {* | ||
Línea 138: | Línea 185: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | --"julrobrel: para todo lo anterior nos hemos basado en que (abc n) siempre devuelve un arbol binario completo. Habra que demostrarlo..." | ||
+ | lemma profundidad_lemma: "profundidad_jrr (abc n) = n" | ||
+ | by (induct n) auto | ||
+ | |||
+ | lemma "es_abc_jrr n (abc n)" | ||
+ | by (induct n) (auto simp add:profundidad_lemma) | ||
text {* | text {* | ||
Línea 145: | Línea 198: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | lemma "(es_abc_jrr (profundidad_jrr a) a) ⟶ a = abc (profundidad_jrr a)" | ||
+ | by (induct a) auto | ||
text {* | text {* | ||
Línea 152: | Línea 207: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | --"julrobrel" | ||
+ | lemma "es_abc_jrr f t = es_abc_jrr (size t) t" | ||
+ | quickcheck | ||
+ | oops | ||
+ | --"julrobrel: El arbol que es una hoja..." | ||
+ | (* | ||
+ | Quickcheck found a counterexample: | ||
+ | |||
+ | f = Suc 0 | ||
+ | t = H | ||
+ | |||
+ | Evaluated terms: | ||
+ | es_abc f t = False | ||
+ | es_abc (size t) t = True | ||
+ | *) | ||
end | end | ||
</source> | </source> |
Revisión del 16:10 12 ene 2014
header {* R8: Árboles binarios completos *}
theory R8_Arboles_binarios_completos
imports Main
begin
text {*
---------------------------------------------------------------------
Ejercicio 1. Definir el tipo de datos arbol para representar los
árboles binarios que no tienen información ni en los nodos y ni en las
hojas. Por ejemplo, el árbol
·
/ \
/ \
· ·
/ \ / \
· · · ·
se representa por "N (N H H) (N H H)".
---------------------------------------------------------------------
*}
datatype arbol = H | N arbol arbol
value "N (N H H) (N H H)"
value "(N (N H H) (N (N H H) H))" --"Este tambien es un arbol segun la definicion del tipo. Sin embargo no es completo."
text {*
---------------------------------------------------------------------
Ejercicio 2. Definir la función
hojas :: "arbol => nat"
tal que (hojas a) es el número de hojas del árbol a. Por ejemplo,
hojas (N (N H H) (N H H)) = 4
---------------------------------------------------------------------
*}
--"diecabmen1"
fun hojas :: "arbol => nat" where
"hojas H = Suc 0"
| "hojas (N i d) = hojas i + hojas d"
--"julrobrel"
fun hojas_jrr :: "arbol => nat" where
"hojas_jrr H = 1" --"julrobrel: minima diferencia con la anterior"
|"hojas_jrr (N i d) = (hojas_jrr i) + (hojas_jrr d)"
value "hojas_jrr (N (N H H) (N H H))" -- "= 4"
value "hojas (N (N H H) (N H H))" -- "= 4"
value "hojas (N (N H H) (N (N H H) H))" --"= 5" --"julrobrel: la funcion tambien funciona sobre arboles binarios no completos"
text {*
---------------------------------------------------------------------
Ejercicio 4. Definir la función
profundidad :: "arbol => nat"
tal que (profundidad a) es la profundidad del árbol a. Por ejemplo,
profundidad (N (N H H) (N H H)) = 2
---------------------------------------------------------------------
*}
--"diecabmen1"
fun profundidad :: "arbol => nat" where
"profundidad H = 0"
| "profundidad (N i d) = (if profundidad i > profundidad d then Suc (profundidad i) else Suc (profundidad d))"
--"julrobrel"
fun maximo :: "nat => nat => nat" where
"maximo a b = (if a < b then b else a)"
value "maximo 3 4" -- "=4"
--"julrobrel"
fun profundidad_jrr :: "arbol => nat" where
"profundidad_jrr H = 0"
|"profundidad_jrr (N i d) = Suc (maximo (profundidad_jrr i) (profundidad_jrr d))" --"julrobrel: En realidad al ser arboles completos no deberia hacer falta el maximo"
value "profundidad (N (N H H) (N H H))" -- "= 2"
value "profundidad (N (N H H) (N (N H H) H))" -- "= 3" --"julrobrel: profundidad tambien funciona sobre arboles binarios no completos"
value "profundidad_jrr (N (N H H) (N H H))" -- "= 2"
value "profundidad_jrr (N (N H H) (N (N H H) H))" -- "= 3" --"julrobrel: profundidad tambien funciona sobre arboles binarios no completos"
text {*
---------------------------------------------------------------------
Ejercicio 5. Definir la función
abc :: "nat ⇒ arbol"
tal que (abc n) es el árbol binario completo de profundidad n. Por
ejemplo,
abc 3 = N (N (N H H) (N H H)) (N (N H H) (N H H))
---------------------------------------------------------------------
*}
--"diecabmen1, julrobrel"
fun abc :: "nat ⇒ arbol" where
"abc 0 = H"
| "abc (Suc n) = (N (abc n) (abc n))"
value "abc 3" -- "= N (N (N H H) (N H H)) (N (N H H) (N H H))"
text {*
---------------------------------------------------------------------
Ejercicio 6. Un árbol binario a es completo respecto de la medida f si
a es una hoja o bien a es de la forma (N i d) y se cumple que tanto i
como d son árboles binarios completos respecto de f y, además,
f(i) = f(r).
Definir la función
es_abc :: "(arbol => 'a) => arbol => bool
tal que (es_abc f a) se verifica si a es un árbol binario completo
respecto de f.
---------------------------------------------------------------------
*}
--"diecabmen1"
fun es_abc :: "(arbol => 'a) => arbol => bool" where
"es_abc f H = True"
| "es_abc f (N i d) = ((es_abc f i) ∧ (es_abc f d) ∧ (f i = f d))"
--"julrobrel"
fun es_abc_jrr:: "nat => arbol => bool" where
"es_abc_jrr 0 H = True"
|"es_abc_jrr (Suc f) H = False"
|"es_abc_jrr 0 (N i d) = False"
|"es_abc_jrr (Suc f) (N i d) = (((es_abc_jrr f i) ∧ (es_abc_jrr f d)) ∧ ((profundidad_jrr i) = (profundidad_jrr d)) ∧ ((profundidad_jrr i) = f))"
value "es_abc_jrr 2 (N (N H H) (N H H))"
value "es_abc_jrr 0 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo"
value "es_abc_jrr 1 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo"
value "es_abc_jrr 2 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo"
value "es_abc_jrr 3 (N (N H H) (N (N H H) H))" --"=False" --"julrobrel: No es un arbol binario completo"
text {*
---------------------------------------------------------------------
Nota. (size a) es el número de nodos del árbol a. Por ejemplo,
size (N (N H H) (N H H)) = 3
---------------------------------------------------------------------
*}
value "size (N (N H H) (N H H))"
value "size (N (N (N H H) (N H H)) (N (N H H) (N H H)))"
value "size (N (N H H) (N (N H H) H))" --"julrobrel: No es un arbol binario completo"
text {*
---------------------------------------------------------------------
Nota. Tenemos 3 funciones de medida sobre los árboles: número de
hojas, número de nodos y profundidad. A cada una le corresponde un
concepto de completitud. En los siguientes ejercicios demostraremos
que los tres conceptos de completitud son iguales.
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 7. Demostrar que un árbol binario a es completo respecto de
la profundidad syss es completo respecto del número de hojas.
---------------------------------------------------------------------
*}
--"julrobrel: en un arbol binario completo la profundidad y las hojas estan relacionadas por la siguiente igualdad (hojas = 2^profundidad)"
lemma "(hojas (abc n))=2^(profundidad (abc n))"
by (induct n) auto
text {*
---------------------------------------------------------------------
Ejercicio 8. Demostrar que un árbol binario a es completo respecto del
número de hojas syss es completo respecto del número de nodos
---------------------------------------------------------------------
*}
--"julrobrel: la relacion entre hojas y nodos la da la siguiente ecuacion (nodos = hojas -1) o lo que es lo mismo (nodos+1=hojas)"
lemma "Suc(size (abc n))=hojas (abc n)"
by (induct n) auto
text {*
---------------------------------------------------------------------
Ejercicio 9. Demostrar que un árbol binario a es completo respecto de
la profundidad syss es completo respecto del número de nodos
---------------------------------------------------------------------
*}
--"julrobrel: la relacion entre la profundidad y el numero de nodos es (nodos+1=2^profundidad)"
lemma "Suc(size (abc n))=2^(profundidad (abc n))"
by (induct n) auto
text {*
---------------------------------------------------------------------
Ejercicio 10. Demostrar que (abc n) es un árbol binario completo.
---------------------------------------------------------------------
*}
--"julrobrel: para todo lo anterior nos hemos basado en que (abc n) siempre devuelve un arbol binario completo. Habra que demostrarlo..."
lemma profundidad_lemma: "profundidad_jrr (abc n) = n"
by (induct n) auto
lemma "es_abc_jrr n (abc n)"
by (induct n) (auto simp add:profundidad_lemma)
text {*
---------------------------------------------------------------------
Ejercicio 11. Demostrar que si a es un árbolo binario completo
respecto de la profundidad, entonces a es (abc (profundidad a)).
---------------------------------------------------------------------
*}
lemma "(es_abc_jrr (profundidad_jrr a) a) ⟶ a = abc (profundidad_jrr a)"
by (induct a) auto
text {*
---------------------------------------------------------------------
Ejercicio 12. Encontrar una medida f tal que (es_abc f) es distinto de
(es_abc size).
---------------------------------------------------------------------
*}
--"julrobrel"
lemma "es_abc_jrr f t = es_abc_jrr (size t) t"
quickcheck
oops
--"julrobrel: El arbol que es una hoja..."
(*
Quickcheck found a counterexample:
f = Suc 0
t = H
Evaluated terms:
es_abc f t = False
es_abc (size t) t = True
*)
end