Arreglos
Octubre del 2004
Introducción
• Asignaciones que afectan arreglos
• Operacion de intercambio
• Bandera holandesa
Asignaciones a los arreglos
• h.E := F– La ejecucion afecta todo el arreglo– Sustitucion de funciones por funciones
• h (x:u).i =– u cuando x=i– h.i en caso contrario
1 0 3
1 1 3h
h (1:0)
Problemas en la asignacion
{h.(h.1) = 1}
h.(h.1) := 0
{h.0 = 1 /\ h.1 = 1}
1 1 ...
1 0 ...
{h.(h.1) = 1}
h.(h.1) := 0
{1 = 0}
Es cierto, esta terna vale, pero no satisface del todo. Uno buscaun programa que se ejecute.
Problemas en la asignacion
{h.(h.1) = 0}
h.(h.1) := 0
{h.0 = 0 /\ h.1 = 1}
0 1 ...
0 0 ...
{h.(h.1) = 0}
h.(h.1) := 0
{0 = 0}
Definitivamente, hay problemas
1 1 ...
1 0 ...
Mas problemas
x, y := 0, 0;
f.x, f.y := 0, 1
No se admiten asignaciones multiples en los arreglos
Definicion
wp.(h.E := F).Q Q(h := h(E:F))
wp.(h.E := F).Q def.F /\ def.(h.E) /\ Q(h := h(E:F))
Calculo
{h.(h.1) = 1}
h.(h.1) := 0
{h(h.1:0).(h(h.1:0).1) = 1}
h(h.1:0).(h(h.1:0).1)= (si h.1 = 1)
h(1:0).(h(1:0).1)=h(1:0).0=h.0
h(h.1:0).(h(h.1:0).1)= (si h.1 1)
h(h.1:0).(h.1)=0
h(h.1:0).(h(h.1:0).1) = 1h.1 = 1 h.0 = 1/\ h.1 1 0 = 1h.0 = 1 /\ h.1 = 1
todosCeros|[par N: int {N 0};var h: array [0..N) of int; todosCeros{(/\i : 0 i < N : h.i = 0)}]|.
Tactica: reemplazo de N por n,incrementando n
P0 : (/\i : 0 i < n : h.i = 0)P1 : 0 n N
|[par N: int {N 0};var h: array [0..N) of int; n := 0; do n N {P0 /\ P1 /\ n N} establecerInvariante {(P0 /\ P1) (n := n+1)} n := n+1 od{(/\i : 0 i < N : h.i = 0)}]|.
todosCeros|[par N: int {N 0};var h: array [0..N) of int; n := 0; do n N {P0} establecerInvariante {P0(n := n+1)} n := n+1 od{(/\i : 0 i < N : h.i = 0)}]|.
P0(n := n+1)(/\i : 0 i < n+1 : h.i = 0)(/\i : 0 i < n : h.i = 0) /\ h.n = 0P0 /\ h.n = 0
P0(/\i : 0 i < n : h.i = 0)(/\i : 0 i < n : h(n:0).i = 0)P0(h := h(n:0))
todosCeros|[par N: int {N 0};var h: array [0..N) of int; n := 0; do n N {P0(h := h(n:0)} h.n := 0 {P0 /\ h.n = 0} n := n+1 od{(/\i : 0 i < N : h.i = 0)}]|.
P0(n := n+1)(/\i : 0 i < n+1 : h.i = 0)(/\i : 0 i < n : h.i = 0) /\ h.n = 0P0 /\ h.n = 0
P0(/\i : 0 i < n : h.i = 0)(/\i : 0 i < n : h(n:0).i = 0)P0(h := h(n:0))
h(n:0).n = 0 vale por definicion
todosCeros|[par N: int {N 0};var h: array [0..N) of int; todosCeros{(/\i : 0 i < N : h.i = 0)}]|.
Tactica: reemplazo de N por n,incrementando n
P0 : (/\i : 0 i < n : h.i = 0)P1 : 0 n N
|[par N: int {N 0};var h: array [0..N) of int; n := 0; do n N {P0} h.n := 0 {P0 (n := n+1)} n := n+1 od{(/\i : 0 i < N : h.i = 0)}]|.
P0 : (/\i : 0 i < n : h.i = H.i)P1 : 0 n N
asignacionSimpleEnArreglos|[par N: int {N 0};var h: array [0..N) of int; asignacionSimpleEnArreglos{(/\i : 0 i < N : h.i = H.i)}]|.
Tactica: reemplazo de N por n,incrementando n
|[par N: int {N 0};var h: array [0..N) of int; n := 0; do n N {P0 /\ E = H.n} h.n := E {P0 (n := n+1)} n := n+1 od{(/\i : 0 i < N : h.i = H.i)}]|.
cuando h no aparece
en H
dados|[par N: int {N 0};par X: array [0..N) of int;{(/\i : 0 i < N : 1 X.i 6)}var h: array [1..6] of int; dados{(/\i : 1 i 6 : h.i = (#k : 0 k < N : X.k = i))}]|.
P0 : (/\i : 1 i 6 : h.i = (#k : 0 k < n : X.k = i))P1 : 0 n N
|[ ... n := 0; algoParecidoAtodosCeros do n N {P0} establecerInvariante {P0(n := n+1)} n := n+1 od... ]|.
dados
h(X.n : h.(X.n)+1).i= (i X.n)
h.i= (P0)
(#k : 0 k < n : X.k = i) + #.(X.n=i)
h(X.n : h.(X.n)+1).i= (i = X.n)
h.(X.n)+1= (P0)
(#k : 0 k < n : X.k = X.n) + 1=(#k : 0 k < n : X.k = i) + #.(X.n=i)
P0 : (/\i : 1 i 6 : h.i = (#k : 0 k < n : X.k = i))
P0(n := n+1) (/\i : 1 i 6 : h.i = (#k : 0 k < n+1 : X.k = i)) (/\i : 1 i 6 : h.i = (#k : 0 k < n : X.k = i) + #.(X.n=i))
dados
|[ ... n := 0; algoParecidoAtodosCeros do n N {P0(n := n+1)(h := h(X.n : h.(X.n)+1))} h.(X.n) := h.(X.n) + 1 {P0(n := n+1)} n := n+1 od... ]|.
|[ ... n := 0; algoParecidoAtodosCeros do n N {P0} establecerInvariante {P0(n := n+1)} n := n+1 od... ]|.
Intercambios
x, y := y, xh.x, h.y := h.y, h.x;
No se admiten asignaciones multiples en los arreglos
Intercambios
h (i, j: A, B).i = Ah (i, j: A, B).j = Bh (i, j: A, B).k = h.kh (i, i: A, B).i = ...
wp.(swap.h.E.F).Q
Q(h := h(E,F : h.E,h.F))
wp.(swap.h.E.F).Q
def.(h.E) /\ def.(h.F) /\ Q(h := h(E,F : h.E,h.F))
Intercambio simple
{(/\i : iE /\ iF : h.i=H.i) /\ h.E=A /\ h.F=B}swap.h.E.F{(/\i : iE /\ iF : h.i=H.i) /\ h.E=B /\ h.F=A}
cuando h no ocurreni en E ni en F
Bandera holandesa
?
Bandera holandesa|[par N: int {N 0};var h: array [0..N) of [rojo, blanco, azul]; banderaHolandesa{(\/p,q : 0 p q N : (/\i : 0 i < p : h.i = rojo) /\ (/\i : p i < q : h.i = blanco) /\ (/\i : q i < N : h.i = azul))}]|.
Solo se admitenintercambios
R: (/\i : 0 i < r : h.i = rojo)
/\ (/\i : r i < w : h.i = blanco)
/\ (/\i : w i < N : h.i = azul)
Decision !!!Agregar una
variable b
Bandera holandesa
r bw
Pr: (/\i : 0 i < r: h.i = rojo)
Pw: (/\i : r i < w: h.i=blanco)
Pb: (/\i : b i < N: h.i = azul)
P1: 0 r w b N
Este largo es la cota
Inicializacion: r, w, b := 0, 0, NCondicion de fin: b = w
Bandera holandesa|[par N: int {N 0};var h: array [0..N) of [rojo, blanco, azul];var r, w, b: int; r, w, b := 0, 0, N {inv. Pr /\ Pw /\ Pb /\ P1, cota. b-w} do w b if h.w = red Sr [] h.w = white Sw [] h.w = blue Sb fi od]|.
Decision !!!Investigar
la posicion w
Top Related