Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
aa:lab:sol:12 [2024/01/14 17:34] dmihai created |
aa:lab:sol:12 [2024/01/19 12:21] (current) dmihai |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== TDA-uri și inducție structurală ====== | ||
+ | | ||
+ | |||
1. | 1. | ||
Line 24: | Line 27: | ||
(DEL2) delete(Insert((k, v), m), k') = if k = k' then m | (DEL2) delete(Insert((k, v), m), k') = if k = k' then m | ||
else Insert((k, v), delete(m, k')) | else Insert((k, v), delete(m, k')) | ||
+ | |||
+ | 4. | ||
+ | |||
+ | * $ \forall t \in \texttt{BTree}. size(t) = size(mirror(t)) $ | ||
+ | |||
+ | cazul de baza: | ||
+ | mirror(Nil) = Nil (M1) => size(Nil) = size(mirror(Nil)) | ||
+ | | ||
+ | ipoteza inductiei: size(l) = size(mirror(l)), size(r) = size(mirror(r)) | ||
+ | size(Node(e, l, r)) = 1 + size(l) + size(r) (S2) | ||
+ | = 1 + size(r) + size(l) (comutativitatea adunării) | ||
+ | = 1 + size(mirror(r)) + size(mirror(l)) (ipoteza inductiei) | ||
+ | = size(Node(e, mirror(r), mirror(l))) (S2) | ||
+ | = size(mirror(Node(e, l, r)) (M2) | ||
+ | |||
+ | * $ \forall t \in \texttt{BTree}. size(t) = length(flatten(t)) $ | ||
+ | |||
+ | cazul de baza: | ||
+ | size(Nil) = (S1) = 0 = (L1) = length(Empty) = (F1) = length(flatten(Nil)) | ||
+ | |||
+ | ipoteza inductiei: size(l) = length(flatten(l)), size(r) = length(flatten(r)) | ||
+ | size(Node(e, l, r)) = 1 + size(l) + size(r) (S2) | ||
+ | = 1 + length(flatten(l)) + length(flatten(r)) (ipoteza inductiei) | ||
+ | = 1 + length(append(flatten(l), flatten(r)) (vom demonstra) | ||
+ | = length(Cons(e, append(flatten(l), flatten(r))) (L2) | ||
+ | = length(flatten(Node(e, l, r))) (F2) | ||
+ | |||
+ | mai trebuie sa demonstram pasul intermediar: demonstram ca | ||
+ | |||
+ | length(append(a, b)) = length(a) + length(b) (LAPP) | ||
+ | prin inductie dupa a | ||
+ | |||
+ | cazul de baza: | ||
+ | length(append(Empty, b)) = length(b) (APP1) | ||
+ | = 0 + length(b) | ||
+ | = length(Empty) + length(b) | ||
+ | |||
+ | ipoteza inductiei: length(append(xs, b)) = length(xs) + length(b) | ||
+ | pasul inductiei: | ||
+ | length(append(Cons(x, xs), b)) = length(Cons(x, append(xs, b))) (APP2) | ||
+ | = 1 + length(append(xs, b)) (L2) | ||
+ | = 1 + length(xs) + length(b) (ip. inductiei) | ||
+ | = length(xs) + 1 + length(b) (comutativitatea adunării) | ||
+ | | ||
+ | = length(Cons(x, xs)) + length(b) (L2) | ||
+ | |||
+ | * $ \forall l \in \texttt{List}. append(l, Empty) = l $ | ||
+ | |||
+ | cazul de baza: | ||
+ | append(Empty, Empty) = Empty (APP1) | ||
+ | | ||
+ | ipoteza inductiei: append(xs, Empty) = s | ||
+ | append(Cons(x, xs), Empty) = Cons(x, append(xs, Empty)) (APP2) | ||
+ | = Cons(x, s) (ip. inductiei) | ||
+ | |||
+ | * $ \forall l_1, l_2, l_3 \in \texttt{List}. append(l_1, append(l_2, l_3)) = append(append(l_1, l_2), l_3) $ | ||
+ | |||
+ | facem inductie structurala dupa l_1. Vom nota append(a, b) cu a ++ b ca sa ne fie mai usor | ||
+ | cazul de baza: | ||
+ | Empty ++ (l_2 ++ l_3)) = l_2 ++ l_3 (APP1) | ||
+ | = (Empty ++ l_2) ++ l_3 (APP1) | ||
+ | |||
+ | ipoteza inductiei: l_1 ++ (l_2 ++ l_3) = (l_1 ++ l_2) ++ l_3 | ||
+ | pasul inductiei: | ||
+ | Cons(x, l_1) ++ (l_2 ++ l_3) = Cons(x, l_1 ++ (l_2 ++ l_3)) (APP2) | ||
+ | = Cons(x, (l_1 ++ l_2) ++ l_3) (ip. inductiei) | ||
+ | = Cons(x, l_1 ++ l_2) ++ l_3 (APP2) | ||
+ | = (Cons(x, l_1) ++ l_2) ++ l_3 (APP2) | ||
+ | |||
+ | * $ \forall l_1, l_2 \in \texttt{List}. length(append(l_1, l_2)) = length(append(l_2, l_1)) $ | ||
+ | |||
+ | |||
+ | facem inductie structurala dupa l_1 | ||
+ | cazul de baza: | ||
+ | length(append(Empty, l_2)) = length(l_2) (APP1) | ||
+ | = length(append(l_2, Empty)) (4.c) | ||
+ | |||
+ | ipoteza inductiei: length(append(l_1, l_2)) = length(append(l_2, l_1)) | ||
+ | pasul inductiei: | ||
+ | length(append(Cons(x, l_1), l_2)) = length(Cons(x, append(l_1, l_2))) (APP2) | ||
+ | = 1 + length(append(l_1, l_2)) (L2) | ||
+ | = 1 + length(l_1) + length(l_2) (LAPP) | ||
+ | = length(l_2) + 1 + length(l_1) (comutativitatea adunării) | ||
+ | = length(l_2) + length(Cons(x, l_1)) (L2) | ||
+ | = length(append(l_2, Cons(x, l_1))) (LAPP) | ||
+ | |||
+ | | ||
+ | |||
+ | * $ \forall l_1, l_2 \in \texttt{List}. reverse(append(l_1, l_2)) = append(reverse(l_2), reverse(l_1)) $. | ||
+ | facem inductie structurala dupa l_1 | ||
+ | cazul de baza: | ||
+ | reverse(append(Empty, l_2)) = reverse(l_2) (APP1) | ||
+ | = append(reverse(l_2), Empty) (4.c) | ||
+ | = append(reverse(l_2), reverse(Empty)) (REV1) | ||
+ | |||
+ | ipoteza inductiei: reverse(append(l_1, l_2)) = append(reverse(l_2), reverse(l_1)) | ||
+ | pasul inductiei: | ||
+ | |||
+ | reverse(append(Cons(x, l_1), l_2)) = reverse(Cons(x, append(l_1, l_2))) (APP2) | ||
+ | = append(reverse(append(l_1, l_2)), Cons(x, Empty)) (REV2) | ||
+ | = append(append(reverse(l_2), reverse(l_1)), Cons(x, Empty)) (ip. inductivă) | ||
+ | = append(reverse(l_2), append(reverse(l_1), Cons(x, Empty))) (4.d) | ||
+ | = append(reverse(l_2), reverse(Cons(x, l_1))) (REV2) | ||