arreglos octubre del 2004. introducción asignaciones que afectan arreglos operacion de intercambio...

23
Arreglos Octubre del 2004

Upload: natividad-torres-munoz

Post on 02-Feb-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

Arreglos

Octubre del 2004

Page 2: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

Introducción

• Asignaciones que afectan arreglos

• Operacion de intercambio

• Bandera holandesa

Page 3: 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)

Page 4: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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.

Page 5: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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 ...

Page 6: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

Mas problemas

x, y := 0, 0;

f.x, f.y := 0, 1

No se admiten asignaciones multiples en los arreglos

Page 7: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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))

Page 8: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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

Page 9: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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)}]|.

Page 10: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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))

Page 11: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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

Page 12: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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)}]|.

Page 13: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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

Page 14: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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... ]|.

Page 15: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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))

Page 16: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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... ]|.

Page 17: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

Intercambios

x, y := y, xh.x, h.y := h.y, h.x;

No se admiten asignaciones multiples en los arreglos

Page 18: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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))

Page 19: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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

Page 20: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

Bandera holandesa

?

Page 21: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio 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

Page 22: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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

Page 23: Arreglos Octubre del 2004. Introducción Asignaciones que afectan arreglos Operacion de intercambio Bandera holandesa

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